數字圖像處理算法_第1頁
數字圖像處理算法_第2頁
數字圖像處理算法_第3頁
數字圖像處理算法_第4頁
數字圖像處理算法_第5頁
已閱讀5頁,還剩38頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、“摘要:關于空間域圖像處理算法框架,直方圖處理,空間域濾波器算法框架的編程心得,使用GDI+(C+)一,圖像文件的讀取初學數字圖像處理時,圖像文件的讀取往往是一件麻煩的事情,我們要面對各種各樣的圖像文件格式, 如果僅用C+的fstream 庫那就必須了解各種圖像編碼格式,這對于初學圖像處理是不太現實的,需要 一個能幫助輕松讀取各類圖像文件的庫。在Win32平臺上GDI+(C+) 是不錯的選擇,不光使用上相對于Win32 GDI要容易得多,而且也容易移植到.Net平臺上的GDI+。Gdiplus:Bitmap 類為我們提供了讀取各類圖像文件的接口, Bitmap:LockBits方法產生的Bit

2、mapData類也為我們提供了高速訪問圖像文件流的途徑。這樣我們就可以將精力集中于圖像處理算法的實現,而不用關心各種圖像編碼。具體使用方式請參考MSDN中GDI+文檔中關于Bitmap 類和BitmapData類的說明。另外GDI+僅在Windows XP/2003 上獲得直接支持,對于Windows 2000必須安裝相關DLL,或者安裝有 Office 2003,Visual Studio 2003 .Net等軟件。二,空間域圖像處理算法框架(1)在空間域圖像處理中,對于一個圖像我們往往需要對其逐個像素的進行處理,對每個像素的處理使用 相同的算法(或者是圖像中的某個矩形部分)。即,對于圖像f

