雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

OpenCV實(shí)現(xiàn)馬賽克和毛玻璃濾鏡效果

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語言|點(diǎn)擊:

本文為大家分享了OpenCV實(shí)現(xiàn)馬賽克和毛玻璃濾鏡的具體代碼,供大家參考,具體內(nèi)容如下

一、馬賽克效果

馬賽克的實(shí)現(xiàn)原理是把圖像上某個(gè)像素點(diǎn)一定范圍鄰域內(nèi)的所有點(diǎn)用鄰域內(nèi)隨機(jī)選取的一個(gè)像素點(diǎn)的顏色代替,這樣可以模糊細(xì)節(jié),但是可以保留大體的輪廓。

以下OpenCV程序?qū)崿F(xiàn)馬賽克效果,通過鼠標(biāo)左鍵在圖像上劃定馬賽克的矩形框。

#include <core\core.hpp> 
#include <highgui\highgui.hpp> 
 
using namespace cv; 
 
Mat imageSourceCopy; //原始圖像 
Mat imageSource; //原始圖像拷貝 
int neightbourHood = 9; //馬賽克上每個(gè)方框的像素大小 
 
RNG rng; 
int randomNum; //鄰域內(nèi)隨機(jī)值 
 
Point ptL; //左鍵按下時(shí)坐標(biāo) 
Point ptR; //右鍵按下時(shí)坐標(biāo) 
 
//鼠標(biāo)回掉函數(shù) 
void onMouse(int event, int x, int y, int flag, void *ustg); 
 
int main() 
{ 
  imageSourceCopy = imread("Test.jpg"); 
  imageSource = imageSourceCopy.clone(); 
  //imshow("馬賽克", imageSourceCopy); 
  namedWindow("馬賽克"); 
  setMouseCallback("馬賽克", onMouse); 
  waitKey(); 
} 
 
void onMouse(int event, int x, int y, int flag, void *ustg) 
{ 
  if (event == CV_EVENT_LBUTTONDOWN) 
  { 
    ptL = Point(x, y); 
  } 
  if (event == CV_EVENT_LBUTTONUP) 
  { 
    //對鼠標(biāo)畫出的矩形框超出圖像范圍做處理,否則會越界崩潰 
    x > imageSource.cols - 2 * neightbourHood ? x = imageSource.cols - 2 * neightbourHood : x = x; 
    y > imageSource.rows - 2 * neightbourHood ? y = imageSource.rows - 2 * neightbourHood : y = y; 
 
    //對鼠標(biāo)從右下往右上畫矩形框的情況做處理 
    ptR = Point(x, y); 
    Point pt = ptR; 
    ptR.x < ptL.x ? ptR = ptL, ptL = pt : ptR = ptR; 
    for (int i = 0; i < ptR.y - ptL.y; i += neightbourHood) 
    { 
      for (int j = 0; j < ptR.x - ptL.x; j += neightbourHood) 
      { 
        randomNum = rng.uniform(-neightbourHood / 2, neightbourHood / 2); 
        Rect rect = Rect(j + neightbourHood + ptL.x, i + neightbourHood + ptL.y, neightbourHood, neightbourHood); 
        Mat roi = imageSourceCopy(rect); 
        Scalar sca = Scalar( 
          imageSource.at<Vec3b>(i + randomNum + ptL.y, j + randomNum + ptL.x)[0], 
          imageSource.at<Vec3b>(i + randomNum + ptL.y, j + randomNum + ptL.x)[1], 
          imageSource.at<Vec3b>(i + randomNum + ptL.y, j + randomNum + ptL.x)[2]); 
        Mat roiCopy = Mat(rect.size(), CV_8UC3, sca); 
        roiCopy.copyTo(roi); 
      } 
    } 
  } 
  imshow("馬賽克", imageSourceCopy); 
  waitKey(); 
} 

可以通過改變程序中neightbourHood參數(shù)的大小調(diào)整小矩形快的大小,實(shí)現(xiàn)效果:

二、毛玻璃效果

毛玻璃效果的實(shí)現(xiàn)通過用像素點(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 main() 
{ 
  Mat imageSource = imread("Test.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(); 
} 

實(shí)現(xiàn)效果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。 

上一篇:C語言手把手教你實(shí)現(xiàn)貪吃蛇AI(中)

欄    目:C語言

下一篇:C語言貪吃蛇經(jīng)典小游戲

本文標(biāo)題:OpenCV實(shí)現(xiàn)馬賽克和毛玻璃濾鏡效果

本文地址:http://www.jygsgssxh.com/a1/Cyuyan/923.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有