graythresh(image)函数输入是一副图像,在图像的变化检测方向就是输入差异图(两幅图像对应位置做差或者做比,或者做对数比),输出就是阈值。在这个函数中,是使用最大类间方差法找到图片的一个合适的阈值(threshold)。再利用im2bw(将灰度图像转换为二值图像)函数,将找到的阈值输入,就可以把原图变为一个二值图。
程序示例如下:
imggray = imread('cell.bmp');
subplot(221);
imshow(imggray); title('原始图像');
imgbw = im2bw(imggray);
subplot(222); imshow(imgbw);
title( '使用默认阈值0.5');
imgbw = im2bw(imggray, 0.25);
subplot(223); imshow(imgbw); title( '指定阈值为0.25');
level = graythresh(imggray);
imgbw = im2bw(imggray,level);
subplot(224); imshow(imgbw);
title('使用最大类间方差法(Otsu)获得阈值');
由此可见, 如果不使用graythresh函数来获得阈值, 可能需要多次尝试才能获得一个合适的阈值。