OpenCV實(shí)現(xiàn)馬賽克功能
本文實(shí)例為大家分享了OpenCV實(shí)現(xiàn)馬賽克功能的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)用按下鼠標(biāo)左鍵拖動時(shí),在鼠標(biāo)經(jīng)過的路徑上打上馬賽克。
馬賽克的原理是將圖像中選中區(qū)域的像素用這個(gè)選中區(qū)域中的某一像素覆蓋。
為了不讓鼠標(biāo)重復(fù)經(jīng)過圖像中同一個(gè)的時(shí)候,選取不一樣的像素,該程序?qū)⒃谳斎雸D片的時(shí)候,就實(shí)現(xiàn)了全圖的馬賽克效果。而當(dāng)鼠標(biāo)劃過的時(shí)候,程序只是將實(shí)現(xiàn)馬賽克的圖片的指定位置復(fù)制到顯示的圖像中。
效果類似于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; //記錄鼠標(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 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; //為了方便后面的計(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); }
效果圖
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
深度解析三個(gè)常見的C語言內(nèi)存函數(shù)
這篇文章主要深度解析了三個(gè)常見的C語言內(nèi)存函數(shù)memcpy,memmove,memcmp,所以本文將對memcpy,memmove,memcmp 三個(gè)函數(shù)進(jìn)行詳解和模擬實(shí)現(xiàn),需要的朋友可以參考下2023-07-07C語言學(xué)生成績管理系統(tǒng)設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了C語言學(xué)生成績管理系統(tǒng)設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01QT6中添加串口模塊SerialPort的實(shí)現(xiàn)
本文主要介紹了QT6中添加串口模塊SerialPort的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09LeetCode題解C++生成每種字符都是奇數(shù)個(gè)的字符串
這篇文章主要為大家介紹了LeetCode題解C++生成每種字符都是奇數(shù)個(gè)的字符串示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10C語言數(shù)據(jù)類型枚舉enum全面詳解示例教程
生活中有很多地方會用到枚舉,比如一周有7天,可以一一枚舉;性別有男、女...等等都可以可以一一枚舉,今天來和筆者一起學(xué)習(xí)一下c語言枚舉吧2021-10-10C語言數(shù)據(jù)結(jié)構(gòu)之?dāng)U展字符詳解
掌握C語言數(shù)據(jù)結(jié)構(gòu)的關(guān)鍵在于理解其核心概念,擴(kuò)展字符作為其中的重要一環(huán),對于編程人員來說至關(guān)重要,本指南將為您深入剖析擴(kuò)展字符的相關(guān)知識,帶您輕松掌握C語言數(shù)據(jù)結(jié)構(gòu),讓我們一起探索這個(gè)令人著迷的領(lǐng)域吧!2024-03-03QT已有項(xiàng)目導(dǎo)入工程時(shí)注意事項(xiàng)圖文詳解
QT開發(fā)這幾年大大小小項(xiàng)目做了不少,花了點(diǎn)時(shí)間對知識點(diǎn)總結(jié)整合了一部分,下面這篇文章主要給大家介紹了關(guān)于QT已有項(xiàng)目導(dǎo)入工程時(shí)注意事項(xiàng)的相關(guān)資料,需要的朋友可以參考下2023-11-11