Tuesday, May 19, 2009

Matlab: Window based Otsu thresholding

This function divides the gray level image into a predefined set of subimages (This can be controlled). Calculates the otsu threshold on each subimage and threshold it accordingly. The function returns a BW image that is thresholded according to window based otsu thresholding.

function IMBw = LocalOtsuThresh( IMGray, NW )

[M N] = size( IMGray );
IMBw = zeros(M, N);

if nargin < 2
NW = 4;
end

for m = 1:NW
for n = 1:NW
IMTmp = uint8(IMGray( 1+(m-1)*floor(M/NW):m*floor(M/NW),...
1+(n-1)*floor(N/NW):n*floor(N/NW) ));
IMTmpBw = im2bw( IMTmp, graythresh( IMTmp ) );

IMBw( 1+(m-1)*floor(M/NW):m*floor(M/NW),...
1+(n-1)*floor(N/NW):n*floor(N/NW) ) = IMTmpBw;
end
end

NW defines the number of subimages. Default is 4. So in default case the gray level image will be divided into 16 subimages.

1 comment:

  1. ça marche pas avec moi il y'a un erreur in:

    IMTmp = uint8(IMGray( 1+(m-1)*floor(M/NW):m*floor(M/NW),...
    1+(n-1)*floor(N/NW):n*floor(N/NW) ));

    pourquoi?

    ReplyDelete

Followers