OpenCV_17马赛克和毛玻璃滤镜效果

2024-03-03 18:38

本文主要是介绍OpenCV_17马赛克和毛玻璃滤镜效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、马赛克效果

       马赛克的实现原理是把图像上某个像素点一定范围邻域内的所有点用邻域内随机选取的一个像素点的颜色代替,这样可以模糊细节,但是可以保留大体的轮廓。

       以下OpenCV程序实现马赛克效果,通过鼠标左键在图像上划定马赛克的矩形框。

代码:

#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 mainFun(void) {inputImage = imread("D:\\test\\12.jpg");inputImage_clone = inputImage.clone();createMosaicImage(inputImage, inputImage_mosaic, neightbourhood);namedWindow("showImage", WINDOW_AUTOSIZE);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);
}//-----开始------
void COpenCVLearningDlg::OnBnClickedStartButton()
{mainFun();
}

效果:

 

二、毛玻璃效果

      毛玻璃效果的实现通过用像素点邻域内随机一个像素点的颜色替代当前像素点的颜色实现。

代码:


#include <core\core.hpp>
#include <highgui\highgui.hpp>using namespace cv;int mainFun()
{Mat imageSource = imread("D:\\test\\12.jpg");Mat imageResult = imageSource.clone();RNG rng;int randomNum;int Number = 5;for (int i = 0; i < imageSource.rows - Number; i++)for (int j = 0; j < imageSource.cols - Number; j++){randomNum = rng.uniform(0, Number);imageResult.at<Vec3b>(i, j)[0] = imageSource.at<Vec3b>(i + randomNum, j + randomNum)[0];imageResult.at<Vec3b>(i, j)[1] = imageSource.at<Vec3b>(i + randomNum, j + randomNum)[1];imageResult.at<Vec3b>(i, j)[2] = imageSource.at<Vec3b>(i + randomNum, j + randomNum)[2];}imshow("毛玻璃效果", imageResult);waitKey();return 0;
}//-----开始------
void COpenCVLearningDlg::OnBnClickedStartButton()
{mainFun();
}

结果:

 

欢迎扫码关注我的微信公众号

原文地址:https://blog.csdn.net/u013167052/article/details/54891479(马赛克)

原文地址:https://blog.csdn.net/dcrmg/article/details/53749976(毛玻璃)此篇马赛克的代码有bug

 

这篇关于OpenCV_17马赛克和毛玻璃滤镜效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/770532

相关文章

vcpkg安装opencv中的特殊问题记录(无法找到opencv_corexd.dll)

我是按照网上的vcpkg安装opencv方法进行的(比如这篇:从0开始在visual studio上安装opencv(超详细,针对小白)),但是中间出现了一些别人没有遇到的问题,虽然原因没有找到,但是本人给出一些暂时的解决办法: 问题1: 我在安装库命令行使用的是 .\vcpkg.exe install opencv 我的电脑是x64,vcpkg在这条命令后默认下载的也是opencv2:x6

17.用300行代码手写初体验Spring V1.0版本

1.1.课程目标 1、了解看源码最有效的方式,先猜测后验证,不要一开始就去调试代码。 2、浓缩就是精华,用 300行最简洁的代码 提炼Spring的基本设计思想。 3、掌握Spring框架的基本脉络。 1.2.内容定位 1、 具有1年以上的SpringMVC使用经验。 2、 希望深入了解Spring源码的人群,对 Spring有一个整体的宏观感受。 3、 全程手写实现SpringM

以canvas方式绘制粒子背景效果,感觉还可以

这个是看到项目中别人写好的,感觉这种写法效果还可以,就存留记录下 就是这种的背景效果。如果想改背景颜色可以通过canvas.js文件中的fillStyle值改。 附上demo下载地址。 https://download.csdn.net/download/u012138137/11249872

echarts省份标注加散点效果

这个是安徽的效果图,鼠标移到红色标注或者对应的市区位置都会显示对应的数值。 先直接上代码: import anhuiMapJson from './anhui.json'getCoords: function(city) {var res = [];if (city != null) {for (var c in this.cityMap.features) {if (this.cityMa

brew install opencv@2 时报错 Error: Can't create update lock in /usr/local/var/homebrew/locks!

解决方案,报错里已经说明了: 我的解决方案: sudo chown -R "$USER":admin /usr/local   stackoverflow上的答案 I was able to solve the problem by using chown on the folder: sudo chown -R "$USER":admin /usr/local Also you'

XMG 抽屉效果

1.比如说我创建了3个View -(void)viewDidLoad{  [ super viewDidLoad]; [self setUpChild] ;         UIPanGestureRecognizer *pan=[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];

《学习OpenCV》课后习题解答7

题目:(P105) 创建一个结构,结构中包含一个整数,一个CvPoint和一个 CvRect;称结构体为“my_struct”。 a. 写两个函数:void Write_my_strct(CvFileStorage* fs, const char * name, my_struct* ms) 和 void read_my_struct(CvFileStorage* fs, CvFileNode

OpenCV中的按钮问题

在HighGUI中,没有显示提供任何形式的按钮。一般有两种方法替代: 1.用只有两个状态的滑动条来替代按钮。开关(switch)事实上就是只有两个状态的滑动条,这两个状态是on和off。然后通过回调函数来实现相关的功能。 实例源码(使用滑动条实现一个开关功能) #include<cv.h>#include<highgui.h>int g_switch_value = 0;void swit

《学习OpenCV》课后习题解答6

题目:(P104) 使用cvCmp()创建一个掩码。加载一个真实的图像。使用cvsplit()将图像分割成红,绿,蓝三个单通道图像。 a.找到并显示绿图。 b.克隆这个绿图两次(分别命名为clone1和clone2)。 c.求出这个绿色平面的最大值和最小值。 d.将clone1的所有元素赋值为theash=(unsigned char)((最大值-最小值)/2.0)。 e.将clone

《学习OpenCV》课后习题解答5

题目:(P104) 为一个图像创建多个图像头。读取一个大小至少为100*100的图像。另创建两个图像头并设置它们的origion,depth,nChannels和widthStep属性同之前读取的图像一样。在新的图像头中,设置宽度为20,高度为30.最后,将imageData指针分别指向像素(5,10)和(50,60)像素位置。传递这两个新的图像头给cvNot()。最后显示最初读取的图像,在那个