OpenCV实现马赛克功能

所属分类: 软件编程 / C 语言 阅读数: 47
收藏 0 赞 0 分享

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

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

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

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

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

#include <opencv2\core\core.hpp> 
#include <opencv2\highgui\highgui.hpp> 
#include <opencv2\imgproc\imgproc.hpp> 
#include <iostream> 
 
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<Vec3b>(row + rand_y, col + rand_x)[0], \ 
  padding.at<Vec3b>(row + rand_y, col + rand_x)[1], \ 
  padding.at<Vec3b>(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); 
} 

效果图

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

C语言非递归后序遍历二叉树

这篇文章主要为大家详细介绍了C语言非递归后序遍历二叉树,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

C语言单链表实现多项式相加

这篇文章主要为大家详细介绍了C语言单链表实现多项式相加,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

C语言二叉排序(搜索)树实例

这篇文章主要为大家详细介绍了C语言二叉排序树实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

NDK 数据结构之队列与栈等的实现

这篇文章主要介绍了NDK 数据结构之队列与栈等的实现的相关资料,希望通过本文大家能理解掌握这部分内容,需要的朋友可以参考下
收藏 0 赞 0 分享

C/C++经典实例之模拟计算器示例代码

最近在看到的一个需求,本以为比较简单,但花了不少时间,所以下面这篇文章主要给大家介绍了关于C/C++经典实例之模拟计算器的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

C语言中的getchar和putchar的使用方法

这篇文章主要介绍了C语言中的getchar和putchar的使用方法的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
收藏 0 赞 0 分享

C++实现洗牌发牌排序功能的示例代码

本篇文章主要介绍了C++实现洗牌发牌排序功能的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

C++计算图任意两点间的所有路径

这篇文章主要为大家详细介绍了C++求图任意两点间的所有路径 ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

zlib库压缩和解压字符串STL string的实例详解

这篇文章主要介绍了zlib库压缩和解压字符串STL string的实例详解的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
收藏 0 赞 0 分享

C/C++ 获取Windows系统的位数32位或64位的实现代码

这篇文章主要介绍了C/C++ 获取Windows系统的位数32位或64位的实现代码的相关资料,希望通过本文能帮助到大家,让大家实现这样的功能,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多