Clear Filters
Clear Filters

Using normxcorr2 to find a pattern, unexpected results

1 view (last 30 days)
Hello, I'm trying to use the normxcorr2 function to find the orientation of overlapping lines in an image, by comparing a smaller template with lines of varying angles to the larger image. The first set of images, as you can see in the picture, are at a 90° angle. The second, more diffuse set of lines has an inclination of 45°.
For each angle, the maximum correlation is saved to a vector, and finally plotted against that angle in a diagram. I would expect the maximum correlation to occur at the angle at which the lines in the image overlap, which I've measured by hand. However, that is close to where the minimum correlation appears.
Can anyone here tell me whether my assumption is wrong, or wheter I'm making a mistake in my implementation?
Below is my piece of code, I also attached the image that is being treated.
load image.mat
%Segmentation
neighborhood=59; %(Size of segments)-1
[m,n]=size(cropped2gray);
rechts=floor(m/neighborhood);
runter=floor(n/neighborhood);
nSeg=rechts*runter; %Number of segments
matcell2=cell(rechts,runter);
for i=1:runter
for j=1:rechts
tempmat=cropped2gray((i-1)*neighborhood+1:(i*neighborhood),(j-1)*neighborhood+1:(j*neighborhood));
matcell2{i,j}=tempmat;
end
end
% Search Cell-Array for MINIMUM correlation
minang=zeros(runter,rechts);
% for re=1:rechts
% for ru=1:runter
re=1;
ru=1;
%Generate template basis from image data
t2=matcell2{re,ru};
template=zeros(round(neighborhood/2))+mean(t2,'all');
template(:,round(neighborhood/4))=prctile(t2,20,'all');
b=0;
xstart=1;
ystart=1;
xend=size(template,2);
angvec=(10:85);
corrvec=zeros(size(angvec));
ctr=1;
corrmax=0;
for ang=10:85
template=zeros(round(neighborhood/2))+mean(t2,'all');
template(:,round(neighborhood/4))=prctile(t2,20,'all');
%Add variable line to image
yend=tand(ang)*xend+b;
[xmat,ymat]=bresenham(xstart,ystart,xend,yend);
template(sub2ind(size(template),xmat(1:size(template,2)),ymat(1:size(template,2))))=prctile(t2,90,'all');
c = normxcorr2(template,t2);
corrvec(ctr)=max(c(:));
ctr=ctr+1;
end
plot(angvec,corrvec);
% end
% end
In the above example, I disabled the loop through all partial images, to show just the effect.
It uses the Bresenham Optimized for Matlab algorithm to evaluate at which pixels the line needs to be inserted.

Answers (0)

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!