OpenCV實(shí)現(xiàn)馬賽克和毛玻璃濾鏡效果
本文為大家分享了OpenCV實(shí)現(xiàn)馬賽克和毛玻璃濾鏡的具體代碼,供大家參考,具體內(nèi)容如下
一、馬賽克效果
馬賽克的實(shí)現(xiàn)原理是把圖像上某個(gè)像素點(diǎn)一定范圍鄰域內(nèi)的所有點(diǎn)用鄰域內(nèi)隨機(jī)選取的一個(gè)像素點(diǎn)的顏色代替,這樣可以模糊細(xì)節(jié),但是可以保留大體的輪廓。
以下OpenCV程序?qū)崿F(xiàn)馬賽克效果,通過鼠標(biāo)左鍵在圖像上劃定馬賽克的矩形框。
#include <core\core.hpp>
#include <highgui\highgui.hpp>
using namespace cv;
Mat imageSourceCopy; //原始圖像
Mat imageSource; //原始圖像拷貝
int neightbourHood = 9; //馬賽克上每個(gè)方框的像素大小
RNG rng;
int randomNum; //鄰域內(nèi)隨機(jī)值
Point ptL; //左鍵按下時(shí)坐標(biāo)
Point ptR; //右鍵按下時(shí)坐標(biāo)
//鼠標(biāo)回掉函數(shù)
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)
{
//對鼠標(biāo)畫出的矩形框超出圖像范圍做處理,否則會越界崩潰
x > imageSource.cols - 2 * neightbourHood ? x = imageSource.cols - 2 * neightbourHood : x = x;
y > imageSource.rows - 2 * neightbourHood ? y = imageSource.rows - 2 * neightbourHood : y = y;
//對鼠標(biāo)從右下往右上畫矩形框的情況做處理
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參數(shù)的大小調(diào)整小矩形快的大小,實(shí)現(xiàn)效果:
二、毛玻璃效果
毛玻璃效果的實(shí)現(xiàn)通過用像素點(diǎn)鄰域內(nèi)隨機(jī)一個(gè)像素點(diǎn)的顏色替代當(dāng)前像素點(diǎn)的顏色實(shí)現(xiàn)。
#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();
}
實(shí)現(xiàn)效果:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:C語言手把手教你實(shí)現(xiàn)貪吃蛇AI(中)
欄 目:C語言
本文標(biāo)題:OpenCV實(shí)現(xiàn)馬賽克和毛玻璃濾鏡效果
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/923.html
您可能感興趣的文章
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)-用棧實(shí)現(xiàn)表達(dá)式求值的方法詳解
- 01-10使用OpenGL實(shí)現(xiàn)3D立體顯示的程序代碼
- 01-10求斐波那契(Fibonacci)數(shù)列通項(xiàng)的七種實(shí)現(xiàn)方法
- 01-10C語言 解決不用+、-、&#215;、&#247;數(shù)字運(yùn)算符做加法
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類算法
- 01-10深入全排列算法及其實(shí)現(xiàn)方法
- 01-10全排列算法的非遞歸實(shí)現(xiàn)與遞歸實(shí)現(xiàn)的方法(C++)
- 01-10用C語言實(shí)現(xiàn)單鏈表的各種操作(一)
- 01-10用C語言實(shí)現(xiàn)單鏈表的各種操作(二)


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10delphi制作wav文件的方法
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文


