# Code Understanding Help Needed

2 views (last 30 days)
Nataliya on 2 Aug 2015
Answered: Image Analyst on 2 Aug 2015
Hello, I have some problems in understanding this piece of code. Anybody's help will be really appreciated.
lambda=100;
level=1;
im=im2double(curr_frame);
scrib=im2double(curr_trimap);
scrib=reshape(scrib(:,:,1),[],1);
nn=[10;2];
[m n d]=size(im);
val=scrib>0.99;
map=(scrib<0.01)+val;
[a b]=ind2sub([m n],1:m*n);
feature=[reshape(im,m*n,d)';[a;b]/sqrt(m*m+n*n)*level+rand(2,m*n)*1e-6];
now=0;
for i=1:size(nn,1)
ind=vl_kdtreequery(vl_kdtreebuild(feature),feature,feature,'NUMNEIGHBORS',nn(i),'MAXNUMCOMPARISONS',nn(i)*2);
a=reshape(repmat(uint32(1:m*n),nn(i),1),[],1);
b=reshape(ind,[],1);
row(now+1:now+m*n*nn(i),:)=[min(a,b) max(a,b)];
feature(d+1:d+2,:)=feature(d+1:d+2,:)/100;
now=now+m*n*nn(i);
end
value=max(1-sum(abs(feature(1:d+2,row(:,1))-feature(1:d+2,row(:,2))))/(d+2),0);
A=sparse(double(row(:,1)),double(row(:,2)),value,m*n,m*n);
A=A+A';
D=spdiags(sum(A,2),0,n*m,n*m);
M=D-A+lambda*spdiags(map,0,m*n,m*n);
L=ichol(M);
x=pcg(M,lambda*val,[],2000,L,L');
% figure,imshow(reshape(x,m,n));
% imwrite(reshape(x,m,n),'res.png','png');
Alpha = reshape(x,m,n);
val=scribe>0.99; it will assign 1 to the values greater than 0.99 and all others =0. Now next is map=(scrib<0.01)+val; How the values will be assign here? Please explain this condition. Also, please explain this line:
feature=[reshape(im,m*n,d)';[a;b]/sqrt(m*m+n*n)*level+rand(2,m*n)*1e-6];
I am unable to understand this line.

Image Analyst on 2 Aug 2015
The first line (where you renamed scrib to scribe)
val=scribe>0.99
creates a binary/logical variable called val that is true/1 for any element in scrib (or scribe) that is greater than 0.99, and false/0 for any element that is 0.99 or less.
The second line
map=(scrib<0.01)+val;
first creates a temporary binary/logical array where it's true for any element where scrib is less than 0.99. Then, because it's using + instead of &, it casts val to double, then casts the temporary logical variable to double, then adds them together to form a new double variable called map. Most good MATLAB programmers would have done it this way instead:
map = (scrib<0.01) & val;
so map would have been a logical variable also.
The third line :
feature=[reshape(im,m*n,d)';[a;b]/sqrt(m*m+n*n)*level+rand(2,m*n)*1e-6];
creates an array by concatenating vertically two other arrays. The first part, reshape(im,m*n,d), takes all the pixels in the image and puts them into a column vector. If the image is color, you will have the green and blue channels in columns 2 and 3 respectively. Then it transposes it so they are in row vectors. Then it concatenates a scaled version of the badly named a and b underneath that.
The reason it's hard to follow is that this is poorly written code. It's an alphabet soup mess of a code with no comments, variable names that are single letters with no clue as to what they mean, describe, or represent, and use variable names (like "now") that destroy built-in functions. Don't be like this programmer. Instead, read this link and be better than them.