3、(x,y),其中0WxWM,0WyWN,圖像為M*N大小,使用算法algo,則f(x,y) = algo(f(x,y)。事先實現一個算法框架,然后再以函數指針或函數對 象(functor,即實現operator。的對象)傳入算法,可以減輕編程的工作量。如下代碼便是一例:#ifndef PROCESSALGO_H#define PROCESSALGO_H#include #include namespace nsimgtktemplate bool ProcessPixelsOneByOne (Gdiplus : Bitmap * const p_bitmap , Processor proce

4、ssor , unsigned int x, unsigned y,unsigned int width , unsigned int height )if (p_bitmap = NULL )return false ;if (width + x p_bitmap - GetWidth () | (height + y p_bitmap - GetHeight ()return false ;Gdiplus : BitmapData bitmapData ;Gdiplus : Rect rect (x, y , width ,height );if ( p_bitmap - LockBits

5、 (& rect , Gdiplus : ImageLockModeWrite , pixelFormat , & bitmapData ) != Gdiplus : Okreturn false ;pixelType * pixels = (pixelType *) bitmapData .ScanO ;for (unsigned int row = 0; row height ; + row )for (unsigned int col = 0; col UnlockBits (& bitmapData ) != Gdiplus : Ok)return false ;return true

6、 ;#endifProcessPixelsOneByOne函數可以對圖像中從(x,y)位置起始,width*height 大小的區域進行處理。模板參數pixelType 用于指定像素大小,例如在 Win32平臺上傳入unsigned char即為8位,用于8階 灰度圖。模板參數Processor為圖像處理算法實現,可以定義類實現 void operator(pixelType *)函數, 或者傳入同樣接口的函數指針。如下便是一些算法示例(說明見具體注釋):#ifndef SPATIALDOMAIN_H#define SPATIALDOMAIN_H#include #include namesp

7、ace nsimgtk/ 8 階灰度圖的灰度反轉算法class NegativeGray8public :void operator ()( unsigned char * const p_value )* p_value 人=Oxff ;;/ 8 階灰度圖的Gamma校正算法class GammaCorrectGray8private :unsigned char d_s 256 ;public :GammaCorrectGray8 : GammaCorrectGray8 (double c, double gamma );void operator ()( unsigned char *

8、const p_value )* p_value = d_s * p_value ;;/ 8階灰度圖的飽和度拉伸算法class ContrastStretchingGray8private :unsigned char d_s 256 ;public :ContrastStretchingGray8 : ContrastStretchingGray8 (double al , double bl ,unsigned int x1 ,b3 );double a2 , double b2 , unsigned int x2 , double a3 , double void operator ()

9、( unsigned char * const p_value )* p_value = d_s * p_value ;/ 8階灰度圖的位平面分割,構造函數指定位平面號class BitPlaneSliceGray8private :unsigned char d_s 256 ;public :BitPlaneSliceGray8 (unsigned char bitPlaneNum ); void operator ()( unsigned char * const p_value )* p_value = d_s * p_value ;#endif/上述類中各構造函數的實現代碼,應該分在另

10、一個文件中,此處為說明方便,一并列岀#include SpatialDomain/spatialDomain.hnamespace nsimgtkGammaCorrectGray8 : GammaCorrectGray8 (double c, double gamma )double temp ;for (unsigned int i=0; i 255 | x2 255 | x1 x1 )for (unsigned int i = 0; i 256 ; + i)d_s i = i;elsedouble tmp ;for (unsigned int i = 0; i x1 ; + i)tmp =

11、 ceil (a1 * double (i)+ b1 );d_s i = (unsigned char )tmp ;for(unsigned int i = x1 ; i x2 ; + i)tmp = ceil (a2 * double + b2 );d_si = (unsigned char )tmp ;for(unsigned int i = x2 ; i 256 ; + i)tmp = ceil (a3 * double (i)+ b3 );d_s i = (unsigned char )tmp ;BitPlaneSliceGray8:BitPlaneSliceGray8 (unsign

12、ed char bitPlaneNum )unsignedchar bitMaskArray8=0x01 , 0x02,0x04 ,0x08 ,0x10 , 0x20,0x40 ,0x80;for(unsignedinti = 0; i bitPlaneNum ) * 255 ;(2)直方圖在GDI+1.0中沒有獲得支持,我們必須自行實現。直方圖相關的處理在數字圖像處理中占有重要地位,可以通過它獲取圖像灰度級的統計信息,且可以通過直方圖進行一些重要的圖像增強技術,如 直方圖均衡化,直方圖規定化,基本全局門限等。下面是獲取8階圖像直方圖的算法實現:namespace nsimgtkbool Ge

13、tHistogramNormalizeGray8 (Gdiplus : Bitmap * const p_bitmap , float* histogramArray )if (p_bitmap = NULL | histogramArray = NULL )return false ;Gdiplus : BitmapData bitmapData ;Gdiplus : Rect rect (0, 0, p_bitmap - GetWidth (), p_bitmap - GetHeight ();if (p_bitmap - LockBits (& rect , Gdiplus : Imag

14、eLockModeRead ,PixelFormat8bpplndexed , &bitmapData ) != Gdiplus : Ok)return false ;unsigned char * pixels = (unsigned char *) bitmapData . Scan0 ;unsigned int histogram 256 ;for (int i = 0; i 256 ; + i)histogram i = 0;for (unsigned int row =0; row GetWidth (); + row )for (unsigned int col = 0; col

15、GetHeight (); + col)+ histogram pixels col + row * bitmapData . Stride ;const unsigned int totalPixels = p_bitmap - GetWidth () *p_bitmap - GetHeight ();for (int i = 0; i UnlockBits (& bitmapData ) != Gdiplus : Ok)return false ; return true在獲取直方圖后(即上面算法的第二個參數),再將其作為參數傳入下面的對象的構造函數,然后以該對象為仿函數傳入Process

16、PixelsOneByOne 即可實現8階圖像直方圖均衡化:#ifndef SPATIALDOMAIN_H#define SPATIALDOMAIN_H#include #include namespace nsimgtk/ 8階灰度圖的直方圖均衡化class HistogramEqualizationGray8private :unsigned char d_s 256 ;public :HistogramEqualizationGray8 (const float * const histogramArray );void operator ()( unsigned char * cons

17、t p_value )* p_value = d_s * p_value ;#endif/#include SpatialDomain/spatialDomain.hnamespace nsimgtkHistogramEqualizationGray8 : HistogramEqualizationGray8 (const float * consthistogramArray)if(histogramArray!= NULL )float sum =0.0 ;for (int i= 0 ;i256 ; + i)sum += histogramArray i;d_s【i= unsigned c

18、har (sum * 255 );(3)空間域濾波器,濾波器是一個m*n大小的掩模,其中 m,n均為大于1的奇數。濾波器逐像素地通過圖像的全部或部分矩形區域,然后逐像素地對掩模覆蓋下的像素使用濾波器算法獲得響應,將響應賦值于 當前像素即掩模中心像素,另外濾波器算法使用中將會涉及到圖像邊緣的問題,這可以對邊緣部分掩模使 用補零法補齊掩模下無像素值的區域,或者掩模的移動范圍以不越岀圖像邊緣的方式移動,當然這些處理 方法都會給圖像邊緣部分帶來不良效果,但是一般情況下,圖像邊緣部分往往不是我們關注的部分或者沒 有重要的信息。下面的濾波器算法框架SpatialFilterAlgo 即以補零法(zero-

19、padding) 實現:#ifndefSPATIALFILTER_H#defineSPATIALFILTER_H#include#include#include#include#include #include namespace nsimgtktemplate bool SpatialFilterAlgo (Gdiplus : Bitmap * const p_bitmap , FilterMask filterMask , unsigned int x , unsigned int y ,unsigned int width , unsigned int height )if (p_bit

20、map = NULL )return false ;if (width + x p_bitmap - GetWidth () | (height +y p_bitmap - GetHeight ()return false ;Gdiplus : BitmapData bitmapData ;Gdiplus : Rect rect (x, y , width ,height );if ( p_bitmap - LockBits (& rect , Gdiplus : ImageLockModeWrite , pixelFormat ,&bitmapData) != Gdiplus : Ok)re

21、turn false ;/ masks width/ masks height/ extend image to.Stride / sizeof ( pixpixelType * pixels = (pixelType *) bitmapData .ScanO ;const unsigned int m = filterMask .d_m ;const unsigned int n = filterMask .d_n ;std : vector tmpImage ( m -1 + width )*( n -1 + height ); use zero-padding/ copy origina

22、l bitmap to extended image with zero-padding methodfor (unsigned int row = 0; row height ; + row )for (unsigned int col = 0; col width ; + col)tmpImage ( col + m/2)+( row + n/2)*( bitmapData elType )+ m -1)=pixels col + row * bitmapData .Stride /sizeof (pixelType );/ process every pixel with filterM

23、askfor (unsigned int row = 0; row height ; + row )for (unsigned int col = 0; col width ; + col)/ fill the m*n mask with the current pixels neighborhoodfor (unsigned int i = 0; i n ; + i)for (unsigned int j = 0 ; j UnlockBits (& bitmapData ) != Gdiplus : Ok )return false ;return true ;#endif其中模板參數Fil

24、terMask即為濾波掩模算法。通常的濾波算法有均值濾波器,可以模糊化圖像,去除圖形中的細節部分,使得我們可以關注圖像中較為明顯的部分,均值濾波器用于周期性噪聲。中值濾波器用于 圖像中存在椒鹽噪聲也即脈沖噪聲的情況下。另外有基于一階微分的 Sobel梯度算子和基于兩階微分的拉普拉斯算子,它們往往被用于邊緣檢測中。下面是一些濾波器算法的具體實現,所以濾波器算法都應該實現pixelType response ()函數以及有d_mask,d_m,d_n成員,這可以通過繼承 _filteMask類獲得(不需要付出虛函數代價)。#ifndefSPATIALFILTER_H#defineSPATIALFI

25、LTER_H#include#include#include#includevgdiplus.h#include#includenamespace nsimgtkmask/濾波器掩模的基類,提供掩模大小d_m,d_n ,掩模覆蓋下的 m*n個像素值d,/ others filterMask should inherit ittemplate struct _filterMaskconst unsignedint d_m ;const unsignedint d_n ;/ images pixels under the m*n filter maskstd : vector d_mask ;/

26、filter masks width and heigh must be a odd, if not, it will plus one for the width or the height_filterMask( unsigned int m , unsigned int n ):d_m (m % 2 ? m : m + 1), d_n (n % 2 ? n : n+ 1), d_mask (d_m * d_n );/掩模權值為全1的均值濾波器template class averagingFilterMaskSp:public _filterMaskpublic :averagingFi

27、lterMaskSp (unsigned int m , unsigned int n):_filterMask( m , n ) pixelType response ()return std : accumulate (d_mask . begin (), d_mask .end (), 0) / (d_m * d_n );/可自定義掩模權值的均值濾波器template class averagingFilterMask:public _filterMaskprivate :std : vector d_weight ;/ weights vector(m*n)int d_weight_s

28、um ;/ all weights sumpublic :averagingFilterMask (unsigned int m , unsigned int n , const std : vector &weightVec ):_filterMask( m , n ), d_weight (weightVec )if (weightVec .size () != d_mask .size ()/ if weights size isnt equal to masks size, it will change filter mask as a specialfilter maskd_weig

29、ht .resize (d_mask .size (), 1);d_weight_sum = std : accumulate (d_weight . begin (), d_weight .end (), 0);pixelType response ()return std : inner_product (d_mask .begin (), d_mask .end (), d_weight .begin (), 0) / d_weight_sum ;/中值濾波器template class medianFilterMask:public _filterMaskpublic :medianF

30、ilterMask(unsignedint m , unsigned int n):_filterMask( m , n ) pixelType response ()std : sort (d_mask .begin (), d_mask .end ();return d maskd_mask . size ()/ 2;/ 3*3拉普拉斯濾波器/ the mask is: 0 1 00 -1 0/1 -5 1or-1 5 -1/0 1 00 -1 0ret=max;/ if pixels brightness is less than min, set it to min/ if pixel

31、s brightness is larger than max, set it to maxtemplate class laplacianFilter:public _filterMaskpubliclaplacianFilter ():_filterMask( 3 , 3) pixelType response ()int ret = (int )( 5*( int ) d_mask 4)-(int)d_mask 5+ d_mask 3+ d_mask 1 + d_mask 7);if(retmax)return ret ;;/ 3*3Sobel 濾波器/ the mask is: -1

32、-2 -1-1 0 1/0 0 0 and -2 0 2/ 1 2 1 -1 0 1/ if pixels brightness is larger than max, set it to maxtemplate class sobelFilter:public _filterMaskpublic :sobelFilter ():_filterMask( 3 , 3) pixelType response ()int ret=:abs (d_mask 6+ 2* d_mask 7+ d_mask 8- d_mask 0- 2* d_mask 1 - d_mask 2)+ : abs (d_ma

33、sk 2+ 2 * d_mask 5+ d_mask 8 - d_mask 0 - 2 * d_mask 3- d_mask 6);if (ret max )ret = max ;return ret ;#endif數字圖像處理算法實現 編程心得(1)2001414 班朱偉 20014123摘要:關于空間域圖像處理算法框架,直方圖處理,空間域濾波器算法框架的編程心得,使用GDI+(C+)一,圖像文件的讀取初學數字圖像處理時,圖像文件的讀取往往是一件麻煩的事情,我們要面對各種各樣的圖像文件格式, 如果僅用C+的fstream 庫那就必須了解各種圖像編碼格式,這對于初學圖像處理是不太現實的,需要

34、一個能幫助輕松讀取各類圖像文件的庫。在Win32平臺上GDI+(C+) 是不錯的選擇,不光使用上相對于Win32 GDI要容易得多,而且也容易移植到.Net平臺上的GDI+。Gdiplus:Bitmap類為我們提供了讀取各類圖像文件的接口,Bitmap:LockBits 方法產生的BitmapData類也為我們提供了高速訪問圖像文件流的途徑。這樣我們就可以將精力集中于圖像處理算法的實現,而不用關心各種圖像編碼。具體使用方式請參考MSDN中GDI+文檔中關于Bitmap 類和BitmapData類的說明。另外GDI+僅在Windows XP/2003 上獲得直接支持,對于 Windows 200

35、0必須安裝相關 DLL,或者安裝有 Office 2003,Visual Studio 2003 .Net等軟件。二,空間域圖像處理算法框架(1)在空間域圖像處理中,對于一個圖像我們往往需要對其逐個像素的進行處理,對每個像素的處理使用 相同的算法(或者是圖像中的某個矩形部分)。即,對于圖像f(x,y),其中0WxWM,0WyWN,圖像為M*N大小,使用算法algo,則f(x,y) = algo(f(x,y)。事先實現一個算法框架,然后再以函數指針或函數對 象(functor,即實現operator。 的對象)傳入算法,可以減輕編程的工作量。如下代碼便是一例:#ifndef PROCESSALG

36、O_H#define PROCESSALGO_Hreturn false ;#include #include namespace nsimgtktemplate int x, unsignedbool ProcessPixelsOneByOne (Gdiplus : Bitmap * const p_bitmap , Processor processor , unsignedy,unsigned int width , unsigned int height )if (p_bitmap = NULL )return false ;if (width + x p_bitmap - GetWi

37、dth () | (height + y p_bitmap - GetHeight ()return false ;Gdiplus : BitmapData bitmapData ;Gdiplus : Rect rect (x, y , width ,height );!= Gdiplus : Okif ( p_bitmap - LockBits (& rect , Gdiplus : ImageLockModeWrite , pixelFormat , & bitmapData )pixelType * pixels = (pixelType *) bitmapData .ScanO ;fo

38、r (unsigned int row = 0; row height ; + row )for (unsigned int col = 0; col UnlockBits (& bitmapData ) != Gdiplus : Ok)return false ;return true ;#endifProcessPixelsOneByOne函數可以對圖像中從(x,y)位置起始,width*height 大小的區域進行處理。模板參數pixelType 用于指定像素大小,例如在 Win32平臺上傳入unsigned char即為8位,用于8階 灰度圖。模板參數Processor為圖像處理算法實

39、現,可以定義類實現 void operator(pixelType *)函數, 或者傳入同樣接口的函數指針。如下便是一些算法示例(說明見具體注釋):#ifndefSPATIALDOMAIN_H#defineSPATIALDOMAIN_H#include #include namespace nsimgtk/ 8 階灰度圖的灰度反轉算法class NegativeGray8public :void operator ()( unsigned char * const p_value )* p_value 人=Oxff ;;/ 8 階灰度圖的Gamma校正算法class GammaCorrectG

40、ray8private :unsigned char d_s 256 ;public :gamma );GammaCorrectGray8 : GammaCorrectGray8 (double c, doublevoid operator ()( unsigned char * const p_value )* p_value = d_s * p_value ;;/ 8階灰度圖的飽和度拉伸算法class ContrastStretchingGray8private :unsigned char d_s 256 ;public :b1 ,ContrastStretchingGray8 : Co

41、ntrastStretchingGray8 (double a1 , double unsigned int x1 ,double a2 , double b2 , unsigned int x2 , double a3 , double b3 );void operator ()( unsigned char * const p_value )* p_value = d_s * p_value ;/ 8階灰度圖的位平面分割,構造函數指定位平面號class BitPlaneSliceGray8private :unsigned char d_s 256 ;public :BitPlaneSli

42、ceGray8 (unsigned char bitPlaneNum );void operator ()( unsigned char * const p_value )* p_value = d_s * p_value ;#endif/上述類中各構造函數的實現代碼,應該分在另一個文件中,此處為說明方便,一并列岀#include SpatialDomain/spatialDomain.hnamespace nsimgtkGammaCorrectGray8 : GammaCorrectGray8 (double c, double gamma )double temp ;for (unsign

43、ed int i= 0; i 255 | x2 255 | x1 x1 )for (unsigned int i = 0; i 256 ; + i)d_s i = i;elsedouble tmp ;for (unsigned int i = 0; i x1 ; + i)tmp = ceil (a1 * double (i)+ bl ); d_si = (unsigned char )tmp ;forforBitPlaneSliceGray8unsigned;(unsigned int i = x1 ; i x2 ; + i)tmp = ceil (a2 * double (i)+ b2 );

44、d_s i = (unsigned char )tmp ;(unsigned int i = x2; i 256 ; + i)tmp = ceil (a3 * double (i)+ b3 );d_s i = (unsigned char )tmp ;:BitPlaneSliceGray8 (unsigned char bitPlaneNum )char bitMaskArray0x01 , 0x02 , 0x04 ,0x10 , 0x20 , 0x40 ,8=0x08 ,0x80for (unsigned int i=0; i bitPlaneNum ) * 255 ;d_s i = tmp ;(2)直方圖在GDI+1.0中沒有獲得支持,我們必須自行實現。直方圖相關的處理在數字圖像處理中占有重要地位,可以通過它獲取圖像灰度級的統計信息,且可以通過直方圖進行一些重要的圖像增強技術,如 直方圖均衡化,直方圖規定化,基本全局門限等。下面是獲取8階圖像直方圖的算法實現:namespace nsimgtkbool GetHistogramNormalizeGray8 (Gdiplus : Bitmap * const p_bitmap , float* histogramArray )if (p_bitmap = NULL | h

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論