OpenCV實(shí)現(xiàn)馬賽克和毛玻璃濾鏡特效
一、馬賽克效果
馬賽克的實(shí)現(xiàn)原理是把圖像上某個(gè)像素點(diǎn)一定范圍鄰域內(nèi)的所有點(diǎn)用鄰域內(nèi)隨機(jī)選取的一個(gè)像素點(diǎn)的顏色代替,這樣可以模糊細(xì)節(jié),但是可以保留大體的輪廓。
以下OpenCV程序?qū)崿F(xiàn)馬賽克效果,通過(guò)鼠標(biāo)左鍵在圖像上劃定馬賽克的矩形框。
代碼:
#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; //記錄鼠標(biāo)的狀態(tài),0為鼠標(biāo)左鍵未按下或彈起,1為鼠標(biāo)左鍵按下 int mouseStatus = 0; void onMouse(int events, int x, int y, int flag, void* ustg); //創(chuàng)建馬賽克圖片 void createMosaicImage(Mat inputMat, Mat& outputMat, int size); //設(shè)置馬賽克區(qū)域 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; //為了方便后面的計(jì)算,將輸入的圖像大小擴(kuò)充到寬高都是size的倍數(shù) 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) { //當(dāng)鼠標(biāo)移除圖片區(qū)域的時(shí)候,不做操作 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); } //-----開(kāi)始------ void COpenCVLearningDlg::OnBnClickedStartButton() { mainFun(); }
效果:
二、毛玻璃效果
毛玻璃效果的實(shí)現(xiàn)通過(guò)用像素點(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 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; } //-----開(kāi)始------ void COpenCVLearningDlg::OnBnClickedStartButton() { mainFun(); }
結(jié)果:
參考鏈接:
OpenCV實(shí)現(xiàn)馬賽克和毛玻璃濾鏡效果
OpenCV實(shí)現(xiàn)馬賽克功能
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:C語(yǔ)言
下一篇:從零開(kāi)始的Socket編程學(xué)習(xí)
本文標(biāo)題:OpenCV實(shí)現(xiàn)馬賽克和毛玻璃濾鏡特效
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/300.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語(yǔ)言 解決不用+、-、&#215;、&#247;數(shù)字運(yùn)算符做加法
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類(lèi)算法
- 01-10深入全排列算法及其實(shí)現(xiàn)方法
- 01-10全排列算法的非遞歸實(shí)現(xiàn)與遞歸實(shí)現(xiàn)的方法(C++)
- 01-10用C語(yǔ)言實(shí)現(xiàn)單鏈表的各種操作(一)
- 01-10用C語(yǔ)言實(shí)現(xiàn)單鏈表的各種操作(二)


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
- 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
隨機(jī)閱讀
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文