clear;
f0=imread('1.jpg');
f1=double(f0);
f2=(f1(:,:,1)+f1(:,:,2)+f1(:,:,3))/3; %转化为灰度图像.
subplot(2,2,1);imshow(f0);title('彩色图像');
subplot(2,2,2);imshow(uint8(f2));title('灰度图像');
[m,n]=size(f2);
for i=1:m %二值化处理
for j=1:n
if f2(i,j)<180
f(i,j)=1;
else
f(i,j)=0;
end
end
end
subplot(2,2,3);imshow(f,[0,1]);title('二值图像(黑白图像)');
x=0;
s=0;
for i=1:m
for j=1:n
x=x+j*f(i,j);
s=s+f(i,j);
end
end
x=x/s;
y=0;
for i=1:m
for j=1:n
y=y+i*f(i,j);
end
end
y=y/s;
hold on
subplot(2,2,3);plot(x,y,'+r');%质心显示
hold on
subplot(2,2,3);plot(n/2,m/2,'+b');