%图像二值化
clear;
close all;
I=imread('onion.png');
figure;
subplot(1,2,1);
imshow(I);
J=find(I<150);
I(J)=0;
J=find(I>=150);
I(J)=255;
title(' 图像二值化( 域值为150 )');
subplot(1,2,2);
imshow(I);
clc;
I=imread('1.bmp');
bw=im2bw(I,0.5);%选取阈值为0.5
bw=im2bw(I,graythresh(I));%用graythresh函数自动选取阈值
figure;
imshow(bw) %显示二值图象