diff options
Diffstat (limited to 'SD-VBS/common/toolbox/toolbox_basic/stella')
14 files changed, 790 insertions, 0 deletions
diff --git a/SD-VBS/common/toolbox/toolbox_basic/stella/afromncut.m b/SD-VBS/common/toolbox/toolbox_basic/stella/afromncut.m new file mode 100755 index 0000000..ec014d0 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/stella/afromncut.m | |||
@@ -0,0 +1,73 @@ | |||
1 | % function a = afromncut(v,s,d,visimg,no_rep,pixel_loc) | ||
2 | % Input: | ||
3 | % v = eigenvectors of d*a*d, starting from the second. | ||
4 | % (the first is all one over some constant determined by d) | ||
5 | % s = eigenvalues | ||
6 | % d = normalization matrix 1/sqrt(rowsum(abs(a))) | ||
7 | % visimg = 1/0 if each eigenvector is/not 2D (so v is 3D) | ||
8 | % no_rep = 1 (default), affinity has attraction only | ||
9 | % if 1, the first column of v is the second eigenvector | ||
10 | % if 0, the first column of v is the first eigenvector. | ||
11 | % pixel_loc = nx1 matrix, each is a pixel location | ||
12 | % Output: | ||
13 | % a = diag(1/d) * na * diag(1/d); | ||
14 | % If pixel_loc = []; a is returned, if not out of memory | ||
15 | % otherwise, only rows of a at pixel_loc are returned. | ||
16 | % | ||
17 | % This routine is used to estimate the original affinity matrix | ||
18 | % through the first few eigenvectors and its normalization matrix. | ||
19 | |||
20 | % A test sequence includes: | ||
21 | % a = randsym(5); | ||
22 | % [na,d] = normalize(a); | ||
23 | % [v,s] = ncut(a,5); | ||
24 | % v = v(:,2:end); s = s(2:end); | ||
25 | % aa = afromncut(v,s,d); | ||
26 | % max(abs(aa(:) - a(:))) | ||
27 | |||
28 | % Stella X. Yu, 2000. | ||
29 | |||
30 | function a = afromncut(v,s,d,visimg,no_rep,pixel_loc) | ||
31 | |||
32 | [nr,nc,nv] = size(v); | ||
33 | if nargin<4 | isempty(visimg), | ||
34 | visimg = (nv>1); | ||
35 | end | ||
36 | |||
37 | if nargin<5 | isempty(no_rep), | ||
38 | no_rep = 1; | ||
39 | end | ||
40 | |||
41 | if visimg, | ||
42 | nr = nr * nc; | ||
43 | else | ||
44 | nv = nc; | ||
45 | end | ||
46 | |||
47 | if nargin<6 | isempty(pixel_loc), | ||
48 | pixel_loc = 1:nr; | ||
49 | end | ||
50 | |||
51 | % D^(1/2) | ||
52 | d = 1./(d(:)+eps); | ||
53 | |||
54 | % first recover the first eigenvector | ||
55 | if no_rep, | ||
56 | u = (1/norm(d)) + zeros(nr,1); | ||
57 | s = [1;s(:)]; | ||
58 | nv = nv + 1; | ||
59 | else | ||
60 | u = []; | ||
61 | end | ||
62 | |||
63 | % the full set of generalized eigenvectors | ||
64 | v = [u, reshape(v,[nr,nv-no_rep])]; | ||
65 | |||
66 | % This is the real D, row sum | ||
67 | d = d.^2; | ||
68 | |||
69 | % an equivalent way to compute v = diag(d) * v; | ||
70 | v = v .* d(:,ones(nv,1)); % to avoid using a big matrix diag(d) | ||
71 | |||
72 | % synthesis | ||
73 | a = v(pixel_loc,:)*diag(s)*v'; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/stella/dispimg.m b/SD-VBS/common/toolbox/toolbox_basic/stella/dispimg.m new file mode 100755 index 0000000..4e419a0 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/stella/dispimg.m | |||
@@ -0,0 +1,65 @@ | |||
1 | % function dispimg(g,fmt,lgd,cmap) display multiple images in one figure. | ||
2 | % Input: | ||
3 | % g = a cell and fmt is a 1x2 vector specifying the layout. | ||
4 | % lgd = a string cell for the title of each image. | ||
5 | % cmap = the colormap (default is the gray, -1 for the inverted gray). | ||
6 | % ishori = a vector of 1/0 to display real and imag parts horizontally / vertically | ||
7 | |||
8 | % Stella X. Yu, 2000. | ||
9 | |||
10 | function dispimg(g,fmt,lgd,cmap,ishori); | ||
11 | |||
12 | cellg = iscell(g); | ||
13 | if cellg, | ||
14 | num_fig = length(g); | ||
15 | else | ||
16 | num_fig = size(g,3); | ||
17 | end; | ||
18 | |||
19 | if nargin<2 | isempty(fmt), | ||
20 | m = ceil(sqrt(num_fig)); | ||
21 | n = ceil(num_fig / m); | ||
22 | else | ||
23 | m = fmt(1); | ||
24 | n = fmt(2); | ||
25 | end | ||
26 | |||
27 | if nargin<3 | isempty(lgd), | ||
28 | lgd = 1:num_fig; | ||
29 | end | ||
30 | if isnumeric(lgd), | ||
31 | lgd = cellstr(num2str(lgd(:),3)); | ||
32 | end | ||
33 | i = size(lgd); | ||
34 | if i(1)==1, | ||
35 | lgd = [lgd, cell(1,num_fig-i(2))]; | ||
36 | else | ||
37 | lgd = [lgd; cell(num_fig-i(1),1)]; | ||
38 | end | ||
39 | |||
40 | if nargin<5 | isempty(ishori), | ||
41 | ishori = ones(num_fig,1); | ||
42 | end | ||
43 | ishori(end+1:num_fig) = ishori(end); | ||
44 | |||
45 | for k=1:num_fig, | ||
46 | subplot(m,n,k); | ||
47 | if cellg, | ||
48 | showim(g{k},[],ishori(k)); | ||
49 | else | ||
50 | showim(g(:,:,k),[],ishori(k)); | ||
51 | end | ||
52 | title(lgd{k}); | ||
53 | end | ||
54 | |||
55 | if nargin<4 | isempty(cmap), | ||
56 | cmap = gray; | ||
57 | end | ||
58 | if length(cmap)==1, | ||
59 | if cmap==1, | ||
60 | cmap = gray; | ||
61 | else | ||
62 | cmap = flipud(gray); | ||
63 | end | ||
64 | end | ||
65 | colormap(cmap); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/stella/firstncut.m b/SD-VBS/common/toolbox/toolbox_basic/stella/firstncut.m new file mode 100755 index 0000000..a22077d --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/stella/firstncut.m | |||
@@ -0,0 +1,67 @@ | |||
1 | % function [v,s,d] = firstncut(base_name,rec_num) | ||
2 | % Input: | ||
3 | % base_name = image name | ||
4 | % rec_num = parameter record number | ||
5 | % Output: | ||
6 | % v = eigenvectors | ||
7 | % s = eigenvalues | ||
8 | % d = normalization matrix d = 1/sqrt(rowsum(abs(a))) | ||
9 | % Convert Jianbo Shi's Ncut Ccode results from images to matlab matrices. | ||
10 | |||
11 | % Stella X. Yu, 2000. | ||
12 | |||
13 | function [v,s,d] = firstncut(base_name,rec_num); | ||
14 | |||
15 | if nargin<2 | isempty(rec_num), | ||
16 | rec_num = 1; | ||
17 | end | ||
18 | |||
19 | cur_dir = pwd; | ||
20 | globalenvar; | ||
21 | cd(IMAGE_DIR); | ||
22 | cd(base_name); | ||
23 | feval([base_name,'_par']); | ||
24 | j = length(p); | ||
25 | if rec_num>j, | ||
26 | disp(sprintf('parameter record number %d out of range %d, check %s!',rec_num,j,[base_name,'_par.m'])); | ||
27 | Qlabel = []; | ||
28 | v = []; | ||
29 | s = []; | ||
30 | ev_info = []; | ||
31 | return; | ||
32 | end | ||
33 | nv = p(rec_num).num_eigvecs; | ||
34 | no_rep = (p(rec_num).offset<1e-6); | ||
35 | |||
36 | % read the image | ||
37 | cm=sprintf('I = readppm(''%s.ppm'');',base_name); | ||
38 | eval(cm); | ||
39 | |||
40 | % read eigenvectors | ||
41 | base_name_hist = sprintf('%s_%d_IC',base_name,rec_num); | ||
42 | if no_rep, | ||
43 | [v,ev_info] = read_ev_pgm(base_name_hist,1,1,nv); | ||
44 | else | ||
45 | [v,ev_info] = read_ev_pgm2(base_name_hist,1,1,nv); | ||
46 | end | ||
47 | s = ev_info(4,:)'; | ||
48 | |||
49 | % read the normalization matrix | ||
50 | d = readpfmc(sprintf('%s_%d_D_IC.pfm',base_name,rec_num)); | ||
51 | cd(cur_dir); | ||
52 | |||
53 | % D^(1/2) | ||
54 | dd = (1./(d(:)+eps)); | ||
55 | |||
56 | % recover real eigenvectors | ||
57 | for j = 1:nv-no_rep, | ||
58 | vmin = ev_info(1,j); | ||
59 | vmax = ev_info(2,j); | ||
60 | y = v(:,:,j).*((vmax - vmin)/256) + vmin; | ||
61 | %validity check: x = D^(1/2)y should be normalized | ||
62 | x = norm(y(:).*dd); | ||
63 | v(:,:,j) = y./x; | ||
64 | end | ||
65 | |||
66 | dispimg(cat(3,mean(I,3),v),[],[{'image'};cellstr(num2str(s,3))]); | ||
67 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/stella/getfnames.m b/SD-VBS/common/toolbox/toolbox_basic/stella/getfnames.m new file mode 100755 index 0000000..4990451 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/stella/getfnames.m | |||
@@ -0,0 +1,47 @@ | |||
1 | % function [fn,dn] = getfnames(direc,opt) | ||
2 | % Input: | ||
3 | % direc = directory | ||
4 | % opt = wildcat | ||
5 | % Output: | ||
6 | % fn = a cell with all filenames under direc and with opt | ||
7 | % dn = a cell with all directory names under direc and with opt | ||
8 | % For example, getfnames('19990910','*.jpg'); | ||
9 | % Set IS_PC according to your platform in globalenvar.m | ||
10 | |||
11 | % Stella X. Yu, 2000. | ||
12 | |||
13 | function [fn,dn] = getfnames(direc,opt) | ||
14 | |||
15 | if (nargin<1 | isempty(direc)), | ||
16 | direc = '.'; | ||
17 | end | ||
18 | |||
19 | if nargin<2 | isempty(opt), | ||
20 | opt = []; | ||
21 | end | ||
22 | |||
23 | fn = {}; | ||
24 | dn = {}; | ||
25 | |||
26 | cur_dir = pwd; | ||
27 | cd(direc); | ||
28 | s = dir(opt); | ||
29 | if isempty(s), | ||
30 | disp('getfnames: no data'); | ||
31 | return; | ||
32 | end | ||
33 | |||
34 | n = length(s); | ||
35 | i = 1; | ||
36 | j = 1; | ||
37 | for k=1:n, | ||
38 | if s(k).isdir, | ||
39 | dn{j,1} = s(k).name; | ||
40 | j = j + 1; | ||
41 | else | ||
42 | fn{i,1} = s(k).name; | ||
43 | i = i + 1; | ||
44 | end | ||
45 | end | ||
46 | cd(cur_dir) | ||
47 | %[fn{1:n,1}]=deal(s.name); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/stella/getimage2.m b/SD-VBS/common/toolbox/toolbox_basic/stella/getimage2.m new file mode 100755 index 0000000..945ddd2 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/stella/getimage2.m | |||
@@ -0,0 +1,46 @@ | |||
1 | % function f = getimage2(imagefile) returns a normalized intensity image. | ||
2 | % If the file postfix is not given, then I will search any possible image file | ||
3 | % under the IMAGE_DIR. | ||
4 | |||
5 | % Stella X. Yu, March 1999 | ||
6 | |||
7 | function f = getimage2(imagefile) | ||
8 | |||
9 | if exist(imagefile)==2, | ||
10 | g = {imagefile}; | ||
11 | else | ||
12 | g = {}; | ||
13 | end | ||
14 | globalenvar; | ||
15 | g = [g; getfnames(IMAGE_DIR,[imagefile,'.*'])]; | ||
16 | |||
17 | j = 1; | ||
18 | for i=1:length(g), | ||
19 | k = findstr(g{i},'.'); | ||
20 | gp = g{i}(k(end)+1:end); | ||
21 | if strcmp(gp,'ppm'), | ||
22 | f = double(readppm(g{i})); | ||
23 | j = 0; | ||
24 | elseif sum(strcmp(gp,{'jpg','tif','jpeg','tiff','bmp','png','hdf','pcx','xwd'}))>0, | ||
25 | f = double(imread(g{i})); | ||
26 | j = 0; | ||
27 | end | ||
28 | if j==0, | ||
29 | disp(sprintf('This is an image read from %s under %s',g{i},IMAGE_DIR)); | ||
30 | break; | ||
31 | end | ||
32 | end | ||
33 | if j, | ||
34 | f = []; | ||
35 | disp('Image not found'); | ||
36 | return; | ||
37 | end | ||
38 | |||
39 | if size(f,3)>1, | ||
40 | %f = sum(f,3)./3; | ||
41 | f = rgb2ntsc(f); | ||
42 | f = f(:,:,1); | ||
43 | end | ||
44 | minf = min(f(:)); | ||
45 | maxf = max(f(:)); | ||
46 | f = (f - minf) ./ (maxf - minf); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/stella/globalenvar.m b/SD-VBS/common/toolbox/toolbox_basic/stella/globalenvar.m new file mode 100755 index 0000000..1e61b61 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/stella/globalenvar.m | |||
@@ -0,0 +1,6 @@ | |||
1 | % globalenvar | ||
2 | |||
3 | HOME_DIR = '/hid/jshi/Research/walking/stella'; | ||
4 | IMAGE_DIR = '/hid/jshi/test_images'; | ||
5 | C_DIR = '/hid/jshi/Research/Ncut_code_C'; | ||
6 | IS_PC = 0; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/stella/jshincut.m b/SD-VBS/common/toolbox/toolbox_basic/stella/jshincut.m new file mode 100755 index 0000000..d0f11cb --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/stella/jshincut.m | |||
@@ -0,0 +1,94 @@ | |||
1 | % function [par, rec_num] = jshincut(par,image_dir) | ||
2 | % Input: | ||
3 | % par = a structure with parameters for command_ncut.tex | ||
4 | % image_dir = the directory where the imagefile is stored | ||
5 | % Output: | ||
6 | % par = parameters used | ||
7 | % rec_num = record number in the NCut database | ||
8 | % Jianbo Shi's ncut_IC is applied to the image | ||
9 | % (If there is no .ppm format for the named image, | ||
10 | % conversion from related files would be attempted.) | ||
11 | % Results are organized according to the parameters. | ||
12 | % Example: jshincut('240018s'); | ||
13 | % See also: jshincutdefpar; ncutcheckin | ||
14 | % Set IS_PC according to your platform in globalenvar.m | ||
15 | |||
16 | % Stella X. Yu, 2000. | ||
17 | |||
18 | function [par,rec_num] = jshincut(par,image_dir) | ||
19 | |||
20 | rec = jshincutdefpar; | ||
21 | |||
22 | fields = fieldnames(rec); | ||
23 | nf = length(fields); | ||
24 | |||
25 | if ischar(par), | ||
26 | imagename = par; | ||
27 | par = rec; | ||
28 | par.fname_base = imagename; | ||
29 | end | ||
30 | |||
31 | globalenvar; | ||
32 | |||
33 | if nargin<2 | isempty(image_dir), | ||
34 | image_dir = IMAGE_DIR; | ||
35 | end | ||
36 | |||
37 | imagename = getfield(par,fields{1}); | ||
38 | for i=2:nf, | ||
39 | t = getfield(par,fields{i}); | ||
40 | if isempty(t), | ||
41 | par = setfield(par,fields{i},getfield(rec,fields{i})); | ||
42 | end | ||
43 | end | ||
44 | |||
45 | % dir and filename | ||
46 | catchar = {'/','\'}; | ||
47 | catchar = catchar{IS_PC+1}; | ||
48 | |||
49 | % first check if there is a ppm file for this image | ||
50 | if not(exist([image_dir,catchar,imagename,'.ppm'])), | ||
51 | j = getfnames(image_dir,[imagename,'.*']); | ||
52 | if isempty(j), | ||
53 | disp('Image not found.'); | ||
54 | return; | ||
55 | end | ||
56 | k = 0; | ||
57 | for i=1:length(j), | ||
58 | k = k + not(isempty(im2ppm(j{i},image_dir))); | ||
59 | if k==1, | ||
60 | disp(sprintf('%s -> %s.ppm succeeded.',j{i},imagename)); | ||
61 | break; | ||
62 | end | ||
63 | end | ||
64 | if k==0, | ||
65 | disp('Sorry. Attempt to convert your named image into ppm format failed.'); | ||
66 | return; | ||
67 | end | ||
68 | end | ||
69 | |||
70 | cd(C_DIR); | ||
71 | |||
72 | % generate command_ncut.tex file | ||
73 | fn = 'command_ncut.tex'; | ||
74 | fid = fopen(fn,'w'); | ||
75 | fprintf(fid,'%21s\t%s%c%s\n',fields{1},image_dir,catchar,imagename); | ||
76 | for i=2:nf, | ||
77 | t = getfield(par,fields{i}); | ||
78 | if isnumeric(t), | ||
79 | t = num2str(t); | ||
80 | end | ||
81 | fprintf(fid,['%21s\t%s\n'],fields{i},t); | ||
82 | end | ||
83 | fclose(fid); | ||
84 | %disp('You can check and modify command_ncut.tex before I run ncut_IC on it. Good?');pause(1); | ||
85 | |||
86 | % run ncut_IC | ||
87 | unix(['.',catchar,'ncut_IC']); | ||
88 | cd(HOME_DIR); | ||
89 | |||
90 | % check in | ||
91 | copyfile([C_DIR,catchar,fn],[image_dir,catchar,fn]); | ||
92 | rec_num = ncutcheckin(fn,image_dir,image_dir); | ||
93 | %delete([image_dir,catchar,imagename,'.ppm']); | ||
94 | %delete([image_dir,catchar,fn]); | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/stella/jshincutdefpar.m b/SD-VBS/common/toolbox/toolbox_basic/stella/jshincutdefpar.m new file mode 100755 index 0000000..4f07192 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/stella/jshincutdefpar.m | |||
@@ -0,0 +1,20 @@ | |||
1 | % function rec = jshincutdefpar; | ||
2 | % default parameter setting for Jianbo Shi's NCut C codes | ||
3 | |||
4 | % Stella X. Yu, 2000. | ||
5 | |||
6 | function rec = jshincutdefpar; | ||
7 | |||
8 | rec.fname_base = '240018s'; | ||
9 | rec.fname_ext = 'ppm'; | ||
10 | rec.num_eigvecs = 15; | ||
11 | rec.spatial_neighborhood_x=20; | ||
12 | rec.sigma_x= 10; | ||
13 | rec.sig_I= -0.16; | ||
14 | rec.sig_IC= 0.01; | ||
15 | rec.hr= 2; | ||
16 | rec.eig_blk_sze= 3; | ||
17 | rec.power_D= 1; | ||
18 | rec.offset = 0.0; | ||
19 | rec.sig_filter = 1.0; | ||
20 | rec.elong_filter = 3.0; | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/stella/ncutcheckin.m b/SD-VBS/common/toolbox/toolbox_basic/stella/ncutcheckin.m new file mode 100755 index 0000000..cd82ee5 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/stella/ncutcheckin.m | |||
@@ -0,0 +1,136 @@ | |||
1 | % function rec_num = ncutcheckin(fn,sdir,tdir) | ||
2 | % Input: | ||
3 | % fn = parameter file name, default = 'command_ncut.tex' | ||
4 | % sdir = source dir for fn as well as data files | ||
5 | % tdir = target dir to check in, both default = IMAGE_DIR | ||
6 | % Output: | ||
7 | % rec_num = the number of current parameter records | ||
8 | % The imagefile_par.m is updated if fn contains a new | ||
9 | % parameter set. Data files are tagged and copied over to | ||
10 | % a subdir under tdir. | ||
11 | % Example: ncutcheckin; | ||
12 | % Set IS_PC, IMAGE_DIR according to your platform in globalenvar.m | ||
13 | |||
14 | % Stella X. Yu, 2000. | ||
15 | |||
16 | function rec_num = ncutcheckin(fn,sdir,tdir) | ||
17 | |||
18 | globalenvar; | ||
19 | |||
20 | cur_dir = pwd; | ||
21 | |||
22 | if nargin<1 | isempty(fn), | ||
23 | fn = 'command_ncut.tex'; | ||
24 | end | ||
25 | |||
26 | if nargin<2 | isempty(sdir), | ||
27 | sdir = IMAGE_DIR; | ||
28 | end | ||
29 | |||
30 | if nargin<3 | isempty(tdir), | ||
31 | tdir = IMAGE_DIR; | ||
32 | end | ||
33 | |||
34 | rec = jshincutdefpar; | ||
35 | |||
36 | % first, generate a parameter record from fn | ||
37 | cd(sdir); | ||
38 | [names,values] = textread(fn,'%s %s','commentstyle','shell'); | ||
39 | n = length(names); | ||
40 | s = rec; | ||
41 | for i=1:n, | ||
42 | j = str2num(values{i}); | ||
43 | if isempty(j), | ||
44 | s = setfield(s,names{i},values{i}); | ||
45 | else | ||
46 | s = setfield(s,names{i},j); | ||
47 | end | ||
48 | end | ||
49 | cd(cur_dir); | ||
50 | |||
51 | % special care to extract the image file name | ||
52 | imagename = getfield(s,names{1}); | ||
53 | catchar = {'/','\'}; | ||
54 | catchar = catchar{IS_PC + 1}; | ||
55 | k = max([0,findstr(imagename,catchar)]); | ||
56 | imagename = imagename(k+1:end); | ||
57 | s = setfield(s,names{1},imagename); | ||
58 | |||
59 | % second, check if the target dir contains a profile for the image | ||
60 | cd(tdir); | ||
61 | if not(exist(imagename,'dir')), | ||
62 | mkdir(imagename); | ||
63 | cd(cur_dir); | ||
64 | j = [catchar,imagename,'.',getfield(s,names{2})]; | ||
65 | copyfile([sdir,j],[tdir,catchar,imagename,j]); | ||
66 | cd(tdir); | ||
67 | end | ||
68 | cd(imagename); | ||
69 | j = [imagename,'_par']; | ||
70 | if not(exist(j)), | ||
71 | rec_num = 1; | ||
72 | p = s; | ||
73 | else | ||
74 | % load par file | ||
75 | feval(j); | ||
76 | rec_num = length(p); | ||
77 | i = 1; | ||
78 | while (i<=rec_num), | ||
79 | k = 0; | ||
80 | for j=1:n, | ||
81 | k = k + sum(getfield(s,names{j})-getfield(p(i),names{j})); | ||
82 | end | ||
83 | if k==0, | ||
84 | if not(isempty(input(['Data already existed as record # ',num2str(i),... | ||
85 | '\nPress any non-return key to Overwrite'],'s'))), | ||
86 | break; | ||
87 | else | ||
88 | rec_num = i; % have checked in already, no update | ||
89 | cd(cur_dir); | ||
90 | return; | ||
91 | end | ||
92 | else | ||
93 | i = i + 1; | ||
94 | end | ||
95 | end | ||
96 | rec_num = i; % new parameter setting | ||
97 | p(rec_num)=s; | ||
98 | end | ||
99 | tdir = [tdir,catchar,imagename]; | ||
100 | cd(cur_dir); | ||
101 | |||
102 | % third, check in data files | ||
103 | % leave .ppm and _edgecon, _phase files | ||
104 | % if not(exist([tdir,catchar,imagename,'.ppm'])), | ||
105 | % copyfile([sdir,catchar,imagename,'.ppm'],[tdir,catchar,imagename,'.ppm']); | ||
106 | % end | ||
107 | |||
108 | % IC files only | ||
109 | dn = getfnames(sdir,[imagename,'*_IC*.*']); | ||
110 | header = sprintf('%s%c%s_%d_',tdir,catchar,imagename,rec_num); | ||
111 | j = length(imagename)+2; | ||
112 | k = length(dn); | ||
113 | for i=1:k, | ||
114 | copyfile([sdir,catchar,dn{i}],[header,dn{i}(j:end)]); | ||
115 | delete([sdir,catchar,dn{i}]); | ||
116 | end | ||
117 | disp(sprintf('%d files checked in as record #%d',k,rec_num)); | ||
118 | |||
119 | |||
120 | % finally, update parameter file | ||
121 | cd(tdir); | ||
122 | fid = fopen([imagename,'_par.m'],'w'); | ||
123 | fprintf(fid,'%% Last checked in at %s\n\n',datestr(now)); | ||
124 | for i=1:rec_num, | ||
125 | for j=1:n, | ||
126 | k = getfield(p(i),names{j}); | ||
127 | if ischar(k), | ||
128 | fprintf(fid,'p(%d).%s=\''%s\'';\n',i,names{j},k); | ||
129 | else | ||
130 | fprintf(fid,'p(%d).%s=%s;\n',i,names{j},num2str(k)); | ||
131 | end | ||
132 | end | ||
133 | fprintf(fid,'\n'); | ||
134 | end | ||
135 | fclose(fid); | ||
136 | cd(cur_dir); \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/stella/openfigure.m b/SD-VBS/common/toolbox/toolbox_basic/stella/openfigure.m new file mode 100755 index 0000000..e677014 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/stella/openfigure.m | |||
@@ -0,0 +1,52 @@ | |||
1 | % function openfigure(m,n,caption,isnew) | ||
2 | function h = openfigure(m,n,caption,isnew) | ||
3 | |||
4 | if nargin<3, | ||
5 | caption = ' '; | ||
6 | end | ||
7 | |||
8 | if nargin<4, | ||
9 | isnew = 1; | ||
10 | end | ||
11 | |||
12 | if (m<=0 | n<=0) | ||
13 | return; | ||
14 | end | ||
15 | |||
16 | if isnew, | ||
17 | h = figure; colormap(gray); | ||
18 | else | ||
19 | h = gcf; | ||
20 | end | ||
21 | clf | ||
22 | |||
23 | subplot('position',[0,0,0.1,0.1]); axis off; | ||
24 | text(0.1,0.15,sprintf('S. X. Yu, %s',date),'FontSize',8); | ||
25 | |||
26 | subplot('position',[0,0.96,0.1,0.1]); axis off; | ||
27 | text(0.1,0.15,caption,'FontSize',8); | ||
28 | |||
29 | subplot(m,n,1); | ||
30 | %return | ||
31 | |||
32 | if (m==1 & n==1), | ||
33 | return; | ||
34 | end | ||
35 | |||
36 | %set(gcf,'PaperPosition',[0.25, 8, 8,2.5*m]); | ||
37 | % set(gcf,'PaperPosition',[0.25,0.25,8,10.5]); | ||
38 | %return | ||
39 | |||
40 | if (m<=n), | ||
41 | set(gcf,'PaperOrientation','landscape','PaperPosition',[0.25,0.25,10.5,8]); | ||
42 | else | ||
43 | set(gcf,'PaperPosition',[0.25,0.25,8,10.5]); | ||
44 | end | ||
45 | |||
46 | % comment on PaperPosition | ||
47 | % [a,b,c,d] | ||
48 | % (a,b) is the coordinate of the lower-left corner of the figure | ||
49 | % (a,b) = (0,0) is the lower-left corner of the paper | ||
50 | % (c,d) is the coordinate of the upper-right corner of the figure relative to the lower-left corner of the figure | ||
51 | % Therefore, c>=a, d>=b | ||
52 | % Full paper position would be [0,0,8.5,11] in inches | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/stella/showim.m b/SD-VBS/common/toolbox/toolbox_basic/stella/showim.m new file mode 100755 index 0000000..10db297 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/stella/showim.m | |||
@@ -0,0 +1,36 @@ | |||
1 | % function showim(f,cmap) display real or complex image. | ||
2 | % When it is complex, the real part and imaginary part | ||
3 | % are displayed as [real,imag] in one image. | ||
4 | % cmap is the colormap. default = gray, -1 = inverted gray. | ||
5 | |||
6 | % Stella X. Yu, 2000. | ||
7 | |||
8 | function showim(f,cmap,ishori) | ||
9 | |||
10 | if not(isreal(f)), | ||
11 | i = [real(f(:)); imag(f(:))]; | ||
12 | j = [min(i), max(i)]; | ||
13 | [nr,nc] = size(f); | ||
14 | if nargin<3 | isempty(ishori), | ||
15 | ishori = nr>nc; | ||
16 | end | ||
17 | if ishori, | ||
18 | i = zeros(nr,1); | ||
19 | f = [real(f), [i+j(1),i+j(2)], imag(f)]; | ||
20 | else | ||
21 | i = zeros(1,nc); | ||
22 | f = [real(f); [i+j(1);i+j(2)]; imag(f)]; | ||
23 | end | ||
24 | end | ||
25 | imagesc(f); axis off; axis image; | ||
26 | |||
27 | if nargin<2 | isempty(cmap), | ||
28 | return; | ||
29 | end | ||
30 | |||
31 | if cmap==1, | ||
32 | cmap = gray; | ||
33 | elseif cmap==-1, | ||
34 | cmap = flipud(gray); | ||
35 | end | ||
36 | colormap(cmap); \ No newline at end of file | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/stella/showncut.m b/SD-VBS/common/toolbox/toolbox_basic/stella/showncut.m new file mode 100755 index 0000000..b1fe1f4 --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/stella/showncut.m | |||
@@ -0,0 +1,92 @@ | |||
1 | % function [g,lgd,v,s,dd] = showncut(fn,rec_num) | ||
2 | % Input: | ||
3 | % fn = file / image name | ||
4 | % rec_num = Ncut record number | ||
5 | % Output: | ||
6 | % g = a cell contains 1D, 2D and 3D embeddings | ||
7 | % lgd = legend for g | ||
8 | % v = eigenvectors | ||
9 | % s = eigenvalues | ||
10 | % dd = normalization matrix = 1/sqrt(rowsum(abs(a))) | ||
11 | % an image is displayed | ||
12 | |||
13 | function [g,lgd,v,s,dd] = showncut(fn,rec_num) | ||
14 | |||
15 | globalenvar; cd(IMAGE_DIR);cd(fn); feval([fn,'_par']);cd(HOME_DIR); | ||
16 | par = p(rec_num); | ||
17 | no_rep = (par.offset<1e-6); | ||
18 | |||
19 | [v,s,dd] = firstncut(fn,rec_num); | ||
20 | [m,n,nc] = size(v); | ||
21 | |||
22 | % generate images for display | ||
23 | nr = 5; | ||
24 | num_plots = nc * nr; | ||
25 | g = cell(num_plots,1); | ||
26 | lgd = g; | ||
27 | names = {'r','\theta','\phi'}; | ||
28 | x = cell(3,1); | ||
29 | for j=1:nc, | ||
30 | g{j} = v(:,:,j); | ||
31 | lgd{j} = sprintf('%s_{%d} = %1.2f','\lambda', j+no_rep, s(j)); | ||
32 | |||
33 | if j<nc, | ||
34 | [x{2},x{1}] = cart2pol(v(:,:,j),v(:,:,j+1)); | ||
35 | k = j; | ||
36 | for t=1:2, | ||
37 | k = k + nc; | ||
38 | g{k} = x{t}; | ||
39 | lgd{k} = sprintf('%s_{%d,%d}',names{t},j+[0:1]+no_rep); | ||
40 | end | ||
41 | |||
42 | if j<nc-1, | ||
43 | [x{2},x{3},x{1}] = cart2sph(v(:,:,j),v(:,:,j+1),v(:,:,j+2)); | ||
44 | for t=[1,3], % theta must be the same as 2D embedding, so ignore it | ||
45 | k = k + nc; | ||
46 | g{k} = x{t}; | ||
47 | lgd{k} = sprintf('%s_{%d,%d,%d}',names{t},j+[0:2]+no_rep); | ||
48 | end | ||
49 | end | ||
50 | end | ||
51 | end | ||
52 | |||
53 | % fill in slots by image f and affinity pattern | ||
54 | j = nc + nc; g{j} = getimage2(fn); lgd{j} = sprintf('%d x %d image',m,n); | ||
55 | j = nr * nc; g{j} = readpcm([fn,'_phase.pfm']); lgd{j} = 'phase'; | ||
56 | j = j - 1; g{j} = exp(-(readpfmc([fn,'_edgecon.pfm'])/(255*par.sig_IC)).^2); lgd{j} = 'IC'; | ||
57 | |||
58 | i = round(m*[1;3]./4); | ||
59 | %i = i([1,1,2,2]); | ||
60 | j = round(n*[1;3]./4); | ||
61 | %j = j([1,2,1,2]); | ||
62 | k = m * (j-1) + i; | ||
63 | |||
64 | a = afromncut(v,s,dd,1,no_rep,k); | ||
65 | |||
66 | y = [4*nc-1, 4*nc, 5*nc-1, 5*nc, 6*nc-1, 6*nc]; | ||
67 | for t=1:length(k), | ||
68 | g{y(t)} = reshape(a(t,:),[m,n]); | ||
69 | lgd{y(t)} = sprintf('a at (%d,%d)',i(t),j(t)); | ||
70 | end | ||
71 | |||
72 | % find parameters | ||
73 | fg_title = sprintf('%s: %s=%d, %s=%d, %s=%3.2f, %s=%3.2f',... | ||
74 | par.fname_base,... | ||
75 | 'r_x', par.spatial_neighborhood_x,... | ||
76 | '\sigma_x',par.sigma_x,... | ||
77 | '\sigma_{IC}',par.sig_IC,... | ||
78 | 'repulsion',par.offset); | ||
79 | |||
80 | openfigure(nr,nc,fg_title,0); | ||
81 | dispimg(g,[nr,nc],lgd); | ||
82 | |||
83 | % fix | ||
84 | subplot(nr,nc,nc*3); | ||
85 | plot(s,'ro'); title('\lambda'); | ||
86 | axis square; axis tight; set(gca,'XTick',[]); | ||
87 | for t=1:length(k), | ||
88 | subplot(nr,nc,y(t)); | ||
89 | hold on; | ||
90 | text(j(t),i(t),'+'); | ||
91 | end | ||
92 | hold off | ||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/stella/startup.m b/SD-VBS/common/toolbox/toolbox_basic/stella/startup.m new file mode 100755 index 0000000..262429a --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/stella/startup.m | |||
@@ -0,0 +1,18 @@ | |||
1 | globalenvar; | ||
2 | |||
3 | addpath(IMAGE_DIR); | ||
4 | |||
5 | addpath(HOME_DIR); | ||
6 | |||
7 | path(path,['/hid/jshi/Research/walking']); | ||
8 | |||
9 | tb_dir = ['/hid/jshi/matlab/toolbox/']; | ||
10 | |||
11 | path(path,[tb_dir,'io']); | ||
12 | path(path,[tb_dir,'filter']); | ||
13 | path(path,[tb_dir,'filter_hist']); | ||
14 | path(path,[tb_dir,'disp']); | ||
15 | path(path,[tb_dir,'common']); | ||
16 | path(path,[tb_dir,'textons']); | ||
17 | path(path,[tb_dir,'pyramid']); | ||
18 | |||
diff --git a/SD-VBS/common/toolbox/toolbox_basic/stella/test_ncutm.m b/SD-VBS/common/toolbox/toolbox_basic/stella/test_ncutm.m new file mode 100755 index 0000000..c9b46ab --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/stella/test_ncutm.m | |||
@@ -0,0 +1,38 @@ | |||
1 | fn = 'walk1'; | ||
2 | |||
3 | repulsion_test = 1; | ||
4 | |||
5 | if 1, | ||
6 | f = getimage2(fn); | ||
7 | par = jshincutdefpar; | ||
8 | par.fname_base = fn; | ||
9 | par.spatial_neighborhood_x = 10; | ||
10 | par.sigma_x = 3 * par.spatial_neighborhood_x; | ||
11 | par.sig_IC = 0.03; | ||
12 | par.num_eigvecs = 10; | ||
13 | par.offset = 0.00; | ||
14 | par.sig_filter = 1.0; | ||
15 | par.elong_filter = 3.0; | ||
16 | [par,rec_num] = jshincut(par); | ||
17 | [g,lgd,v,s,dd] = showncut(fn,rec_num); | ||
18 | |||
19 | if repulsion_test, | ||
20 | par.offset = 0.1; | ||
21 | [par,rec_num] = jshincut(par); | ||
22 | figure; | ||
23 | [g,lgd,v,s,dd] = showncut(fn,rec_num); | ||
24 | end | ||
25 | end | ||
26 | |||
27 | if 0, | ||
28 | x = v(:,:,1); | ||
29 | y = v(:,:,2); | ||
30 | figure; | ||
31 | subplot(2,1,1); plot(x(:),y(:),'ro'); | ||
32 | r = sqrt(x.^2+y.^2); | ||
33 | x = x./r; | ||
34 | y = y./r; | ||
35 | subplot(2,1,2); im([x,y]*[x,y]'); | ||
36 | % mask = (x>0) & y>0; | ||
37 | % showmask(f,mask); | ||
38 | end | ||