读图像,对文件进行颜色反色操作,显示图像,保存图像。
读图像,对文件进行颜色反色操作,显示图像,保存图像。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 #include <cv.h>
2 #include <highgui.h>
3 #include <iostream.h>
4 #include <stdlib.h>
5
6 void main(int argc,char*argv[])
7 {
8 IplImage*img1=0,*img2=0;
9 int height,width,step,channel;
10 uchar *data;
11 if (argc<3)
12 {
13 cout<<"ERROR:Please Enter the Names of TWO Images!"<<endl;
14 exit(0);
15 }
16 img1=cvLoadImage(argv[1],1);
17 if(!img1)
18 {
19 cout<<"File open ERROR!!"<<endl;
20 exit(0);
21 }
22 height=img1->height;
23 width=img1->width;
24 step=img1->widthStep;
25 channel=img1->nChannels;
26 data=(uchar*)img1->imageData;
27
28 img2=cvCreateImage(cvGetSize(img1),img1->depth,channel);
29 cvCopyImage(img1,img2,NULL);
30
31 cvNamedWindow(argv[1],CV_WINDOW_AUTOSIZE);
32 cvMoveWindow(argv[1],0,0);
33 cout<<"Process image "<<argv[1]<<"(Size: "<<width<<"*"<<height<<", Channels: "<<channel<<"WidthStep: "<<step<<" )"<<endl;
34 for(int i=0;i<width;i++)
35 for(int j=0;j<height;j++)
36 for(int k=0;k<=channel;k++)
37 {
38 data[i*channel+j*step+k]=-data[i*channel+j*step+k];
39 img2->imageData[i*channel+j*step+k]=data[i*channel+j*step+k];
40 }
41 cvShowImage(argv[1],img1);
42 cvWaitKey(0);
43 cvSaveImage(argv[2],img2);
44 cvDestroyWindow(argv[1]);
45 cvReleaseImage(&img1);
46 cvReleaseImage(&img2);
47 }