//例3-12 用imageROI增加某范围的像素值 #include "stdafx.h" #include <cv.h> #include <highgui.h> int main(int argc, char** argv) { IplImage* src; if (argc==7&&((src=cvLoadImage(argv[1],1))!=0)) { int x=atoi(argv[2]); int y=atoi(argv[3]); int width=atoi(argv[4]); int height=atoi(argv[5]); int add=atoi(argv[6]); cvSetImageROI(src,cvRect(x,y,width,height)); cvAddS(src,cvScalar(add),src); cvResetImageROI(src); cvNamedWindow("Roi_Add",1); cvShowImage("Roi_Add",src); cvWaitKey(0); } return 0; } 笔记: 输入的参数为D:/try0621/3-12/debug>3-12.exe b.jpg 660 180 100 100 150 //例3-13 增加像素值的另一种方法 #include "stdafx.h" #include <cv.h> #include <highgui.h> int main(int argc,char** argv) { cvNamedWindow("ha"); IplImage* interest_img=cvLoadImage(argv[1],1); cvShowImage(" interest_img",interest_img); CvRect interest_rect=cvRect(300,500,200,200); IplImage* sub_img=cvCreateImageHeader(cvSize(interest_rect.width,interest_rect.height),interest_img->depth,interest_img->nChannels); //cvShowImage("sub_img ",sub_img);//这里不宜输出,会出错的 sub_img->origin=interest_img->origin; sub_img->widthStep=interest_img->widthStep; sub_img->imageData=interest_img->imageData+interest_rect.y*interest_img->widthStep+interest_rect.x*interest_img->nChannels;printf("here"); cvAddS(sub_img,cvScalar(50),sub_img); cvShowImage("之后",sub_img); cvWaitKey(0); cvReleaseImageHeader(&sub_img); cvReleaseImage(&interest_img); cvDestroyWindow("la"); return 0; } 笔记: 增加像素 //例3-14 图像叠加 #include "stdafx.h" #include <cv.h> #include <highgui.h> int main(int argc, char** argv) { IplImage *src1,*src2; //if (argc==9&&((src1=cvLoadImage(argv[1],1))!=0)&&((src2=cvLoadImage(argv[2],1))!=0)) { src1=cvLoadImage(argv[1],1); src2=cvLoadImage(argv[2],1); //int x=atoi(argv[3]); //int y=atoi(argv[4]); //int width=atoi(argv[5]);//200 //int height=atoi(argv[6]);//100 //double alpha=(double)atof(argv[7]); //double beta=(double)atof(argv[8]); cvSetImageROI(src1,cvRect(500,350,280,150)); cvSetImageROI(src2,cvRect(380,190,280,150)); cvAddWeighted(src1,0.3,src2,0.7,0.0,src1); cvResetImageROI(src1); cvNamedWindow("Alpha_blend",1); cvShowImage("Alpha_blend",src1); cvWaitKey(); } return 0; } //另外一个练习 int main(int argc, char** argv) { cvNamedWindow("图像叠加",1); IplImage *src1,*src2;//src1=src1+src2。二者的ROI必须是相同的尺寸 src1=cvLoadImage(argv[1],1); cvSetImageROI(src1,cvRect(400,300,280,150));//被覆盖的区域 cvShowImage("src1",src1); src2=cvLoadImage(argv[2],1); cvSetImageROI(src2,cvRect(380,190,280,150));//用来填充的区域 cvShowImage("src2",src2); cvAddWeighted(src1,0.3,src2,0.7,0.0,src1); cvResetImageROI(src1); cvShowImage("生成的目标图像",src1); cvWaitKey(0); return 0; } 笔记: 找出希望的区域是一件很困难的事情 cvRect(起点横轴,起点纵轴,宽度,高度) cvAddWeighted(加数1,加数1的系数,加数2,加数2的系数,gamma,生成的目标图像) 生成的目标图像一般为加数1 //例3-15 存储和读取CvMat--使用xml文件 // 3-15存储和读取CvMat.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <cv.h> #include <highgui.h> int main(int argc, char** argv) { float the_matrix_data[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; CvMat A=cvMat(4,4,CV_32F,the_matrix_data); cvSave("my_matrix.xml",&A); CvMat* A1=(CvMat*)cvLoad("my_matrix.xml"); printf("%f",cvGet2D(A1,1,1)); return 0; } //生成的xml文件: <?xml version="1.0"?> <opencv_storage> <my_matrix type_id="opencv-matrix"> <rows>4</rows> <cols>4</cols> <dt>f</dt> <data> 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. </data> </my_matrix> </opencv_storage> 笔记: 希望能够借此学习xml 生成的还是比较规范的