使用OpenCV怎么實(shí)現(xiàn)馬賽克和毛玻璃濾鏡特效?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
一、馬賽克效果
馬賽克的實(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(); }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)建站的支持。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站jinyejixie.com,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、建站服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。
分享標(biāo)題:使用OpenCV怎么實(shí)現(xiàn)馬賽克和毛玻璃濾鏡特效-創(chuàng)新互聯(lián)
本文地址:http://jinyejixie.com/article32/psgpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、服務(wù)器托管、品牌網(wǎng)站建設(shè)、ChatGPT、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容