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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人工智能 > 循环神经网络 >内容正文

循环神经网络

阈值法进行边缘检测matlab

發布時間:2023/12/14 循环神经网络 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 阈值法进行边缘检测matlab 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、基于全局閾值處理

1)選擇初始閾值T

2)用T分割圖像,分別是大于T的像素集合和小于T的像素集合。

3)計算兩集合的平均灰度m1,m2

4)計算新閾值T=(m1+m2)/2

5)重復上述步驟,直至兩次迭代的T的變化小于一定值

6)利用函數im2bw分割圖像

例:

clc clear f=imread('E:\桌面\數字圖像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)

結果如下:

count=2,迭代兩次即收斂。

直方圖有兩個谷峰,很明顯可以分開,程序得到T=125,由圖可以看出來是可行的。

2、otsu方法的最佳全局閾值處理

閾值k將灰度分為兩組。

首先定義可分性度量,即類間方差與圖像灰度方差的比。越大說明可分性越好。

工具箱函數graythresh,[T,SM]=graythresh(f)

例:

clc clear f=imread('E:\桌面\數字圖像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)

?結果如下:

第一張圖是利用第一種方法得到的,第二個是otsu方法得到了更好的分割效果。

?

?還有一種基于圖像直方圖給出閾值的函數otsuthresh。

3、使用圖像平滑改進全局閾值

當圖像含有噪聲時,進行圖像平滑(濾波)再進行分割會更好。

例:

clc clear f=imread('E:\桌面\數字圖像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)%直接進行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)

結果如下:

進行濾波之后,圖像可分性變得更強了。

?4、使用邊緣改進全局閾值

例:使用基于梯度的邊緣信息改進全局閾值

(通過選擇梯度較大的像素點,也就是邊緣,然后在進行全局閾值處理)

clc clearf=imread('E:\桌面\數字圖像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;%因為大部分是0值,所以把0的統計數去掉 subplot(235),bar(hp) T=otsuthresh(hp);%以直方圖作為輸入 T*(numel(hp)-1)g=im2bw(f,T);%大于T的為1,小于為0,得到二值圖像 subplot(236),imshow(g)

結果如下:

? ? ? ?進行梯度選擇后的圖像,可分性變強。

? ? ? ?這個例子中中間白色物體的大小比背景小的多,對直方圖的貢獻很小,所以如果運用上面的平均值濾波,得不到想要的結果,只能考慮邊緣信息。

例:使用拉普拉斯邊緣信息改進全局閾值處理

得到原圖酵母細胞的亮點區域,圖三是直接用于graythresh函數

clc clearf=imread('E:\桌面\數字圖像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)

?其實上面的基于梯度的改進與拉普拉斯類似,也能得到很好的結果。

5、使用移動平均的圖像閾值處理

對圖像像素進行‘己’字掃描,書上稱為zigzag模式。在每個掃描點處計算平均灰度

例:

對一副由斑點灰度模式遮蔽了的手寫文本圖像,進行分割

clc clearf=imread('E:\桌面\數字圖像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)%直接應用otsug2=movingthresh(f,20,0.5);%移動平均 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, :));%列翻轉,假設有三列第一列與第三列互換 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全局閾值處理,第三個是移動平均

?

總結

以上是生活随笔為你收集整理的阈值法进行边缘检测matlab的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。