OpenCV实现马赛克和毛玻璃滤镜效果
2024-08-31 21:33:10
一、马赛克效果
马赛克的实现原理是把图像上某个像素点一定范围邻域内的所有点用邻域内随机选取的一个像素点的颜色代替,这样可以模糊细节,但是可以保留大体的轮廓。
以下OpenCV程序实现马赛克效果,通过鼠标左键在图像上划定马赛克的矩形框。
#include <core\core.hpp>
#include <highgui\highgui.hpp>
using namespace cv;
Mat imageSourceCopy; //原始图像
Mat imageSource; //原始图像拷贝
int neightbourHood = 9; //马赛克上每个方框的像素大小
RNG rng;
int randomNum; //邻域内随机值
Point ptL; //左键按下时坐标
Point ptR; //右键按下时坐标
//鼠标回掉函数
void onMouse(int event, int x, int y, int flag, void *ustg);
int main()
{
imageSourceCopy = imread("Test.jpg");
imageSource = imageSourceCopy.clone();
//imshow("马赛克", imageSourceCopy);
namedWindow("马赛克");
setMouseCallback("马赛克", onMouse);
waitKey();
}
void onMouse(int event, int x, int y, int flag, void *ustg)
{
if (event == CV_EVENT_LBUTTONDOWN)
{
ptL = Point(x, y);
}
if (event == CV_EVENT_LBUTTONUP)
{
//对鼠标画出的矩形框超出图像范围做处理,否则会越界崩溃
x > imageSource.cols - 2 * neightbourHood ? x = imageSource.cols - 2 * neightbourHood : x = x;
y > imageSource.rows - 2 * neightbourHood ? y = imageSource.rows - 2 * neightbourHood : y = y;
//对鼠标从右下往右上画矩形框的情况做处理
ptR = Point(x, y);
Point pt = ptR;
ptR.x < ptL.x ? ptR = ptL, ptL = pt : ptR = ptR;
for (int i = 0; i < ptR.y - ptL.y; i += neightbourHood)
{
for (int j = 0; j < ptR.x - ptL.x; j += neightbourHood)
{
randomNum = rng.uniform(-neightbourHood / 2, neightbourHood / 2);
Rect rect = Rect(j + neightbourHood + ptL.x, i + neightbourHood + ptL.y, neightbourHood, neightbourHood);
Mat roi = imageSourceCopy(rect);
Scalar sca = Scalar(
imageSource.at<Vec3b>(i + randomNum + ptL.y, j + randomNum + ptL.x)[0],
imageSource.at<Vec3b>(i + randomNum + ptL.y, j + randomNum + ptL.x)[1],
imageSource.at<Vec3b>(i + randomNum + ptL.y, j + randomNum + ptL.x)[2]);
Mat roiCopy = Mat(rect.size(), CV_8UC3, sca);
roiCopy.copyTo(roi);
}
}
}
imshow("马赛克", imageSourceCopy);
waitKey();
}
可以通过改变程序中neightbourHood参数的大小调整小矩形快的大小,实现效果:
二、毛玻璃效果
毛玻璃效果的实现通过用像素点邻域内随机一个像素点的颜色替代当前像素点的颜色实现。
#include <core\core.hpp>
#include <highgui\highgui.hpp>
using namespace cv;
int main()
{
Mat imageSource = imread("Test.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();
}
实现效果:
最新文章
- CentOS7下安装chrome浏览器
- Android工程文件下assets文件夹与res文件夹的区别
- SpringMVC核心——参数获取与Servlet资源获取问题
- select_tag in rails about selected not change and onchange()
- springMVC controller间跳转、重定向、传参
- kail ip配置
- python 根据对象和方法名,返回提供这个方法的定义的类
- [MVCSharp]MVC# Overview概述
- plsql快速选中一行的快捷键
- 顺丰快递单号查询api对接(全代码)
- 使用CPU探查器优化XAML程序
- HDU 2295.Radar (DLX重复覆盖)
- Struts2中OGNL
- Ext:添加进度条
- javascript入门篇(四)
- SVN问题解决--Attempted to lock an already-locked dir
- ARMV8体系结构简介
- [物理学与PDEs]第1章第9节 Darwin 模型 9.3 Darwin 模型
- default配置页面一级菜单用于进入二级菜单
- MyBatis-resultType 几种返回类型
热门文章
- HIVE快速入门 分类: B4_HIVE 2015-06-06 11:27 59人阅读 评论(0) 收藏
- [Ionic2] Device Interaction in an Ionic App with Cordova Plugins
- icvPrecalculate
- (五)RabbitMQ消息队列-安装amqp扩展并订阅/发布Demo(PHP版)
- deep learning实践经验总结
- Swift开发教程--关于Existing instance variable &;#39;_delegate&;#39;...的解决的方法
- .NET-架构优化实战-前端优化
- 【33.33%】【codeforces 552B】Vanya and Books
- 【转】dbx用法讲解
- 三天打渔,俩天晒网(C++实现)