图像的二值化,就是将图像的每个像素点上的灰度值分成2个组,0或255,利用一定的手段求得分辨的阈值,我所使用的是均值减去均方差作为阈值。
具体代码如下,希望对大家有所帮助。
clear
clc
[file,path] = uigetfile('*.*');
if isequal(file,0)
disp('User selected Cancel');
else
disp(['User selected ', fullfile(path,file)]);
end
RGB = im2double(imread(file));
R = RGB(:,:,1);
G = RGB(:,:,2);
B = RGB(:,:,3);
I = (R+G+B)/3;
[A,B] = size(I);
Y1 = mean(I(:));
Y2 = var(I(:));
Y = Y1-Y2;
H = zeros(A,B);
for i = 1:A
for j = 1:B
if I(i,j) < Y
H(i,j) = 0;
else
H(i,j) = 255;
end
end
end
imshow(H)