概念:
什麼是均值濾波?我們先從概念和原理上來理解什麼是均值濾波。均值濾波是一種典型的線性濾波算法,主要是利用像素點鄰域的像素值相加然後取平均值即爲該像素點的新的像素值。
openv 提供方法blur
C++: void blur(
InputArray src, // 輸入圖像
OutputArray dst, // 輸出圖像
Size ksize, // 內核大小, 一般這樣寫Size( w,h )來表示內核的大小( 其中,w 爲像素寬度, h爲像素高度)。Size(3,3)就表示3x3的核大小,Size(5,5)就表示5x5的核大小
Point anchor=Point(-1,-1), // 表示錨點(即被平滑的那個點), 指定計算時核與原圖像的對齊方式,默認爲Point(-1,-1), 表示kernel 的中心位置開始計算,一般我們沒有特殊需要不用修改此參數
int borderType=BORDER_DEFAULT ) // 用於推斷圖像外部像素的某種邊界模式,有默認值BORDER_DEFAULT
// image_correct.cpp : 定義控制檯應用程序的入口點。
//
#include "stdafx.h"
#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Mat SrcMat = imread("F:\\build\\test_app\\correct.png"); //IMREAD_GRAYSCALE:原圖做爲灰度圖加載
if(SrcMat.empty()) {
cout << "SrcMat.empty" << endl;
return -1;
}
Mat grayImg;
cvtColor(SrcMat, grayImg, CV_BGR2GRAY); // 色彩轉換空間
// imshow("image input", SrcMat);
Mat blurImg;
blur(grayImg, blurImg,Size(5, 5)); // 均值濾波
imshow("image input", blurImg);
waitKey(0);
return 0;
}
原圖和濾波後圖像效果對比: