OpenCV實現(xiàn)馬賽克和毛玻璃濾鏡特效
本文實例為大家分享了OpenCV實現(xiàn)馬賽克和毛玻璃濾鏡特效的具體代碼,供大家參考,具體內(nèi)容如下
一、馬賽克效果
馬賽克的實現(xiàn)原理是把圖像上某個像素點一定范圍鄰域內(nèi)的所有點用鄰域內(nèi)隨機(jī)選取的一個像素點的顏色代替,這樣可以模糊細(xì)節(jié),但是可以保留大體的輪廓。
以下OpenCV程序?qū)崿F(xiàn)馬賽克效果,通過鼠標(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;
//為了方便后面的計算,將輸入的圖像大小擴(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ū)域的時候,不做操作
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);
}
//-----開始------
void COpenCVLearningDlg::OnBnClickedStartButton()
{
mainFun();
}
效果:

二、毛玻璃效果
毛玻璃效果的實現(xiàn)通過用像素點鄰域內(nèi)隨機(jī)一個像素點的顏色替代當(dāng)前像素點的顏色實現(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;
}
//-----開始------
void COpenCVLearningDlg::OnBnClickedStartButton()
{
mainFun();
}
結(jié)果:

參考鏈接:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于OpenCV實現(xiàn)車道線檢測(自動駕駛 機(jī)器視覺)
無人駕駛技術(shù)是機(jī)器學(xué)習(xí)為主的一門前沿領(lǐng)域,在無人駕駛領(lǐng)域中機(jī)器學(xué)習(xí)的各種算法隨處可見,本文將為大家介紹無人駕駛技術(shù)中的車道線檢測,感興趣的小伙伴可以了解一下2021-11-11
C/C++自主分配出現(xiàn)double free or corruption問題解決
這篇文章主要為大家介紹了C/C++出現(xiàn)double free or corruption問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04

