OpenCV实现马赛克功能-创新互联

本文实例为大家分享了OpenCV实现马赛克功能的具体代码,供大家参考,具体内容如下

站在用户的角度思考问题,与客户深入沟通,找到枝江网站设计与枝江网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站设计制作、网站制作、企业官网、英文网站、手机端网站、网站推广、域名注册雅安服务器托管、企业邮箱。业务覆盖枝江地区。

实现用按下鼠标左键拖动时,在鼠标经过的路径上打上马赛克。

马赛克的原理是将图像中选中区域的像素用这个选中区域中的某一像素覆盖。

为了不让鼠标重复经过图像中同一个的时候,选取不一样的像素,该程序将在输入图片的时候,就实现了全图的马赛克效果。而当鼠标划过的时候,程序只是将实现马赛克的图片的指定位置复制到显示的图像中。

效果类似于QQ截图中的马赛克。

#include  
#include  
#include  
#include  
 
using namespace cv; 
using namespace std; 
 
Mat inputImage; 
Mat inputImage_mosaic; 
Mat inputImage_clone; 
 
//马赛克的大小 
int neightbourhood = 20; 
 
//记录鼠标的状态,0为鼠标左键未按下或弹起,1为鼠标左键按下 
int mouseStatus = 0; 
 
void onMouse(int events, int x, int y, int flag, void* ustg); 
 
//创建马赛克图片 
void createMosaicImage(Mat inputMat, Mat& outputMat, int size); 
 
//设置马赛克区域 
void setMosaic(Mat& inputMat, Rect rect); 
 
int main(void){ 
 inputImage = imread("test2.jpg"); 
 inputImage_clone = inputImage.clone(); 
 createMosaicImage(inputImage, inputImage_mosaic, neightbourhood); 
 
 namedWindow("showImage", 0); 
 setMouseCallback("showImage", onMouse); 
 
 waitKey(); 
 return 0; 
} 
 
void createMosaicImage(Mat inputMat, Mat& outputMat, int size){ 
 RNG rng; 
 int height = inputMat.rows; 
 int width = inputMat.cols; 
 Mat padding; 
 Mat tempMat; 
 
 //为了方便后面的计算,将输入的图像大小扩充到宽高都是size的倍数 
 copyMakeBorder(inputMat, padding, 0, size - inputMat.rows % size, 0, size - inputMat.cols % size, BORDER_REPLICATE); 
 tempMat = padding.clone(); 
 
 for (int row = 0; row < padding.rows; row += size){ 
 for (int col = 0; col < padding.cols; col += size){ 
  int rand_x = rng.uniform(0, size); 
  int rand_y = rng.uniform(0, size); 
  Rect rect = Rect(col, row, size, size); 
  Mat roi = tempMat(rect); 
  Scalar color = Scalar(padding.at(row + rand_y, col + rand_x)[0], \ 
  padding.at(row + rand_y, col + rand_x)[1], \ 
  padding.at(row + rand_y, col + rand_x)[2]); 
  roi.setTo(color); 
 } 
 } 
 outputMat = tempMat(Rect(0, 0, width, height)).clone(); 
} 
 
void setMosaic(Mat& inputMat, Rect rect){ 
 Mat roi = inputMat(rect); 
 Mat tempRoi = inputImage_mosaic(rect); 
 tempRoi.copyTo(roi); 
} 
 
void onMouse(int events, int x, int y, int flag, void* ustg){ 
 
 //当鼠标移除图片区域的时候,不做操作 
 if (x < 0 || x > inputImage.cols || y < 0 || y > inputImage.rows){ 
 return; 
 } 
 
 //马赛克块的位置信息 
 int x_left, x_right, y_top, y_bottom; 
 x - neightbourhood <= 0 ? x_left = 0 : x_left = x - neightbourhood; 
 x + neightbourhood > inputImage.cols ? x_right = inputImage.cols: x_right = x + neightbourhood; 
 y - neightbourhood <= 0 ? y_top = 0 : y_top = y - neightbourhood; 
 y + neightbourhood > inputImage.rows ? y_bottom = inputImage.rows: y_bottom = y + neightbourhood; 
 
 if (events == CV_EVENT_LBUTTONDOWN){ 
 mouseStatus = 1; 
 setMosaic(inputImage_clone, Rect(x_left, y_top, x_right - x_left, y_bottom - y_top)); 
 } 
 else if (events == CV_EVENT_MOUSEMOVE){ 
 if (mouseStatus == 1){ 
  setMosaic(inputImage_clone, Rect(x_left, y_top, x_right - x_left, y_bottom - y_top)); 
 } 
 else{ 
  //nothing 
 } 
 } 
 else if (events == CV_EVENT_LBUTTONUP){ 
 mouseStatus = 0; 
 } 
 else { 
 //cout << "nothing" << endl; 
 } 
 imshow("showImage", inputImage_clone); 
}

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享文章:OpenCV实现马赛克功能-创新互联
URL标题:http://lszwz.com/article/dpjjpc.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款