日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

均值滤波 中值滤波 高斯平滑滤波

發(fā)布時(shí)間:2023/12/10 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 均值滤波 中值滤波 高斯平滑滤波 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

????????? 均值濾波是典型的線性濾波算法,它是指在圖像上對(duì)目標(biāo)像素給一個(gè)模板,該模板包括了其周圍的臨近像素(以目標(biāo)象素為中心的周圍8個(gè)像素,構(gòu)成一個(gè)濾波模板,即去掉目標(biāo)像素本身),再用模板中的全體像素的平均值來代替原來像素值。均值濾波本身存在著固有的缺陷,即它不能很好地保護(hù)圖像細(xì)節(jié),在圖像去噪的同時(shí)也破壞了圖像的細(xì)節(jié)部分,從而使圖像變得模糊,不能很好地去除噪聲點(diǎn)。


????????? 圖像平滑用于去除圖像中的噪聲。高斯平滑,就是將每個(gè)像素的灰度值用其領(lǐng)域的加權(quán)平均值代替。該算法簡(jiǎn)單,能夠有效去除高斯噪聲。

?????? 高斯平滑模板:?
??????????
//高斯平滑 中值濾波 均值濾波?? ? #include<cv.h>?? ? #include<highgui.h> ?// 高斯平滑 ? // 1. pImageData?? 圖像數(shù)據(jù) ? // 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);? //對(duì)應(yīng)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] +??? //對(duì)應(yīng)3行3列矩陣中的各個(gè)點(diǎn)。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 };//也可以使用改進(jìn)的高斯模板,但是效果相近??? ?int tem2[9] = { 0 };//獲取中值時(shí)用于排序? ?//高斯濾波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"); //運(yùn)行結(jié)果: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);//賦新值,四舍五入?? ?}}}//中值濾波 在去除噪聲的同時(shí),圖像的模糊程度比較小,比均值濾波更加適合?? ?//沖擊噪聲或者稱為椒鹽噪聲?? ?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模板覆蓋的值拷貝進(jìn)數(shù)組,一邊查找中值?? ?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);?? ?}}//對(duì)數(shù)組進(jìn)行冒泡排序?? ?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]);?? ?//對(duì)新圖賦予新值?? ?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; } 原圖:



高斯:


均值:


中值:




總結(jié)

以上是生活随笔為你收集整理的均值滤波 中值滤波 高斯平滑滤波的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。