欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

鼠标马赛克图像部分区域

发布时间:2025/4/16 编程问答 29 豆豆
生活随笔 收集整理的这篇文章主要介绍了 鼠标马赛克图像部分区域 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

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

[c-sharp] view plaincopy
  • #include <cv.h>  
  • #include <highgui.h>  
  • #pragma comment( lib, "cv.lib" )  
  • #pragma comment( lib, "cxcore.lib" )  
  • #pragma comment( lib, "highgui.lib" )  
  • IplImage* org = 0;  
  • IplImage* img = 0;   
  • IplImage* tmp = 0;  
  • IplImage* dst = 0;   
  • int foo=6;  
  • void on_mouse( int eventint x, int y, int flags, void* ustc)  
  • {  
  •     CvPoint p0;  
  •     CvPoint p1;  
  •     ifevent == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON))  
  •     {  
  •         img=cvCloneImage(tmp);  
  •         cvResetImageROI(img);  
  •         if(x<foo)  
  •         {  
  •             if(y<foo)  
  •             {  
  •                 p0=cvPoint(0,0);  
  •                 p1=cvPoint(2*foo,2*foo);  
  •             }  
  •             else if(y>img->height-foo)  
  •             {   
  •                 p0=cvPoint(0,img->height-2*foo);  
  •                 p1=cvPoint(2*foo,img->height);  
  •             }  
  •             else  
  •             {  
  •                 p0=cvPoint(0,y-foo);  
  •                 p1=cvPoint(2*foo,y+foo);  
  •             }  
  •         }  
  •         else if(x>img->width-foo)  
  •         {  
  •             if(y<foo)  
  •             {  
  •                 p0=cvPoint(img->width-2*foo,0);  
  •                 p1=cvPoint(img->width,2*foo);  
  •             }  
  •             else if(y>img->height-foo)  
  •             {   
  •                 p0=cvPoint(img->width-2*foo,img->height-2*foo);  
  •                 p1=cvPoint(img->width,img->height);  
  •             }  
  •             else  
  •             {  
  •                 p0=cvPoint(img->width-2*foo,y-foo);  
  •                 p1=cvPoint(img->width,y+foo);  
  •             }  
  •         }  
  •         else  
  •         {  
  •             if(y<foo)  
  •             {  
  •                 p0=cvPoint(x-foo,0);  
  •                 p1=cvPoint(x+foo,2*foo);  
  •             }  
  •             else if(y>img->height-foo)  
  •             {   
  •                 p0=cvPoint(x-foo,img->height-2*foo);  
  •                 p1=cvPoint(x+foo,img->height);  
  •             }  
  •             else  
  •             {  
  •                 p0=cvPoint(x-foo,y-foo);  
  •                 p1=cvPoint(x+foo,y+foo);  
  •             }  
  •         }  
  •         cvRectangle(img,p0,p1,CV_RGB(0,255,0));  
  •         cvShowImage( "img", img );  
  •     }  
  •     else ifevent == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))  
  •     {  
  •         if(x<foo)  
  •         {  
  •             if(y<foo)  
  •             {  
  •                 p0=cvPoint(0,0);  
  •                 p1=cvPoint(2*foo,2*foo);  
  •             }  
  •             else if(y>img->height-foo)  
  •             {   
  •                 p0=cvPoint(0,img->height-2*foo);  
  •                 p1=cvPoint(2*foo,img->height);  
  •             }  
  •             else  
  •             {  
  •                 p0=cvPoint(0,y-foo);  
  •                 p1=cvPoint(2*foo,y+foo);  
  •             }  
  •         }  
  •         else if(x>img->width-foo)  
  •         {  
  •             if(y<foo)  
  •             {  
  •                 p0=cvPoint(img->width-2*foo,0);  
  •                 p1=cvPoint(img->width,2*foo);  
  •             }  
  •             else if(y>img->height-foo)  
  •             {   
  •                 p0=cvPoint(img->width-2*foo,img->height-2*foo);  
  •                 p1=cvPoint(img->width,img->height);  
  •             }  
  •             else  
  •             {  
  •                 p0=cvPoint(img->width-2*foo,y-foo);  
  •                 p1=cvPoint(img->width,y+foo);  
  •             }  
  •         }  
  •         else  
  •         {  
  •             if(y<foo)  
  •             {  
  •                 p0=cvPoint(x-foo,0);  
  •                 p1=cvPoint(x+foo,2*foo);  
  •             }  
  •             else if(y>img->height-foo)  
  •             {   
  •                 p0=cvPoint(x-foo,img->height-2*foo);  
  •                 p1=cvPoint(x+foo,img->height);  
  •             }  
  •             else  
  •             {  
  •                 p0=cvPoint(x-foo,y-foo);  
  •                 p1=cvPoint(x+foo,y+foo);  
  •             }  
  •         }  
  •         dst=cvCloneImage(tmp);    
  •         cvSetImageROI(dst,cvRect(p0.x,p0.y,p1.x-p0.x,p1.y-p0.y));  
  •         CvScalar mean=cvAvg(dst);  
  •         cvSet(dst,mean);  
  •         cvResetImageROI(dst);  
  •         tmp=cvCloneImage(dst);  
  •         cvRectangle(dst,p0,p1,CV_RGB(0,255,0));  
  •         cvShowImage( "img", dst );        
  •     }  
  • }  
  • int main()  
  • {  
  •     org=cvLoadImage("lena.jpg",1);  
  •     img=cvCloneImage(org);  
  •     tmp=cvCloneImage(org);  
  •     dst=cvCloneImage(org);  
  •     cvNamedWindow("img",1);  
  •     cvSetMouseCallback( "img", on_mouse, 0 );  
  •     cvShowImage("img",img);  
  •     cvWaitKey(0);   
  •     cvDestroyAllWindows();  
  •     cvReleaseImage(&org);  
  •     cvReleaseImage(&img);  
  •     cvReleaseImage(&tmp);  
  •     cvReleaseImage(&dst);  
  •     return 0;  
  • }  
  • 效果图如下,这里马赛克了lena图的左眼。

     

    总结

    以上是生活随笔为你收集整理的鼠标马赛克图像部分区域的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。