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

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


????????? 圖像平滑用于去除圖像中的噪聲。高斯平滑,就是將每個像素的灰度值用其領域的加權平均值代替。該算法簡單,能夠有效去除高斯噪聲。

?????? 高斯平滑模板:?
??????????圖像高斯平滑 - illidan - illidan的博客
//高斯平滑  中值濾波  均值濾波?? ?
#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;
}
原圖:



高斯:


均值:


中值:




本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/255543.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/255543.shtml
英文地址,請注明出處:http://en.pswp.cn/news/255543.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

javaWeb開發總結 ---- 前端數據插入到后臺

一&#xff0c;概述&#xff1a; 本文主要描述如何將數據通過表單提交到后臺并插入到數據庫&#xff0e;其中后臺使用spring框架&#xff0e; 二&#xff0c;開發流程&#xff1a; 明確需求&#xff0c;即將什么數據插入到數據庫平臺搭建&#xff0c;配置spring, 數據庫&#…

對clear float 的理解

之前自己對于清除浮動的用法比較模糊 &#xff0c;如果用到的話&#xff0c;一般都是采用簡單粗暴的方式解決&#xff0c;就是直接用overflow&#xff1a;hidden&#xff0c;但是越用久就會發現其實有BUG&#xff0c;這個BUG正是overflow&#xff1a;hidden帶來的&#xff0c;因…

【機器視覺】——相機標定降低重投影誤差方法

目錄 一、標定結果分析 二、影響標定誤差的原因 1、平面標定板黑白棋盤格的精度

linux 安裝RedisLive

為什么80%的碼農都做不了架構師&#xff1f;>>> RedisLive 用來監控Redis&#xff0c;便于redis性能分析 安裝步驟&#xff1a; 1.安裝pip&#xff08;代碼參考&#xff1a;https://github.com/pypa/pip&#xff09; 官方網站&#xff1a;https://pypi.python.org/…

高斯濾波和雙向濾波的區別與聯系

1. 簡介 圖像平滑是一個重要的操作&#xff0c;而且有多種成熟的算法。這里主要簡單介紹一下Bilateral方法&#xff08;雙邊濾波&#xff09;&#xff0c;這主要是由于前段時間做了SSAO&#xff0c;需要用bilateral blur 算法進行降噪。Bilateral blur相對于傳統的高斯blur來說…

Eclipse變量名自動補全問題 自定義上屏按鍵為TAB

Eclipse空格等號等都可以上屏&#xff0c;這樣有時候輸入變量名再按空格就會自動補全&#xff0c;非常討厭。那么怎么辦呢&#xff1f; 1.首先你的Eclipse需要裝有 Eclipse plug-in development environment 和 Eclipse JDT Plug-in Developer Resources 或者直接去下載一個和…

Dreamweaver CS6 Mac破解版

介于之前小子分享的Mac版的Adobe的PS CS6和Illustrator CS6&#xff0c;有用戶要求小子分享下Dreamweaver&#xff0c;小子當然樂意效勞。 Dreamweaver CS6 是世界頂級軟件廠商Adobe推出的一套擁有可視化編輯界面&#xff0c;用于制作并編輯網站和移動應用程序的網頁設計軟件。…

【pyqt5學習】——graphicView顯示matplotlib圖像

目錄 一、導入模塊 二、自定義一個matplotlib窗口類Figure 三、利用QT_designer繪制窗口 四、寫邏輯代碼 五、結果展示 一、導入模塊 import matplotlibmatplotlib.use("Qt5Agg") from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg from matplo…

happens-before規則

1&#xff09;程序順序規則&#xff1a;一個線程中的每個操作&#xff0c;happens-before于該線程中的任意后續操作。2&#xff09;監視器鎖規則&#xff1a;對一個鎖的解鎖&#xff0c;happens-before于隨后對這個鎖的加鎖。3&#xff09;volatile變量規則&#xff1a;對一個v…

what is ssao

說到ssao 就要從ao說起&#xff0c;ao&#xff0c;即間接環境光遮蔽技術。我們知道現實中的光線&#xff0c;除了來自太陽和電燈的直射光線以外&#xff0c;光線碰到物體以后&#xff0c;還會再次反射&#xff0c;折射&#xff0c;而再次反射折射的過程中&#xff0c;又會被其他…

【pyqt5學習】——groupBox顯示matplotlib圖像

目錄 一、導入模塊 二、創建matplotlib窗口類 三、qt_designer設計窗口 四、邏輯代碼 五、結果展示 一、導入模塊 import matplotlibmatplotlib.use("Qt5Agg") from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg from matplotlib.figure impor…

[BZOJ3545][ONTAK2010]Peaks

[BZOJ3545][ONTAK2010]Peaks 試題描述 在Bytemountains有N座山峰&#xff0c;每座山峰有他的高度h_i。有些山峰之間有雙向道路相連&#xff0c;共M條路徑&#xff0c;每條路徑有一個困難值&#xff0c;這個值越大表示越難走&#xff0c;現在有Q組詢問&#xff0c;每組詢問詢問從…

杭電1027Ignatius and the Princess II模擬

地址&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid1027 題目&#xff1a; Problem DescriptionNow our hero finds the door to the BEelzebub feng5166. He opens the door and finds feng5166 is about to kill our pretty Princess. But now the BEelzebub has t…

angular 使用rxjs 監聽同級兄弟組件數據變化

angular 的官網給出了父子組件之間數據交互的方法&#xff0c;如ViewChild、EventEmitter 但是如果要在同級組件之間進行數據同步&#xff0c;似乎并沒有給出太多的信息。 有時候我們想&#xff0c;在一個組件中修改數據之后&#xff0c;馬上反映到另外一個組件中&#xff0c; …

OpenCV里IplImage的widthStep參數 和width參數

一直以為IplImage結構體中的widthStep元素大小等于width*nChannels&#xff0c;大錯特錯&#xff01;&#xff08;為了快速訪問&#xff0c;要內存對齊啊&#xff09;查看OpenCV2.1的源碼&#xff0c;在src/cxcore/cxarray.cpp文件中&#xff0c;找到cvInitImageHeader函數&…

【數字信號處理】——Python頻譜繪制

# -*- coding: utf-8 -*- from matplotlib import pyplotpyplot.rcParams[font.sans-serif] [SimHei] pyplot.rcParams[axes.unicode_minus] Falseimport numpy as np import matplotlib.pyplot as pl import matplotlib import math import randomN 500 # 繪制點總數 fs 5…

Android開發:《Gradle Recipes for Android》閱讀筆記1.3

想命令行執行gradle的構建&#xff0c;可以通過提供的gradle wrapper或者安裝gradle。 構建android項目不需要安裝gradle&#xff0c;因為android studio已經包含gradle。"gradle wrapper"指的是根目錄下的gradlew和gradlew.bat腳本&#xff08;結尾的w是wrapper的意…

pic

轉載于:https://www.cnblogs.com/edisonxiang/p/5392651.html

leetcode 643 Maximum Average Subarray I

題目詳情 Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value. And you need to output the maximum average value. 輸入一個數組nums和一個整數k。要求找出輸入數組中長度為k的子數組&#xff0c…