opencv--学习之感兴趣区域(ROI)

来源:互联网 发布:马扎克加工中心编程 编辑:程序博客网 时间:2024/06/05 16:37

两种获得感兴趣矩形区域的方式


一个是直接用函数  cvSetImageROI(IplImage* image, Cvrect  rect),    其中image是加载的一幅图像,  rect =  cvRect(x, y, width,  height) ,x,y给出了矩形在原图像中的起点(从左上起),width,height给出了矩形的宽和高。    书中示例给出了将该矩形区域的蓝色通道增加150  ( 即语句cvAddS(interestimg, cvScalar(add), interestimg))后的图像输出。书中源码如下:


[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <cv.h>  
  2. #include <highgui.h>  
  3. // ch3_ex3_12 image_name x y width height add#   
  4. int main(int argc, char** argv)  
  5. {  
  6.   
  7.     IplImage* src;  
  8.     cvNamedWindow("Example3_12_pre", CV_WINDOW_AUTOSIZE);  
  9.     cvNamedWindow("Example3_12_post", CV_WINDOW_AUTOSIZE);    
  10.     if( argc == 7 && ((src=cvLoadImage(argv[1],1)) != 0 ))  
  11.     {  
  12.         int x = atoi(argv[2]);  
  13.         int y = atoi(argv[3]);  
  14.         int width = atoi(argv[4]);  
  15.         int height = atoi(argv[5]);  
  16.         int add = atoi(argv[6]);  
  17.         cvShowImage( "Example3_12_pre", src);  
  18.         cvSetImageROI(src, cvRect(x,y,width,height));  
  19.         cvAddS(src, cvScalar(add),src);  
  20.         cvResetImageROI(src);  
  21.         cvShowImage( "Example3_12_post",src);  
  22.       cvWaitKey();  
  23.     }  
  24.   cvReleaseImage( &src );  
  25.   cvDestroyWindow("Example3_12_pre");  
  26.   cvDestroyWindow("Example3_12_post");     
  27.     return 0;  
  28. }  


另一个方法是通过使用widthStep来直接达到与上面相同的效果,这个相当于通过原图的信息来建立一个需要的长宽的矩形IplImage类型,然后再根据期望的感兴趣区域位置,找到矩形的左上角在原图上的坐标,这样就成功划出了感兴趣矩形区域了

与此同时,根据后面的alpha融合的内容,试着将载入了另一幅图像,然后选取了该图像同样大小的一部分,和原图的感兴趣区域按一定的权值融合了,于是使得操作区域出现了“你中有我,我中有你”的现象

 

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <cv.h>  
  2. #include <highgui.h>  
  3. // ch3_ex3_12 image_name x y width height add#   
  4. int main(int argc, char** argv)  
  5. {  
  6.   
  7.     IplImage* src;  
  8.     IplImage* src2;  
  9.     cvNamedWindow("Example3_12_pre", CV_WINDOW_AUTOSIZE);  
  10.     cvNamedWindow("Example3_12_post", CV_WINDOW_AUTOSIZE);  
  11.     if (argc == 10 && ((src = cvLoadImage(argv[1], 1)) != 0))  
  12.     {  
  13.         src2 = cvLoadImage(argv[9]);  
  14.         IplImage* interestimg = cvCreateImageHeader(  
  15.             cvSize(250, 300),  
  16.             src->depth,  
  17.             src->nChannels  
  18.             );  
  19.         IplImage* interestimg2 = cvCreateImageHeader(  
  20.             cvSize(250, 300),  
  21.             src->depth,  
  22.             src->nChannels  
  23.             );  
  24.         interestimg2->origin = src2->origin;  
  25.         interestimg2->widthStep = src2->widthStep;  
  26.         interestimg2->imageData = src2->imageData + 12 * src2->widthStep + 12 * src2->nChannels;  
  27.         interestimg->origin = src->origin;  
  28.         interestimg->widthStep = src->widthStep;  
  29.         interestimg->imageData = src->imageData + 120 * src->widthStep + 120 * src->nChannels;  
  30.         int add = atoi(argv[6]);  
  31.         //double alpha = (double)atof(argv[7]);  
  32.         //double beta = (double)atof(argv[8]);  
  33.         cvShowImage("Example3_12_pre", src);  
  34.         //cvAddS(interestimg, cvScalar(add), interestimg);  
  35.         cvAddWeighted(interestimg, alpha, interestimg2, beta, 0.0, interestimg);  
  36.         //interestimg->imageData = src->imageData + 120 * src->widthStep + 120 * src->nChannels + 1;  
  37.         //cvAddS(interestimg, cvScalar(20), interestimg);  
  38.         //interestimg->imageData = src->imageData + 120 * src->widthStep + 120 * src->nChannels + 2;  
  39.         //cvAddS(interestimg, cvScalar(30), interestimg);  
  40.         cvResetImageROI(src);  
  41.         cvShowImage("Example3_12_post", src);  
  42.         cvWaitKey();  
  43.     }  
  44.     cvReleaseImage(&src);  
  45.     cvDestroyWindow("Example3_12_pre");  
  46.     cvDestroyWindow("Example3_12_post");  
  47.     return 0;  
  48. }  


书中源程序使用

         sub_img->imageData = interest_img->imageData + 
          interest_rect.y * interest_img->widthStep  +
          interest_rect.x * interest_img->nChannels;

来找到感兴趣区域的左上角坐标,这样是对蓝色通道数据进行操作,我们可以在后面+1,或+2来对红色或绿色通道来操作,如:interestimg->imageData = src->imageData + 120 * src->widthStep + 120 * src->nChannels + 1;来对红色通道操作


在本程序中,出项了之前没用过的函数“atoi()”,这是一个将字符转换为整形的函数,比如,命令参数中输入的字符串“12”,可以用该函数转换为整形数“12”,同样,atof()将字符转换为浮点型……

0 0
原创粉丝点击