【正文】
axes()。 imagesc(im_filtered)。 colormap(gray(256))。 axis equal。 axis tight。 axis off。 set(,39。HandleVisibility39。,39。OFF39。) [labeled,numObjects] = bwlabel(im_filter,4)。 numObjects。 % function varargout = QuitPush_Callback(h, eventdata, handles, varargin) 廈門大學(xué)本科畢業(yè)論文 附錄 28 clear all % to close GUI close(gcbf) % 加噪函數(shù) function newim = gaussian_bai(img) newim = imnoise(img,39。gaussian39。)。 function newim = salt(img) newim = imnoise(img,39。salt amp。 pepper39。,)。 function newim = speckle(img) newim = imnoise(img,39。speckle39。)。 % 去噪函數(shù) function out = meanfilter(im) out = filter2(fspecial(39。average39。,3),im)。 %執(zhí)行二維的均值 濾波 function out = medfilter(im) out = medfilt2(im)。 %執(zhí)行二維的中值濾波 function out = wienerfilter(im) % im = imnoise(I,39。gaussian39。,0,)。 out = wiener2(im,[5 5])。 %執(zhí)行二維的維納濾波 function out = forced_filter(im) [c1,l1]=wavedec2(im,2,39。sym439。)。 a1=wrcoef2(39。a39。,c1,l1,39。sym439。,1)。 a2=wrcoef2(39。a39。,c1,l1,39。sym439。,2)。 out=a2。 %執(zhí)行二維的強制去噪處理 function out = all_filter(im) [c,l]=wavedec2(im,5,39。sym439。)。 thr=50。 [sxd,cxd,lxd,perf0,perf12]=wdencmp(39。gbl39。,im,39。sym239。,2,thr,39。h39。,1)。 out=sxd。 %執(zhí)行全局閾值去噪 function out = setting_filter(im) thr_h=[75 70]。 thr_d=[74 72]。 廈門大學(xué)本科畢業(yè)論文 附錄 29 thr_v=[78 68]。 thr2=[thr_h。thr_d。thr_v] [yxd,cxd,lxd,perf0,perf12]=wdencmp(39。lvd39。,im,39。sym239。,2,thr2,39。h39。)。 out=yxd。 %執(zhí)行給定閾值去噪 % function varargout = Untitled_1_Callback(h, eventdata, handles, varargin) % function varargout = Untitled_2_Callback(h, eventdata, handles, varargin) % Executes on button press in Savepushbutton7. function Savepushbutton7_Callback(hObject, eventdata, handles) global im_original im_noised im_filtered [filename, pathname] = uiputfile( 39。*.bmp。*.pgm。*.tif。*.jpg。*.*39。, 39。Save imagecrop image39。)。 imwrite(im_filtered,filename)。 % Executes on button press in Saveushbutton7. function Saveushbutton7_Callback(hObject, eventdata, handles) global im_original im_noised im_filtered [filename, pathname] = uiputfile( 39。*.bmp。*.pgm。*.tif。*.jpg。*.*39。, 39。Save imagecrop image39。)。 imwrite(im_filtered,filename)。 if ispc set(hObject,39。BackgroundColor39。,39。white39。)。 else set(hObject,39。BackgroundColor39。,get(0,39。defaultUicontrolBackgroundColor39。))。 end function edit10_Callback(hObject, eventdata, handles) function popupmenu5_CreateF(hObject, eventdata, handles) if ispc set(hObject,39。BackgroundColor39。,39。white39。)。 else set(hObject,39。BackgroundColor39。,get(0,39。defaultUicontrolBackgroundColor39。))。 end 廈門大學(xué)本科畢業(yè)論文 附錄 30 副程序: : function imagBW = kittlerMet(imag) % KITTLERMET binarizes a gray scale image 39。imag39。 into a binary image % Input: % imag: the gray scale image, with black foreground(0), and white % background(255). % Output: % imagBW: the binary image of the gray scale image 39。imag39。, with kittler39。s % minimum error thresholding algorithm. % Reference: % J. Kittler and J. Illingworth. Minimum Error Thresholding. Pattern % Recognition. 1986. 19(1):4147 MAXD = 100000。 imag = imag(:,:,1)。 [counts, x] = imhist(imag)。 % counts are the histogram. x is the intensity level. GradeI = length(x)。 % the resolusion of the intensity. . 256 for uint8. J_t = zeros(GradeI, 1)。 % criterion function prob = counts ./ sum(counts)。 % Probability distribution meanT = x39。 * prob。 % Total mean level of the picture % Initialization w0 = prob(1)。 % Probability of the first class miuK = 0。 % Firstorder cumulative moments of the histogram up to the kth level. J_t(1) = MAXD。 n = GradeI1。 for i = 1 : n w0 = w0 + prob(i+1)。 miuK = miuK + i * prob(i+1)。 % firstorder cumulative moment if (w0 == 0) || (w0 == 1) J_t(i+1) = MAXD。 % T = i else miu1 = miuK / w0。 miu2 = (meanTmiuK) / (1w0)。 var1 = (((0 : i)39。miu1).^2)39。 * prob(1 : i+1)。 var1 = var1 / w0。 % variance var2 = (((i+1 : n)39。miu2).^2)39。 * prob(i+2 : n+1)。 var2 = var2 / (1w0)。 if var1 0 amp。amp。 var2 0 % in case of var1=0 or var2 =0 J_t(i+1) = 1+w0 * log(var1)+(1w0) * log(var2)2*w0*log(w0)2*(1w0)*log(1w0)。 else J_t(i+1) = MAXD。 end 廈門大學(xué)本科畢業(yè)論文 附錄 31 end end minJ = min(J_t)。 index = find(J_t == minJ)。 th = mean(index)。 th = (th1)/n imagBW = im2bw(imag, th)。 % figure, imshow(imagBW), title(39。kittler binary39。)。 : function imagBW = otsu(imag) % Reference: % Nobuyuki Otsu. A Threshold Selection Method from GrayLevel Histograms. % IEEE Transactions on Systems, Man, and Cyberics. (1):6266 imag = imag(:, :, 1)。 [counts, x] = imhist(imag)。 % counts are the histogram. x is the intensity level. GradeI = length(x)。 % the resolusion of the intensity. . 256 for uint8. varB = zeros(GradeI, 1)。 % Betweenclass Variance of binarized image. prob = counts ./ sum(counts)。 % Probability distribution meanT = 0。 % Total mean level of the picture for i = 0 : (GradeI1) meanT = meanT + i * prob(i+1)。 end varT = ((xmeanT).^2)39。 * prob。 % Initialization w0 = prob(1)。 % Probability of the first class miuK = 0。 % Firstorder cumulative moments of the histogram up to the kth level. varB(1) = 0。 % Betweenclass variance calculation for i = 1 : (GradeI1) w0 = w0 + prob(i+1)。 miuK = miuK + i * prob(i+1)。 if (w0 == 0) || (w0 == 1) varB(i+1) = 0。 else varB(i+1)