分割总结

预处理:pca,海森增强
function testPca
img = imread('C0003323.jpg');
grayimg=rgb2gray(img);
figure(1);
imshow(grayimg);

r=double(img(:,:,1))/255;
g=double(img(:,:,1))/255;
b=double(img(:,:,1))/255;
[rows,columns]=size(r);

feature_r=reshape(r,rowscolumns,1);
feature_g=reshape(g,rows
columns,1);
feature_b=reshape(b,rows*columns,1);

% X=[feature_r feature_g feature_b];

X = double(reshape(img, rows * columns, 3));

coeff = pca(X);

Itransformed = X * coeff;

pca1Image = reshape(Itransformed(:,1), rows, columns);
pca2Image = reshape(Itransformed(:,2), rows, columns);
pca3Image = reshape(Itransformed(:,3), rows, columns);
figure(2);
imshow(pca1Image,[]);
figure(3);
imshow(pca2Image,[]);
figure(4);
imshow(pca3Image,[]);

2.分割:分割的本质实际上是属于基于像素层次的分类
adaboost,keans等方法可以尝试

function [mu,mask]=kmeans1(ima,k)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% kmeans image segmentation
%
% Input:
% ima: grey color image
% k: Number of classes
% Output:
% mu: vector of class means
% mask: clasification image mask
%
% Author: Jose Vicente Manjon Herrera
% Email: [email protected]
% Date: 27-08-2005
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% check image
ima=double(ima);
copy=ima; % make a copy
ima=ima(:); % vectorize ima
mi=min(ima); % deal with negative
ima=ima-mi+1; % and zero values

s=length(ima);

% create image histogram

m=max(ima)+1;
h=zeros(1,m);
hc=zeros(1,m);

for i=1:s
if(ima(i)>0) h(ima(i))=h(ima(i))+1;end;
end
ind=find(h);
hl=length(ind);

% initiate centroids

mu=(1:k)*m/(k+1);

% start process

while(true)

oldmu=mu;
% current classification

for i=1:hl
c=abs(ind(i)-mu);
cc=find(c==min(c));
hc(ind(i))=cc(1);
end

%recalculation of means

for i=1:k,
a=find(hc==i);
mu(i)=sum(a.*h(a))/sum(h(a));
end

if(mu==oldmu) break;end;

end

% calculate mask
s=size(copy);
mask=zeros(s);
for i=1:s(1),
for j=1:s(2),
c=abs(copy(i,j)-mu);
a=find(c==min(c));
mask(i,j)=a(1);
end
end

mu=mu+mi-1; % recover real range

你可能感兴趣的:(分割总结)