????????? 均值濾波是典型的線性濾波算法,它是指在圖像上對目標像素給一個模板,該模板包括了其周圍的臨近像素(以目標象素為中心的周圍8個像素,構成一個濾波模板,即去掉目標像素本身),再用模板中的全體像素的平均值來代替原來像素值。均值濾波本身存在著固有的缺陷,即它不能很好地保護圖像細節,在圖像去噪的同時也破壞了圖像的細節部分,從而使圖像變得模糊,不能很好地去除噪聲點。
????????? 圖像平滑用于去除圖像中的噪聲。高斯平滑,就是將每個像素的灰度值用其領域的加權平均值代替。該算法簡單,能夠有效去除高斯噪聲。
?????? 高斯平滑模板:?
??????????

高斯:

均值:

中值:

??????????

//高斯平滑 中值濾波 均值濾波?? ?
#include<cv.h>?? ?
#include<highgui.h> ?// 高斯平滑 ?
// 1. pImageData?? 圖像數據 ?
// 2. nWidth?????? 圖像寬度 ?
// 3. nHeight????? 圖像高度 ?
// 4. nWidthStep?? 圖像行大小
bool SmoothGauss(unsigned char *pImageData, int nWidth, int nHeight, int nWidthStep)
{int i = 0;int j = 0;int nValue = 0;unsigned char *pLine[3] = { NULL, NULL, NULL };int nTemplate[9] ={1, 2, 1,2, 4, 2,1, 2, 1};for (j = 1; j < nHeight - 1; j++){pLine[0] = pImageData + nWidthStep * (j - 1);? //對應3行3列高斯模板矩陣中的 3行列。pLine[1] = pImageData + nWidthStep * j;pLine[2] = pImageData + nWidthStep * (j + 1);for (i = 1; i < nWidth - 1; i++){nValue =(pLine[0][i - 1] * nTemplate[0] +??? //對應3行3列矩陣中的各個點。pLine[0][i] * nTemplate[1] +pLine[0][i + 1] * nTemplate[2] +pLine[1][i - 1] * nTemplate[3] +pLine[1][i] * nTemplate[4] +pLine[1][i + 1] * nTemplate[5] +pLine[2][i - 1] * nTemplate[6] +pLine[2][i] * nTemplate[7] +pLine[2][i + 1] * nTemplate[8]) / 16;pLine[0][i - 1] = (unsigned char)nValue;}}return true;
}int main()
{IplImage * image, *image2, *image3,*image4;image = cvLoadImage("C:\\Users\\lyb\\Documents\\Visual Studio 2013\\Projects\\ConsoleApplication 14_11_4\\11.bmp", 0);//以灰度圖像的形式讀入圖片?? ?cvNamedWindow("image_first-hand", CV_WINDOW_AUTOSIZE);cvNamedWindow("image_jun_zhi", CV_WINDOW_AUTOSIZE);cvNamedWindow("image_zhong_zhi", CV_WINDOW_AUTOSIZE);cvNamedWindow("image_gauss", CV_WINDOW_AUTOSIZE);//cvSaveImage("E:\\image\\moon.jpg",image,0);?? ?cvShowImage("image_first-hand", image);//cvWaitKey(0);?? ?unsigned char * ptr, *dst;int i, j, m, n, sum, temp, r, s;image2 = cvCreateImage(cvGetSize(image), image->depth, 1);image3 = cvCreateImage(cvGetSize(image), image->depth, 1);image4 = cvLoadImage("C:\\Users\\lyb\\Documents\\Visual Studio 2013\\Projects\\ConsoleApplication 14_11_4\\11.bmp", 0);//以灰度圖像的形式讀入圖片 ?//image4 = cvCreateImage(cvGetSize(image), image->depth, 1);//模板1 均值??? ?int tem[9] = { 1, 1, 1, 1, 1, 1, 1, 1, 1 };//也可以使用改進的高斯模板,但是效果相近??? ?int tem2[9] = { 0 };//獲取中值時用于排序? ?//高斯濾波unsigned char *pImageData1 = (unsigned char *)image4->imageData;int nWidth1 = image4->width;int nHeight1 = image4->height;int nWidthStep1 = image4->widthStep;if (SmoothGauss( pImageData1,nWidth1,nHeight1,nWidthStep1)==true)printf("%15s", "return"); //運行結果:return;//均值濾波3*3模板的均值?? ?for (i = 0; i < image->height; i++){for (j = 0; j< image->width; j++){//邊界處理?? ?if (i == 0 || i == image->height || j == 0 || j == image->width){ptr = (unsigned char *)image->imageData + i*image->widthStep + j;dst = (unsigned char *)image2->imageData + i*image2->widthStep + j;*dst = *ptr; //邊界值賦予源圖像的值?? ?}else {sum = 0;for (m = -1; m <= 1; m++){for (n = -1; n <= 1; n++){ptr = (unsigned char *)image->imageData + (i + m)*image->widthStep + j + n;sum += (*ptr) * tem[3 * (m + 1) + n + 1];}}dst = (unsigned char *)image2->imageData + i *image2->widthStep + j;*dst = (unsigned char)((sum + 4) / 9);//賦新值,四舍五入?? ?}}}//中值濾波 在去除噪聲的同時,圖像的模糊程度比較小,比均值濾波更加適合?? ?//沖擊噪聲或者稱為椒鹽噪聲?? ?for (i = 0; i < image->height; i++){for (j = 0; j< image->width; j++){//邊界處理?? ?if (i == 0 || i == image->height || j == 0 || j == image->width){ptr = (unsigned char *)image->imageData + i*image->widthStep + j;dst = (unsigned char *)image3->imageData + i*image3->widthStep + j;*dst = *ptr; //邊界值賦予源圖像的值?? ?}else {temp = 0;//將3*3模板覆蓋的值拷貝進數組,一邊查找中值?? ?for (m = -1; m <= 1; m++){for (n = -1; n <= 1; n++){ptr = (unsigned char *)image->imageData + (i + m)*image->widthStep + j + n;tem2[3 * (m + 1) + n + 1] = *ptr;//printf("%d",*ptr);?? ?}}//對數組進行冒泡排序?? ?for (r = 0; r <8; r++){for (s = 0; s< r - 1; s++){if (tem2[s] > tem2[s + 1]){temp = tem2[s];tem2[s] = tem2[s + 1];tem2[s + 1] = temp;}}}//printf("%d",tem2[4]);?? ?//對新圖賦予新值?? ?dst = (unsigned char *)image3->imageData + i *image3->widthStep + j;*dst = (unsigned char)(tem2[4]);//賦新值?? ?}}}cvShowImage("image_jun_zhi", image2);cvShowImage("image_zhong_zhi", image3);cvShowImage("image_gauss", image4);cvWaitKey(0);//cvSaveImage("E:\\image\\Dart2.bmp", image2, 0);//cvSaveImage("E:\\image\\Dart3.bmp", image3, 0);return 0;
}
原圖:高斯:
均值:
中值: