Advertisement
Guest User

Untitled

a guest
Oct 17th, 2016
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 5.18 KB | None | 0 0
  1. function demoing(write)
  2. if ~exist('write', 'var'), write = false; end;
  3. SI =5; SX = 6; r = 1.5; sNcut = 0.02;
  4. I = imread('3.jpg');
  5. segI = main(I, SI, SX, r, sNcut);
  6. % show
  7. for i=1:length(segI)
  8.     if ~write
  9.         figure; imshow(segI{i});
  10.     end
  11. end
  12. end
  13.  
  14. function SegI = main(image, sig_i, sig_x, r, min_cut)
  15.  
  16. [num_row, num_col, components] = size(image);
  17. N=num_row * num_col;
  18.  
  19. % create graph (vertical list)
  20. V=reshape(image, N, components);
  21.  
  22. % Step 1. Compute weight matrix W, and D
  23. W = computeW(image, sig_i, sig_x, r);
  24.  
  25. % nodes are labled by pixel #
  26. Seg=(1:N)';% the first segment has whole nodes. [1 2 3 ... N]'
  27. [Seg]=Ncuts(Seg, W, min_cut);
  28.  
  29. % Convert node ids into images
  30. for i=1:length(Seg)
  31.     subV = zeros(N, components); %ones(N, c) * 255;
  32.     subV(Seg{i}, :) = V(Seg{i}, :);
  33.     SegI{i} = uint8(reshape(subV, num_row, num_col, components));
  34. end
  35. end
  36.  
  37. function W = computeW(in_seg, sig_i, sig_x, r);
  38. % input will be the image, sig_i (in the exponential weighting), sig_x
  39. % (in the exponential weighting), and r the threshold distance
  40. % get the parameters of the image the number of rows, columns, and the
  41. [num_row, num_col, components] = size(in_seg);
  42. N = num_row * num_col;
  43. W = zeros(N,N);
  44.  
  45. % Feature Vectors
  46. F = in_seg;
  47. F = reshape(F, N, 1, components); % col vector
  48.  
  49. %create a matrix of pixel w/ coordinate values
  50. xcoord=repmat((1:num_row)', 1, num_col);
  51. ycoord=repmat((1:num_col), num_row, 1);
  52. X=cat(3, xcoord, ycoord);
  53. X=reshape(X, N, 1, 2); % col vector
  54.  
  55. %create symmetrical matrix W with weights
  56. % we have two sets of coordinates here one i for the node we are
  57. % using as the center node, j nodes which are the ones which it is
  58. % connected to
  59. for i_col=1:num_col
  60.     for i_row=1:num_row
  61. %         % compute the j nodes' coordinates using the r value
  62. %         % j will be within i_row+r and i_row-r same applies for columns
  63. % use floor here because discrete
  64.         j_col = (i_col - floor(r)) : (i_col + floor(r)); % vector
  65.         j_row = ((i_row - floor(r)) :(i_row + floor(r)))';
  66.         j_col = j_col(j_col >= 1 & j_col <= num_col);
  67.         j_row = j_row(j_row >= 1 & j_row <= num_row);
  68.  
  69.         % compute the locations of the i nodes
  70.         % index in 1D array format
  71.         % compute the locations of the j nodes connected to i
  72.         % index in 1D array format ??
  73.         i = i_row + (i_col - 1) * num_row;
  74.         j = repmat(j_row, 1, length(j_col)) + repmat((j_col -1) * num_row, length(j_row), 1);
  75.         j = reshape(j, length(j_col) * length(j_row), 1); % a col vector
  76.  
  77.         %compute X_i-X_j and F_i-F_j
  78.         X_j = X(j, 1, :);% store coordinates of each index j [x,y] of each connected node
  79.         X_i = repmat(X(i, 1, :), length(j), 1);
  80.         %compute difference
  81.         DiffX = X_i - X_j;
  82.         %calculate ||F(i)-F(j)||2 where 2 represents L2 the euclidian distance
  83.         DiffX = sum(DiffX .* DiffX, 3); %distance formula in 3D
  84.  
  85.         %retain all points that are relevant using the condition that they are <r
  86.         retained = find(sqrt(DiffX) <= r);
  87.         j = j(retained);
  88.         DiffX = DiffX(retained);
  89.  
  90.         % feature vector disimilarity
  91.         F_j = F(j, 1, :);
  92.         F_i = repmat(F(i, 1, :), length(j), 1);
  93.         DiffF = F_i - F_j;
  94.         DiffF = sum(DiffF .* DiffF, 3); % squared euclid distance
  95.  
  96.         %calculate similarity W matrix
  97.         W(i, j) = exp(-DiffF / (sig_i*sig_i)) .* exp(-DiffX / (sig_x*sig_x));
  98.     end
  99. end
  100. end
  101.  
  102. function [seg_out ] = Ncuts(in_seg, W, min_cut)
  103. % seg_out is the segments returned after cutting
  104. % ncut is the normalized cut values of each seg_out part
  105. % seg_in represents the segment fed in to be cut
  106. % W is the weight matrix computed
  107. % min_cut is the minimum cut value (min Ncut(A,B))
  108. % min_area is the minimum input area (in paper they used a 5x5 grid)
  109.  
  110. % compute NxN D matrix from section 2.1
  111. % recall d(i)=summation_{j} w(i,j) and D is NxN with d on its diag
  112. N=length(W);
  113. d=sum(W, 2);
  114. D=spdiags(d,0,N,N);
  115.  
  116. % as per section section 3 we use the eig_vec with the second smallest
  117. % eig_val given by the definition of the rayleigh quotient
  118. [eig_vec,diag_eigval] = eigs(D-W, D, 2, 'sm');
  119. eig_vec2=eig_vec(:, 2);% grab second smallest
  120.  
  121. % https://www.quora.com/What-is-an-eigenvector-of-a-covariance-matrix
  122. % "...and the second eigenvector is orthogonal (perpendicular) to the
  123. % first. (for euclidian space)" which i'm guessing means is the center
  124. % of the data. knowing that fminsearch will find data based on its initial
  125. % value input we can choose the center value to predict a minimum as
  126. % follows
  127.  
  128. % HOW TO GET THE MINIMUM EQUATION THING?????
  129. t=mean(eig_vec2);
  130. t=fminsearch('NcutValue', t, [], eig_vec2, W, D);
  131.  
  132. % get cuts in A and B with the condition that one is less than one is
  133. % greatre than
  134. A=find(eig_vec2>t);
  135. B=find(eig_vec2<=t);
  136.  
  137. % ncut computed value is larger than the condition we set break recursion and end the
  138. % function
  139. ncut=NcutValue(t, eig_vec2, W, D);
  140. if (ncut>min_cut)
  141.     seg_out{1}=in_seg;
  142.     return;
  143. end
  144.  
  145. % recursive call on the A part
  146. [seg1] = Ncuts(in_seg(A), W(A, A), min_cut);
  147. % recursive call on the B part
  148. [seg2] = Ncuts(in_seg(B), W(B, B), min_cut);
  149.  
  150. % return complete array of segments
  151. seg_out=[seg1 seg2];
  152. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement