在OpenCV中利用鼠标马赛克图像部分区域


这是一个简单的马赛克程序,利用选定矩形区域的平均值代替该区域各点的像素值。大家有没有更好的方法马赛克呢?

  1. #include <cv.h>  
  2. #include <highgui.h>  
  3. #pragma comment( lib, "cv.lib" )  
  4. #pragma comment( lib, "cxcore.lib" )  
  5. #pragma comment( lib, "highgui.lib" )   
  6. IplImage* org = 0;  
  7. IplImage* img = 0;   
  8. IplImage* tmp = 0;  
  9. IplImage* dst = 0;   
  10. int foo=6;  
  11. void on_mouse( int eventint x, int y, int flags, void* ustc)  
  12. {  
  13.     CvPoint p0;  
  14.     CvPoint p1;  
  15.     ifevent == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON))  
  16.     {  
  17.         img=cvCloneImage(tmp);  
  18.         cvResetImageROI(img);  
  19.         if(x<foo)  
  20.         {  
  21.             if(y<foo)  
  22.             {  
  23.                 p0=cvPoint(0,0);  
  24.                 p1=cvPoint(2*foo,2*foo);  
  25.             }  
  26.             else if(y>img->height-foo)  
  27.             {   
  28.                 p0=cvPoint(0,img->height-2*foo);  
  29.                 p1=cvPoint(2*foo,img->height);  
  30.             }  
  31.             else  
  32.             {  
  33.                 p0=cvPoint(0,y-foo);  
  34.                 p1=cvPoint(2*foo,y+foo);  
  35.             }  
  36.         }  
  37.         else if(x>img->width-foo)  
  38.         {  
  39.             if(y<foo)  
  40.             {  
  41.                 p0=cvPoint(img->width-2*foo,0);  
  42.                 p1=cvPoint(img->width,2*foo);  
  43.             }  
  44.             else if(y>img->height-foo)  
  45.             {   
  46.                 p0=cvPoint(img->width-2*foo,img->height-2*foo);  
  47.                 p1=cvPoint(img->width,img->height);  
  48.             }  
  49.             else  
  50.             {  
  51.                 p0=cvPoint(img->width-2*foo,y-foo);  
  52.                 p1=cvPoint(img->width,y+foo);  
  53.             }  
  54.         }  
  55.         else  
  56.         {  
  57.             if(y<foo)  
  58.             {  
  59.                 p0=cvPoint(x-foo,0);  
  60.                 p1=cvPoint(x+foo,2*foo);  
  61.             }  
  62.             else if(y>img->height-foo)  
  63.             {   
  64.                 p0=cvPoint(x-foo,img->height-2*foo);  
  65.                 p1=cvPoint(x+foo,img->height);  
  66.             }  
  67.             else  
  68.             {  
  69.                 p0=cvPoint(x-foo,y-foo);  
  70.                 p1=cvPoint(x+foo,y+foo);  
  71.             }  
  72.         }  
  73.         cvRectangle(img,p0,p1,CV_RGB(0,255,0));  
  74.         cvShowImage( "img", img );  
  75.     }  
  76.     else ifevent == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))  
  77.     {  
  78.         if(x<foo)  
  79.         {  
  80.             if(y<foo)  
  81.             {  
  82.                 p0=cvPoint(0,0);  
  83.                 p1=cvPoint(2*foo,2*foo);  
  84.             }  
  85.             else if(y>img->height-foo)  
  86.             {   
  87.                 p0=cvPoint(0,img->height-2*foo);  
  88.                 p1=cvPoint(2*foo,img->height);  
  89.             }  
  90.             else  
  91.             {  
  92.                 p0=cvPoint(0,y-foo);  
  93.                 p1=cvPoint(2*foo,y+foo);  
  94.             }  
  95.         }  
  96.         else if(x>img->width-foo)  
  97.         {  
  98.             if(y<foo)  
  99.             {  
  100.                 p0=cvPoint(img->width-2*foo,0);  
  101.                 p1=cvPoint(img->width,2*foo);  
  102.             }  
  103.             else if(y>img->height-foo)  
  104.             {   
  105.                 p0=cvPoint(img->width-2*foo,img->height-2*foo);  
  106.                 p1=cvPoint(img->width,img->height);  
  107.             }  
  108.             else  
  109.             {  
  110.                 p0=cvPoint(img->width-2*foo,y-foo);  
  111.                 p1=cvPoint(img->width,y+foo);  
  112.             }  
  113.         }  
  114.         else  
  115.         {  
  116.             if(y<foo)  
  117.             {  
  118.                 p0=cvPoint(x-foo,0);  
  119.                 p1=cvPoint(x+foo,2*foo);  
  120.             }  
  121.             else if(y>img->height-foo)  
  122.             {   
  123.                 p0=cvPoint(x-foo,img->height-2*foo);  
  124.                 p1=cvPoint(x+foo,img->height);  
  125.             }  
  126.             else  
  127.             {  
  128.                 p0=cvPoint(x-foo,y-foo);  
  129.                 p1=cvPoint(x+foo,y+foo);  
  130.             }  
  131.         }  
  132.         dst=cvCloneImage(tmp);    
  133.         cvSetImageROI(dst,cvRect(p0.x,p0.y,p1.x-p0.x,p1.y-p0.y));  
  134.         CvScalar mean=cvAvg(dst);  
  135.         cvSet(dst,mean);  
  136.         cvResetImageROI(dst);  
  137.         tmp=cvCloneImage(dst);  
  138.         cvRectangle(dst,p0,p1,CV_RGB(0,255,0));  
  139.         cvShowImage( "img", dst );        
  140.     }  
  141. }  
  142. int main()  
  143. {  
  144.     org=cvLoadImage("lena.jpg",1);  
  145.     img=cvCloneImage(org);  
  146.     tmp=cvCloneImage(org);  
  147.     dst=cvCloneImage(org);  
  148.     cvNamedWindow("img",1);  
  149.     cvSetMouseCallback( "img", on_mouse, 0 );  
  150.     cvShowImage("img",img);  
  151.     cvWaitKey(0);   
  152.     cvDestroyAllWindows();  
  153.     cvReleaseImage(&org);  
  154.     cvReleaseImage(&img);  
  155.     cvReleaseImage(&tmp);  
  156.     cvReleaseImage(&dst);  
  157.     return 0;  
  158. }  

效果图如下,这里马赛克了lena图的左眼。

相关内容