图像工程的一个作业、参考教材做的
使用大津阈值法对图像进行分割,彩色图片变黑白
clear all
I = imread('1.jpg');
%先把彩色图变灰度图%figure(1)
subplot(221)
imshow(I);
title('原图');
I=rgb2gray(I);
subplot(222)
imshow(I);
title('灰度图');
level = graythresh(I);
BW = imbinarize(I,level);
subplot(223)
%figure(2)
imshow(BW);
title('matlab自带函数');
IMAX = max(max(I));
IMIN = min(min(I));T=IMIN:IMAX;
ISIZE=size(I); %图像大小muxSize = ISIZE(1) * ISIZE(2);Tmp=0;%zeros(1,length(T));
mid=0;
for i=1:length(T) %从MIN到MAXTK =T(1,i);ifground=0;ibground=0;FgroundS=0;BgroundS=0;for j=1:ISIZE(1)for k=1:ISIZE(2)tmp = I(j,k);if(tmp>=TK)ifground=ifground+1;%像素个数FgroundS=FgroundS+double(tmp);%总的前景色灰度elseibground=ibground+1;Bground=BgroundS+double(tmp);endendendw0=ifground/muxSize;w1=ibground/muxSize;u0=FgroundS/ifground;u1=BgroundS/ibground;tmp=w0*w1*(u0-u1)*(u0-u1);if(Tmp
end
subplot(224)
%figure(3)
imshow(I);
title('编程实现大津阈值法');
下图是matlab自带函数处理的图像和自己编程实现的大津阈值法效果对比
大津阈值法编程实现
见目录下文件pca实现人脸识别pca.html
待写
待写
待写