Matlab:规范化割实现

Matlab:规范化割实现,matlab,image-processing,computer-vision,Matlab,Image Processing,Computer Vision,我已经实现了“标准化切割”分割。这里的变化是,我没有给出一个图像作为输入,而是给出了一个图像矩阵。因为我的输出对我来说很奇怪。我需要知道我的实现是否正确。 代码: 输出: 提前感谢。您使用的是imshow,没有任何缩放参数,因此首先要检查输出数据的范围。我不清楚这里的差异是什么意思。在MATLAB中,一切基本上都存储为一个矩阵(否则您希望如何存储图像?)。你期望什么样的产出? clear all tic; % im = imread('lena.pgm'); im =[94 122 99 101

我已经实现了“标准化切割”分割。这里的变化是,我没有给出一个图像作为输入,而是给出了一个图像矩阵。因为我的输出对我来说很奇怪。我需要知道我的实现是否正确。 代码:

输出:
提前感谢。

您使用的是
imshow
,没有任何缩放参数,因此首先要检查输出数据的范围。我不清楚这里的差异是什么意思。在MATLAB中,一切基本上都存储为一个矩阵(否则您希望如何存储图像?)。你期望什么样的产出?
clear all
tic;
% im = imread('lena.pgm');
im =[94 122 99 101 111 101;
99 92 103 87 107 116;
93 109 113 84 86 106;
5 17 6 54 56 53;
13 11 5 56 44 50;
0 10 5 49 42 51];
% resizing to avoid out of memory error
% cim=imresize(im,[100 100]);
cim=im;
[r, c]=size(cim);  
ind=find(cim);
lind=length(ind);
[I,J]=ind2sub([r,c],ind);

% % I've used linear indexing to speed up the partitioning
% vectoring the pixel nodes
for i=1:lind
       V1(i)=double(cim(ind(i)));
end

% normalizing to [0-1] scale
V=(V1./255);   

% w is the weight matrix (similarity matrix or adjacency matrix)
w=zeros(lind,lind);

% r, sigmaI, sigmaX values
rad=4.5;
sigi=10;
sigx=25;

% computing the weight matrix
for i=1:lind
    x1=I(i,1);
    y1=J(i,1);

    for j=1:lind
         if (i==j)
                 w(i,j)=1;
         else          
                 x2=I(j,1);
                 y2=J(j,1);

            dist=((x1-x2)^2 + (y1-y2)^2);  
            if sqrt(dist)>=rad
                dx=0;            
            else
                dx=exp(-((dist)/(sigx^2)));
            end

            pdiff=(V(i)-V(j))^2;
            di=exp(-((pdiff)/(sigi)^2));  
            w(i,j)=di*dx;
        end

    end
end

d=zeros(lind,lind);
s=sum(w,2);

% the diagonal matrix for computing the laplacian matrix
for i=1:lind
     d(i,i)=s(i);
end

A=zeros(lind,lind);
A=(d-w); % A is the laplacian matrix

%  vt has the eigen vectors corresponding to eigen values in vl
%  other eigs / eig functions in matlab can be used but I'm using the
%  function to compute the 5 smallest eigenvectors
[vt,vl]=eigs(A,d,5,'sm');

% se has the second smallest eigen vector, third and so on
se=vt(:,2:4);

% % % % % Simultaneous 'k' partitions
k=6;
id=kmeans(se,k);
imp=cell(1,k);
pic=cell(1,k);

for i=1:k
    imp{1,i}= find(id(:,1)==i);
    mat=zeros(100,100);
    in=imp{1,i};
    mat(in)=cim(in);
    pic{1,i}=uint8(mat);
%     figure,imshow(pic{1,i});
end

% pic has the sub graphs or partitiond sub images
figure;
subplot(2,4,1);imshow(uint8(im));title('Original image');
subplot(2,4,2);imshow(uint8(cim));title('Preprocessed image');
subplot(2,4,3);imshow(pic{1,1});title('Partition 1');
subplot(2,4,4);imshow(pic{1,2});title('Partition 2');
subplot(2,4,5);imshow(pic{1,3});title('Partition 3');
subplot(2,4,6);imshow(pic{1,4});title('Partition 4');
subplot(2,4,7);imshow(pic{1,5});title('Partition 5');
subplot(2,4,8);imshow(pic{1,6});title('Partition 6');
toc;