国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

OpenCV實現馬賽克功能

2020-05-23 13:31:17
字體:
來源:轉載
供稿:網友

本文實例為大家分享了OpenCV實現馬賽克功能的具體代碼,供大家參考,具體內容如下

實現用按下鼠標左鍵拖動時,在鼠標經過的路徑上打上馬賽克。

馬賽克的原理是將圖像中選中區域的像素用這個選中區域中的某一像素覆蓋。

為了不讓鼠標重復經過圖像中同一個的時候,選取不一樣的像素,該程序將在輸入圖片的時候,就實現了全圖的馬賽克效果。而當鼠標劃過的時候,程序只是將實現馬賽克的圖片的指定位置復制到顯示的圖像中。

效果類似于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;  //記錄鼠標的狀態,0為鼠標左鍵未按下或彈起,1為鼠標左鍵按下 int mouseStatus = 0;  void onMouse(int events, int x, int y, int flag, void* ustg);  //創建馬賽克圖片 void createMosaicImage(Mat inputMat, Mat& outputMat, int size);  //設置馬賽克區域 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;   //為了方便后面的計算,將輸入的圖像大小擴充到寬高都是size的倍數  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){   //當鼠標移除圖片區域的時候,不做操作  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); } 

效果圖

OpenCV,馬賽克

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阿拉善左旗| 云梦县| 全州县| 湘乡市| 独山县| 永春县| 那坡县| 望奎县| 津市市| 绵阳市| 小金县| 贞丰县| 遂川县| 郯城县| 平邑县| 密山市| 垫江县| 明水县| 阿克苏市| 万盛区| 阜宁县| 同德县| 绥芬河市| 泾阳县| 河北区| 含山县| 江津市| 涪陵区| 潮安县| 荆门市| 高密市| 青岛市| 德惠市| 江西省| 右玉县| 会同县| 达州市| 罗定市| 巴东县| 河南省| 庐江县|