Why NaN inf appears when only use simple sum and multiply
Show older comments
Hello, I am trying to use the code to do some image processing. First I randomly sample 2 images to get 2 vectors and try to locate the element position of the vector in the original image. Finally do some simple calculation based on the position and light intensity information. But finally there are always NaN or Inf occurs in the Ix Iy and It vectors. Could someone help me figure out what cause the Nans and Infs? Thanks!
if true
% Ims1=imread('abc.jpg');
Ims2=imread('def.jpg');
Ims1=double(Im1); % for comparing image
Ims2=double(Im2);
HEIGHT = size(Im1, 1);
WIDTH = size(Im1, 2);
data_num=10000; %max 27648
ce=zeros(data_num,2); %store position ce(1) for row, ce(2) for column
Im1=Ims1'; Im2=Ims2';
Im1=Im1(:); Im2=Im2(:);
aa=length(Im1);
p = randperm(aa,data_num);
y1=Im1(p);
y2=Im2(p);
%y1=double(y1);
%y2=double(y2);
for ii=1:data_num
cc=floor(p(ii)/WIDTH)+1;
dd=mod(p(ii),WIDTH);
if dd==0
dd=WIDTH;
cc=cc-1;
end
ce(ii,1)=cc;
ce(ii,2)=dd;
end
Ix=zeros(data_num,1);
Iy=zeros(data_num,1);
It=zeros(data_num,1);
G=zeros(data_num,1);
for jj=1:data_num
A1=ce(jj,:); %A1 selected point position
if jj==1
B1=ce(2:end,:); %B1 point position except A1
elseif jj==data_num
B1=ce(1:data_num-1,:);
else
B1=ce([1:jj-1,jj+1:end],:);
end
% dist=bsxfun(@hypot,B1(:,1)-A1(1),B1(:,2)-A1(2));
% out = B1(dist==min(dist),:); % position of the nearest point out(1) row out(2) col
% out=out(1,:);
% [rr,~]=size(out);
% for iii=1:rr
% if out(iii,1)>A1(1)&&out(iii,1)==A1(2) down=out(iii,:); havedown=1;
% else havedown=0; end
% if out(iii,1)>A1(1)&&out(iii,1)>A1(2) rightdown=out(iii,:); haverightdown=1;
% else haverightdown=0; end
% if out(iii,1)==A1(1)&&out(iii,1)>A1(2) right=out(iii,:); haveright=1;
% else haveright=0; end
% end
% [~,indx]=ismember(out,B1,'rows');
% B1(indx,:)=[];
dn=1;rn=1;rdn=1;count=1; K=10;
while ~(dn>1&&rdn>1&&rn>1)
% [n,~] = findNearestNeighbors(B1,A1,K)
[n]=knnsearch(B1,A1,'k',count*10);% n is the vector of index
count=count+1;
for iii=1:length(n) %all down rightdown right are for position
if (B1(n(iii),1)>A1(1))&&(B1(n(iii),2)==A1(2))
down(dn,:)=B1(n(iii ),:); dn=dn+1;
end
if (B1(n(iii),1)>A1(1))&&(B1(n(iii),2)>A1(2))
rightdown(rdn,:)=B1(n(iii),:); rdn=rdn+1;
end
if (B1(n(iii),1)==A1(1))&&(B1(n(iii),2)>A1(2))
right1(rn,:)=B1(n(iii),:); rn=rn+1;
end
end
if (count>6)
break
end
end
if ((dn>1)&&(rdn>1)&&(rn>1))
[n]=knnsearch(down,A1,'k',1);
outd1=down(n,:);
[n]=knnsearch(rightdown,A1,'k',1);
outrd1=rightdown(n,:);
[n]=knnsearch(right1,A1,'k',1);
outr1=right1(n,:);
[~,dp]=ismember(outd1,ce,'rows'); %find out which row
[~,rdp]=ismember(outrd1,ce,'rows');
[~,rp]=ismember(outr1,ce,'rows');
Ix(jj)=(y1(rp,1)-y1(jj,1))/(ce(rp,2)-A1(1,2))+(y1(rdp)-y1(dp))/(ce(rdp,2)-ce(dp,2))+(y2(rp)-y2(jj))/(ce(rp,2)-A1(1,2))+(y2(rdp)-y2(dp))/(ce(rdp,2)-ce(dp,2));
Iy(jj)=(y1(dp,1)-y1(jj,1))/(ce(dp,1)-A1(1,1))+(y1(rdp)-y1(rp))/(ce(rdp,1)-ce(rp,1))+(y2(dp)-y2(jj))/(ce(dp,1)-A1(1,1))+(y2(rdp)-y2(rp))/(ce(rdp,1)-ce(rp,1));
It(jj)=(y2(jj,1)-y1(jj,1)+y2(dp)-y1(dp)+y2(rdp)-y1(rdp)+y2(rp)-y1(rp))/4;
end
if ((dn>1)&&(rdn<2)&&(rn<2))
[n]=knnsearch(down,A1,'k',1);
outd1=down(n,:);
[~,dp]=ismember(outd1,ce,'rows');
Iy(jj)=(y1(dp,1)-y1(jj,1))/(ce(dp,1)-A1(1,1))+(y2(dp)-y2(jj))/(ce(dp,1)-A1(1,1));
Ix(jj)=0;
It(jj)=(y2(jj,1)-y1(jj,1)+y2(dp)-y1(dp))/2;
end
if ((dn>1)&&(rdn>1)&&(rn<2))
[n]=knnsearch(down,A1,'k',1);
outd1=down(n,:);
[n]=knnsearch(rightdown,A1,'k',1);
outrd1=rightdown(n,:);
[~,dp]=ismember(outd1,ce,'rows'); %find out which row
[~,rdp]=ismember(outrd1,ce,'rows');
Ix(jj)=(y1(rdp)-y1(dp))/(ce(rdp,2)-ce(dp,2))+(y2(rdp)-y2(dp))/(ce(rdp,2)-ce(dp,2));
Iy(jj)=(y1(dp,1)-y1(jj,1))/(ce(dp,1)-A1(1,1))+(y1(rdp)-y1(rp))/(ce(rdp,1)-ce(rp,1))+(y2(dp)-y2(jj))/(ce(dp,1)-A1(1,1))+(y2(rdp)-y2(rp))/(ce(rdp,1)-ce(rp,1));
It(jj)=(y2(jj,1)-y1(jj,1)+y2(dp)-y1(dp)+y2(rdp)-y1(rdp))/3;
end
if ((dn>1)&&(rdn<2)&&(rn>1))
[n]=knnsearch(down,A1,'k',1);
outd1=down(n,:);
[n]=knnsearch(right1,A1,'k',1);
outr1=right1(n,:);
[~,dp]=ismember(outd1,ce,'rows'); %find out which row
[~,rp]=ismember(outr1,ce,'rows');
Ix(jj)=(y1(rp,1)-y1(jj,1))/(ce(rp,2)-A1(1,2))+(y2(rp)-y2(jj))/(ce(rp,2)-A1(1,2));
Iy(jj)=(y1(dp,1)-y1(jj,1))/(ce(dp,1)-A1(1,1))+(y2(dp)-y2(jj))/(ce(dp,1)-A1(1,1));
It(jj)=(y2(jj,1)-y1(jj,1)+y2(dp)-y1(dp)+y2(rp)-y1(rp))/3;
end
if ((dn<2)&&(rdn>1)&&(rn<2))
[n]=knnsearch(rightdown,A1,'k',1);
outrd1=rightdown(n,:);
[~,rdp]=ismember(outrd1,ce,'rows');
Ix(jj)=0; Iy(jj)=0;
It(jj)=(y2(jj,1)-y1(jj,1)+y2(rdp)-y1(rdp))/2;
end
if ((dn<2)&&(rdn>1)&&(rn>1))
[n]=knnsearch(rightdown,A1,'k',1);
outrd1=rightdown(n,:);
[n]=knnsearch(right1,A1,'k',1);
outr1=right1(n,:);
[~,rdp]=ismember(outrd1,ce,'rows');
[~,rp]=ismember(outr1,ce,'rows');
Ix(jj)=(y1(rp,1)-y1(jj,1))/(ce(rp,2)-A1(1,2))+(y2(rp)-y2(jj))/(ce(rp,2)-A1(1,2));
Iy(jj)=(y1(rdp)-y1(rp))/(ce(rdp,1)-ce(rp,1))+(y2(rdp)-y2(rp))/(ce(rdp,1)-ce(rp,1));
It(jj)=(y2(jj,1)-y1(jj,1)+y2(rdp)-y1(rdp)+y2(rp)-y1(rp))/3;
end
if ((dn<2)&&(rdn<2)&&(rn>1))
[n]=knnsearch(right1,A1,'k',1);
outr1=right1(n,:);
[~,rp]=ismember(outr1,ce,'rows');
Ix(jj)=(y1(rp,1)-y1(jj,1))/(ce(rp,2)-A1(1,2))+(y2(rp)-y2(jj))/(ce(rp,2)-A1(1,2));
Iy(jj)=0;
It(jj)=(y2(jj,1)-y1(jj,1)+y2(rp)-y1(rp))/2;
end
if ((dn<2)&&(rdn<2)&&(rn<2))
Ix(jj)=0;
Iy(jj)=0;
It(jj)=y2(jj,1)-y1(jj,1);
end
G(jj)=ce(jj,1)*Iy(jj)+ce(jj,2)*Ix(jj);
end
A=[nansum(Ix.^2),nansum(Ix.*Iy),nansum(G.*Ix);nansum(Ix.*Iy),nansum(Iy.^2),nansum(G.*Iy);nansum(G.*Ix),nansum(G.*Iy),nansum(G.^2)]; % left side of equation 7
B=[nansum(Ix.*It);nansum(Iy.*It);nansum(G.*It)]; % right side of equation 7
C=-pinv(A)*B;
TTC(i)=1/C(3);
end
Answers (2)
Azzi Abdelmalek
on 20 Jul 2016
0 votes
nan is caused by 0/0 and inf is caused by 1/0 for example
1 Comment
Thomas Bradley
on 20 Jul 2016
Steven Lord
on 21 Jul 2016
0 votes
You can set an error breakpoint that will cause MATLAB to stop when an Inf or NaN is generated. This will allow you to determine which line generated the Inf or NaN.
2 Comments
Thomas Bradley
on 21 Jul 2016
Walter Roberson
on 21 Jul 2016
Which line was the error shown as occurring on?
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!