1. 程式人生 > >MATLAB聚類分割程式 影象聚類分割

MATLAB聚類分割程式 影象聚類分割

clc,clear;
I=imread('egg4.bmp');
I1=I(:,:,1);
I2=I(:,:,2);
I3=I(:,:,3);
[y,x,z]=size(I);
d1=zeros(y,x);
d2=d1;
myI=double(I);
I0=zeros(y,x);
for i=1:x
    for j=1:y
     d1(j,i)=sqrt((myI(j,i,1)-10)^2+(myI(j,i,2)-10)^2+(myI(j,i,3)-10)^2) ;
     d2(j,i)=sqrt((myI(j,i,1)-180)^2+(myI(j,i,2)-180)^2+(myI(j,i,3)-180)^2) ;
     if d1(j,i)>= d2(j,i)
       I0(j,i)=1;
    end
end
end
figure(1);
subplot(221);imshow(I); title('a 原始影象');
subplot(222);imshow(I1);title('b R分量');
subplot(223);imshow(I2);title('c G分量');
subplot(224);imshow(I3);title('d B分量');
figure(2);
subplot(131);imhist(I1); title(' R分量直方圖');
subplot(132);imhist(I2); title(' G分量直方圖');
subplot(133);imhist(I3); title(' B分量直方圖');
figure(3);
subplot(221);imshow(I0);title('聚類分割後的影象');


RGB2=imadjust(RGB1,[0.6 0.8],[0 1] ,0.8);
figure(1);
subplot(221);imshow(RGB1);
subplot(222);imshow(RGB2);
I=rgb2gray(RGB2);
figure(2);
subplot(221);imshow(I);title('a 灰度影象');
subplot(222);imhist(I);title('b 直方圖');
[m n]=size(I);
for i=1:m                     % 進行灰度變化
     for j=1:n
       if I(i,j)<=10
           I(i,j)=0;
        elseif I(i,j)<=80
            I(i,j)=(255-0)/(80-10)*(I(i,j)-10)+10;
        else
           I(i,j)=255;
        end
     end
end
                      % 顯示變換後的結果
subplot(223);imshow(I);title('c 灰度變換');
I=im2double(I);
subplot(224);imhist(I);title('d 變換後直方圖');

J=imadjust(I,[0.2 0.8],[0 1],0.8);
figure(3);
subplot(221);imshow(J);title('a 對比度調整');
I2=medfilt2(J,[5 5]);
subplot(222);imshow(I2);title('b 中值濾波');
I3=decorrstretch(I2);
subplot(223);imshow(I3);title('c 去相關拉伸');

[m n]=size(I2);
for i=1:m
for j=1:n
   if I2(i,j)>0.001&I2(i,j)<0.95 I2(i,j)=1;
   else I2(i,j)=0;   
   end   
end
end

subplot(224);imshow(I2);title('d 影象二值化');