阈值法进行边缘检测matlab
1、基于全局閾值處理
1)選擇初始閾值T
2)用T分割圖像,分別是大于T的像素集合和小于T的像素集合。
3)計(jì)算兩集合的平均灰度m1,m2
4)計(jì)算新閾值T=(m1+m2)/2
5)重復(fù)上述步驟,直至兩次迭代的T的變化小于一定值
6)利用函數(shù)im2bw分割圖像
例:
clc clear f=imread('E:\桌面\數(shù)字圖像matlab\DIP3E_CH10_Original_Images\DIP3E_Original_Images_CH10\Fig1038(a)(noisy_fingerprint).tif');count=0; T=mean2(f); done=false; while ~donecount=count+1;g=f>T;Tnext=0.5*(mean(f(g))+mean(f(~g)));done=abs(T-Tnext)<0.5;T=Tnext; end g=im2bw(f,T/255); subplot(121),imshow(f)subplot(122),imshow(g) figure,imhist(f)結(jié)果如下:
count=2,迭代兩次即收斂。
直方圖有兩個(gè)谷峰,很明顯可以分開,程序得到T=125,由圖可以看出來是可行的。
2、otsu方法的最佳全局閾值處理
閾值k將灰度分為兩組。
首先定義可分性度量,即類間方差與圖像灰度方差的比。越大說明可分性越好。
工具箱函數(shù)graythresh,[T,SM]=graythresh(f)
例:
clc clear f=imread('E:\桌面\數(shù)字圖像matlab\DIP3E_CH10_Original_Images\DIP3E_Original_Images_CH10\Fig1039(a)(polymersomes).tif');count=0; T1=mean2(f); done=false; while ~donecount=count+1;g=f>T1;Tnext=0.5*(mean(f(g))+mean(f(~g)));done=abs(T1-Tnext)<0.5;T1=Tnext; endg=im2bw(f,T1/255); subplot(121),imshow(g)[T2,SM]=graythresh(f); g1=im2bw(f,T2); subplot(122),imshow(g1)?結(jié)果如下:
第一張圖是利用第一種方法得到的,第二個(gè)是otsu方法得到了更好的分割效果。
?
?還有一種基于圖像直方圖給出閾值的函數(shù)otsuthresh。
3、使用圖像平滑改進(jìn)全局閾值
當(dāng)圖像含有噪聲時(shí),進(jìn)行圖像平滑(濾波)再進(jìn)行分割會(huì)更好。
例:
clc clear f=imread('E:\桌面\數(shù)字圖像matlab\DIP3E_CH10_Original_Images\DIP3E_Original_Images_CH10\Fig1036(a)(original_septagon).tif');fn=imnoise(f,'gaussian',0,0.038);%噪聲圖 subplot(231),imshow(fn)subplot(232),imhist(fn) Tn=graythresh(fn); gn=im2bw(fn,Tn); subplot(233),imshow(gn)%直接進(jìn)行otsuw=fspecial('average',5); fa=imfilter(fn,w,'replicate');%平均值濾波 subplot(234),imshow(fa) subplot(235),imhist(fa) Ta=graythresh(fa); ga=im2bw(fa,Ta); subplot(236),imshow(ga)結(jié)果如下:
進(jìn)行濾波之后,圖像可分性變得更強(qiáng)了。
?4、使用邊緣改進(jìn)全局閾值
例:使用基于梯度的邊緣信息改進(jìn)全局閾值
(通過選擇梯度較大的像素點(diǎn),也就是邊緣,然后在進(jìn)行全局閾值處理)
clc clearf=imread('E:\桌面\數(shù)字圖像matlab\DIP3E_CH10_Original_Images\DIP3E_Original_Images_CH10\Fig1041(a)(septagon_small_noisy_mean_0_stdv_10).tif'); subplot(231),imshow(f) f=tofloat(f); sx=fspecial('sobel'); sy=sx'; gx=imfilter(f,sx,'replicate'); gy=imfilter(f,sy,'replicate'); grad=sqrt(gx.*gx+gy.*gy); grad=grad/max(grad(:));h=imhist(grad); subplot(232),imhist(f) Q=percentile2i(h,0.999);%生成閾值,用來找出較大的99.9%的梯度值markerImage=grad>Q;%梯度選擇后的 subplot(233),imshow(markerImage); fp=f.*markerImage; subplot(234),imshow(fp) hp=imhist(fp);hp(1)=0;%因?yàn)榇蟛糠质?值,所以把0的統(tǒng)計(jì)數(shù)去掉 subplot(235),bar(hp) T=otsuthresh(hp);%以直方圖作為輸入 T*(numel(hp)-1)g=im2bw(f,T);%大于T的為1,小于為0,得到二值圖像 subplot(236),imshow(g)結(jié)果如下:
? ? ? ?進(jìn)行梯度選擇后的圖像,可分性變強(qiáng)。
? ? ? ?這個(gè)例子中中間白色物體的大小比背景小的多,對直方圖的貢獻(xiàn)很小,所以如果運(yùn)用上面的平均值濾波,得不到想要的結(jié)果,只能考慮邊緣信息。
例:使用拉普拉斯邊緣信息改進(jìn)全局閾值處理
得到原圖酵母細(xì)胞的亮點(diǎn)區(qū)域,圖三是直接用于graythresh函數(shù)
clc clearf=imread('E:\桌面\數(shù)字圖像matlab\DIP3E_CH10_Original_Images\DIP3E_Original_Images_CH10\Fig1043(a)(yeast_USC).tif'); subplot(231),imshow(f),title('原圖') f=tofloat(f); subplot(232),imhist(f) [Tf,SMf]=graythresh(f); gf=im2bw(f,Tf); subplot(233),imshow(gf),title('直接otsu')%直接otsuw=[-1 -1 -1;-1 8 -1;-1 -1 -1]; lap=abs(imfilter(f,w,'replicate')); lap=lap/max(lap(:)); h=imhist(lap); Q=percentile2i(h,0.995); markerImage=lap>Q; fp=f.*markerImage; subplot(234),imshow(fp); hp=imhist(fp); hp(1)=0; subplot(235),bar(hp) T=otsuthresh(hp);%原理也是讓類間方差最大 g=im2bw(f,T); subplot(236),imshow(g)?其實(shí)上面的基于梯度的改進(jìn)與拉普拉斯類似,也能得到很好的結(jié)果。
5、使用移動(dòng)平均的圖像閾值處理
對圖像像素進(jìn)行‘己’字掃描,書上稱為zigzag模式。在每個(gè)掃描點(diǎn)處計(jì)算平均灰度
例:
對一副由斑點(diǎn)灰度模式遮蔽了的手寫文本圖像,進(jìn)行分割
clc clearf=imread('E:\桌面\數(shù)字圖像matlab\DIP3E_CH10_Original_Images\DIP3E_Original_Images_CH10\Fig1049(a)(spot_shaded_text_image).tif');subplot(131),imshow(f) T=graythresh(f); g1=im2bw(f,T); subplot(132),imshow(g1)%直接應(yīng)用otsug2=movingthresh(f,20,0.5);%移動(dòng)平均 subplot(133),imshow(g2) function g = movingthresh(f, n, K) %MOVINGTHRESH Image segmentation using a moving average threshold. % G = MOVINGTHRESH(F, n, K) segments image F by thresholding its % intensities based on the moving average of the intensities along % individual rows of the image. The average at pixel k is formed % by averaging the intensities of that pixel and its n ? 1 % preceding neighbors. To reduce shading bias, the scanning is % done in a zig-zag manner, treating the pixels as if they were a % 1-D, continuous stream. If the value of the image at a point % exceeds K percent of the value of the running average at that % point, a 1 is output in that location in G. Otherwise a 0 is % output. At the end of the procedure, G is thus the thresholded % (segmented) image. K must be a scalar in the range [0, 1]. % Preliminaries. f = tofloat(f); [M, N] = size(f); if (n < 1) || (rem(n, 1) ~= 0) error('n must be an integer >= 1.') end if K < 0 || K > 1 error('K must be a fraction in the range [0, 1].') end % Flip every other row of f to produce the equivalent of a zig-zag % scanning pattern. Convert image to a vector. %按己字掃描 f(2:2:end, :) = fliplr(f(2:2:end, :));%列翻轉(zhuǎn),假設(shè)有三列第一列與第三列互換 f = f'; % Still a matrix. f = f(:)'; % Convert to row vector for use in function filter. % Compute the moving average. maf = ones(1, n)/n; % The 1-D moving average filter. ma = filter(maf, 1, f); % Computation of moving average. %https://blog.csdn.net/qq_38559814/article/details/86521602?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-4.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-4.control % Perform thresholding. g = f > K * ma; % Go back to image format (indexed subscripts). g = reshape(g, N, M)'; % Flip alternate rows back. g(2:2:end, :) = fliplr(g(2:2:end, :));圖1是原圖,圖二是直接用otsu全局閾值處理,第三個(gè)是移動(dòng)平均
?
總結(jié)
以上是生活随笔為你收集整理的阈值法进行边缘检测matlab的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 初学servlet
- 下一篇: Unity游戏开发——新发教你做游戏(三