summaryrefslogtreecommitdiffstats
path: root/SD-VBS/common/toolbox/toolbox_basic/stella/firstncut.m
blob: a22077d138fc740e3730585406e38fdf6adc8406 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
% function [v,s,d] = firstncut(base_name,rec_num)
% Input:
%    base_name = image name
%    rec_num = parameter record number 
% Output:
%    v = eigenvectors
%    s = eigenvalues
%    d = normalization matrix d = 1/sqrt(rowsum(abs(a)))
% Convert Jianbo Shi's Ncut Ccode results from images to matlab matrices.

% Stella X. Yu, 2000.

function [v,s,d] = firstncut(base_name,rec_num);

if nargin<2 | isempty(rec_num),
   rec_num = 1;
end

cur_dir = pwd;
globalenvar;
cd(IMAGE_DIR);
cd(base_name);
   feval([base_name,'_par']);
   j = length(p);
   if rec_num>j,
      disp(sprintf('parameter record number %d out of range %d, check %s!',rec_num,j,[base_name,'_par.m']));
      Qlabel = [];
      v = [];
      s = [];
      ev_info = [];
      return;
   end
   nv = p(rec_num).num_eigvecs;
   no_rep = (p(rec_num).offset<1e-6);
   
   % read the image
   cm=sprintf('I = readppm(''%s.ppm'');',base_name);
   eval(cm);

   % read eigenvectors   
   base_name_hist = sprintf('%s_%d_IC',base_name,rec_num);
   if no_rep,
      [v,ev_info] = read_ev_pgm(base_name_hist,1,1,nv);
   else
      [v,ev_info] = read_ev_pgm2(base_name_hist,1,1,nv);
   end
   s = ev_info(4,:)';
   
   % read the normalization matrix
   d = readpfmc(sprintf('%s_%d_D_IC.pfm',base_name,rec_num));   
cd(cur_dir);

% D^(1/2)
dd = (1./(d(:)+eps));

% recover real eigenvectors
for j = 1:nv-no_rep,
   vmin = ev_info(1,j);
   vmax = ev_info(2,j);
   y = v(:,:,j).*((vmax - vmin)/256) + vmin;
   %validity check: x = D^(1/2)y should be normalized
   x = norm(y(:).*dd);
   v(:,:,j) = y./x;
end

dispimg(cat(3,mean(I,3),v),[],[{'image'};cellstr(num2str(s,3))]);