資源簡介
Ostu方法又名最大類間差方法,通過統計整個圖像的直方圖特性來實現全局閾值T的自動選取,其算法步驟為:
1) 先計算圖像的直方圖,即將圖像所有的像素點按照0~255共256個bin,統計落在每個bin的像素點數量
2) 歸一化直方圖,也即將每個bin中像素點數量除以總的像素點
3) i表示分類的閾值,也即一個灰度級,從0開始迭代
4) 通過歸一化的直方圖,統計0~i 灰度級的像素(假設像素值在此范圍的像素叫做前景像素) 所占整幅圖像的比例w0,并統計前景像素的平均灰度u0;統計i~255灰度級的像素(假設像素值在此范圍的像素叫做背景像素) 所占整幅圖像的比例w1,并統計背景像素的平均灰度u1;
5) 計算前景像素和背景像素的方差 g = w0*w1*(u0-u1) (u0-u1)
6) i++;轉到4),直到i為256時結束迭代
7)將最大g相應的i值作為圖像的全局閾值

代碼片段和文件信息
function?[IDXsep]?=?otsu(In)
%OTSU?Global?image?thresholding/segmentation?using?Otsu‘s?method.
%???IDX?=?OTSU(IN)?segments?the?image?I?into?N?classes?of?Otsu‘s
%???N-thresholding?method.?OTSU?returns?an?array?IDX?containing?the?cluster
%???indices?(from?1?to?N)?of?each?point.?Zero?values?are?assigned?to
%???non-finite?(NaN?or?Inf)?pixels.
%
%???IDX?=?OTSU(I)?uses?two?classes?(N=2?default?value).
%
%???[IDXsep]?=?OTSU(...)?also?returns?the?value?(sep)?of?the?separability
%???criterion?within?the?range?[0?1].?Zero?is?obtained?only?with?data
%???having?less?than?N?values?whereas?one?(optimal?value)?is?obtained?only
%???with?N-valued?arrays.
%
%???Notes:
%???-----
%???It?should?be?noticed?that?the?thresholds?generally?become?less?credible
%???as?the?number?of?classes?(N)?to?be?separated?increases?(see?Otsu‘s
%???paper?for?more?details).
%
%???If?I?is?an?RGB?image?a?Karhunen-Loeve?transform?is?first?performed?on
%???the?three?RGB?channels.?The?segmentation?is?then?carried?out?on?the
%???image?component?that?contains?most?of?the?energy.
%
%???Example:
%???-------
%???load?clown
%???subplot(221)
%???X?=?ind2rgb(Xmap);
%???imshow(X)
%???title(‘Original‘‘FontWeight‘‘bold‘)
%???for?n?=?2:4
%?????IDX?=?otsu(Xn);
%?????subplot(22n)
%?????imagesc(IDX)?axis?image?off
%?????title([‘n?=?‘?int2str(n)]‘FontWeight‘‘bold‘)
%???end
%???colormap(gray)
%
%???Reference:
%???---------
%???Otsu?N?A?Threshold?Selection?Method?from?Gray-Level?Histograms
%???IEEE?Trans.?Syst.?Man?Cybern.?9:62-66;1979
%
%???See?also?GRAYTHRESH?IM2BW
%
%???--?Damien?Garcia?--?2007/08?revised?2010/03
%???Visit?my?%???href=“matlab:web(‘http://www.biomecardio.com/matlab/otsu.html‘)“>website?for?more?details?about?OTSU
error(nargchk(12nargin))
%?Check?if?is?the?input?is?an?RGB?image
isRGB?=?isrgb(I);
assert(isRGB?|?ndims(I)==2...
????‘The?input?must?be?a?2-D?array?or?an?RGB?image.‘)
%%?Checking?n?(number?of?classes)
if?nargin==1
????n?=?2;
elseif?n==1;
????IDX?=?NaN(size(I));
????sep?=?0;
????return
elseif?n~=abs(round(n))?||?n==0
????error(‘MATLAB:otsu:WrongNValue‘...
????????‘n?must?be?a?strictly?positive?integer!‘)
elseif?n>255
????n?=?255;
????warning(‘MATLAB:otsu:TooHighN‘...
????????‘n?is?too?high.?n?value?has?been?changed?to?255.‘)
end
I?=?single(I);
%%?Perform?a?KLT?if?isRGB?and?keep?the?component?of?highest?energy
if?isRGB
????sizI?=?size(I);
????I?=?reshape(I[]3);
????[VD]?=?eig(cov(I));
????[tmpc]?=?max(diag(D));
????I?=?reshape(I*V(:c)sizI(1:2));?%?component?with?the?highest?energy
end
%%?Convert?to?256?levels
I?=?I-min(I(:));
I?=?round(I/max(I(:))*255);
%%?Probability?distribution
unI?=?sort(unique(I));
nbins?=?min(length(unI)256);
if?nbins==n
????IDX?=?ones(size(I));
????for?i?=?1:n?IDX(I==unI(i))?=?i;?end
????sep?=?1;
????return
elseif?nbins ????IDX?=?NaN(si
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????6046??2018-01-26?13:54??otsu.m
評論
共有 條評論