学习了其他一些图像处理的函数,在上一个程序的基础上测试一下。【详见注释】
1 #include <cv.h>
2 #include <highgui.h>
3 #include <iostream.h>
4 #include <stdlib.h>
5
6 //定义宏
7 #define CV_IMAGE_ELEM(img, T,pt,c)\
8 &((T*)(img->imageData+img->widthStep*pt.y))[pt.x*img->nChannels + c]
9
10 void main(int argc,char*argv[])
11 {
12 IplImage*img1=0,*img2=0;
13 int height,width,step,channel;
14 uchar *data;
15 CvPoint pt=cvPoint(100,100);
16 /*if (argc<3)
17 {
18 cout<<"ERROR:Please Enter the Names of TWO Images!"<<endl;
19 exit(0);
20 }*/
21 argv[1]="Lena.jpg";
22 argv[2]="temp.jpg";
23 img1=cvLoadImage(argv[1],1);
24 if(!img1)
25 {
26 cout<<"File open ERROR!!"<<endl;
27 exit(0);
28 }
29 height=img1->height;
30 width=img1->width;
31 step=img1->widthStep;
32 channel=img1->nChannels;
33 data=(uchar*)img1->imageData;
34
35 //改变img1中pt点的像素值
36 uchar* Ptemp=CV_IMAGE_ELEM(img1,uchar,pt,0);
37 Ptemp[0]+=30;
38 Ptemp[1]+=30;
39 Ptemp[2]+=30;
40
41 //创建新图像img2(两种方法:第一种只创建空间,并不初始化里面的数据;第二种:创建并初始化数据)
42 img2=cvCreateImageHeader(cvGetSize(img1),img1->depth,channel);
43 cvCreateData(img2);
44 //img2=cvCreateImage(cvGetSize(img1),img1->depth,channel);
45 //cvCopyImage(img1,img2,NULL);
46
47 cvNamedWindow(argv[1],CV_WINDOW_AUTOSIZE);
48 cvMoveWindow(argv[1],0,0);
49 cvNamedWindow("Befoer Process",CV_WINDOW_AUTOSIZE);
50 cvShowImage("Befoer Process",img1);
51 cvSetImageROI(img1,cvRect(width/2,0,width/2,height)); //设置ROI(兴趣区域)
52 //类似的函数还有cvSetImageCOI(设置兴趣通道)cvResetImageROI(重设兴趣区域)
53 //cvGetImageROI(获取兴趣区域)cvGetImageCOI(获得兴趣通道)
54 //进行颜色处理
55 cout<<"Process image "<<argv[1]<<"(Size: "<<width<<"*"<<height<<", Channels: "<<channel<<"WidthStep: "<<step<<" )"<<endl;
56 for(int i=0;i<width;i++)
57 for(int j=0;j<height;j++)
58 //for(int k=0;k<=channel;k++)
59 {
60 //data[i*channel+j*step+k]=-data[i*channel+j*step+k];
61 //img2->imageData[i*channel+j*step+k]=data[i*channel+j*step+k];
62 data[i*channel+j*step+0]*=-1;
63 data[i*channel+j*step+1]*=-1;
64 data[i*channel+j*step+2]*=-1;
65
66 //测试3个通道的颜色:0 B 1 G 3 R
67 }
68 //显示图像
69 cvPolyLine(img1,cvPoint(),)
70 cvShowImage(argv[1],img1);
71 cvWaitKey(0);
72 cvSaveImage(argv[2],img2);
73 cvDestroyWindow(argv[1]);
74 cvDestroyWindow("Before Process");
75 cvReleaseImage(&img1);
76 //leaseImage(&img2);
77 cvReleaseImageHeader(&img2);
78 }
79
2 #include <highgui.h>
3 #include <iostream.h>
4 #include <stdlib.h>
5
6 //定义宏
7 #define CV_IMAGE_ELEM(img, T,pt,c)\
8 &((T*)(img->imageData+img->widthStep*pt.y))[pt.x*img->nChannels + c]
9
10 void main(int argc,char*argv[])
11 {
12 IplImage*img1=0,*img2=0;
13 int height,width,step,channel;
14 uchar *data;
15 CvPoint pt=cvPoint(100,100);
16 /*if (argc<3)
17 {
18 cout<<"ERROR:Please Enter the Names of TWO Images!"<<endl;
19 exit(0);
20 }*/
21 argv[1]="Lena.jpg";
22 argv[2]="temp.jpg";
23 img1=cvLoadImage(argv[1],1);
24 if(!img1)
25 {
26 cout<<"File open ERROR!!"<<endl;
27 exit(0);
28 }
29 height=img1->height;
30 width=img1->width;
31 step=img1->widthStep;
32 channel=img1->nChannels;
33 data=(uchar*)img1->imageData;
34
35 //改变img1中pt点的像素值
36 uchar* Ptemp=CV_IMAGE_ELEM(img1,uchar,pt,0);
37 Ptemp[0]+=30;
38 Ptemp[1]+=30;
39 Ptemp[2]+=30;
40
41 //创建新图像img2(两种方法:第一种只创建空间,并不初始化里面的数据;第二种:创建并初始化数据)
42 img2=cvCreateImageHeader(cvGetSize(img1),img1->depth,channel);
43 cvCreateData(img2);
44 //img2=cvCreateImage(cvGetSize(img1),img1->depth,channel);
45 //cvCopyImage(img1,img2,NULL);
46
47 cvNamedWindow(argv[1],CV_WINDOW_AUTOSIZE);
48 cvMoveWindow(argv[1],0,0);
49 cvNamedWindow("Befoer Process",CV_WINDOW_AUTOSIZE);
50 cvShowImage("Befoer Process",img1);
51 cvSetImageROI(img1,cvRect(width/2,0,width/2,height)); //设置ROI(兴趣区域)
52 //类似的函数还有cvSetImageCOI(设置兴趣通道)cvResetImageROI(重设兴趣区域)
53 //cvGetImageROI(获取兴趣区域)cvGetImageCOI(获得兴趣通道)
54 //进行颜色处理
55 cout<<"Process image "<<argv[1]<<"(Size: "<<width<<"*"<<height<<", Channels: "<<channel<<"WidthStep: "<<step<<" )"<<endl;
56 for(int i=0;i<width;i++)
57 for(int j=0;j<height;j++)
58 //for(int k=0;k<=channel;k++)
59 {
60 //data[i*channel+j*step+k]=-data[i*channel+j*step+k];
61 //img2->imageData[i*channel+j*step+k]=data[i*channel+j*step+k];
62 data[i*channel+j*step+0]*=-1;
63 data[i*channel+j*step+1]*=-1;
64 data[i*channel+j*step+2]*=-1;
65
66 //测试3个通道的颜色:0 B 1 G 3 R
67 }
68 //显示图像
69 cvPolyLine(img1,cvPoint(),)
70 cvShowImage(argv[1],img1);
71 cvWaitKey(0);
72 cvSaveImage(argv[2],img2);
73 cvDestroyWindow(argv[1]);
74 cvDestroyWindow("Before Process");
75 cvReleaseImage(&img1);
76 //leaseImage(&img2);
77 cvReleaseImageHeader(&img2);
78 }
79