i am using folowing code for template matching using normxcorr2,and i get above error, please review the code and guid me.
code :
function findtemplate(im,temp,th,showtemp)
out = normxcorr2(temp,im);
[m,n] = size(temp);
out = out(m+1:end,n+1:end);
bw = out>th;
r = regionprops(bwlabel(bw));
if nargin > 3
im(1:m,1:n) = temp;%,r(i).Centroid(3),r(i).Centroid(4)
end
clf
imshow(im,[])
hold on
for i=1:length(r)
rectangle('position',[r(i).Centroid(1),r(i).Centroid(2),r(i).Centroid(3),r(i).Centroid(4)],'LineWidth',5);
end
error:
Index exceeds the number of array elements (2).
Error in findtemplate (line 14)
rectangle('position',[r(i).Centroid(1),r(i).Centroid(2),r(i).Centroid(3),r(i).Centroid(4)],'LineWidth',5);

 Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 28 Mär. 2020
Bearbeitet: Ameer Hamza am 28 Mär. 2020

0 Stimmen

I guess you are trying to draw rectangles using BoundingBox instead of Centroid. Centroid just have two elements, therefore, r(i).Centroid(3) gives error. Change the line to
rectangle('position',[r(i).BoundingBox(1),r(i).BoundingBox(2),r(i).BoundingBox(3),r(i).BoundingBox(4)],'LineWidth',5);

2 Kommentare

Sadoon Alwaz
Sadoon Alwaz am 28 Mär. 2020
thanks alot brother , it did work
Ameer Hamza
Ameer Hamza am 28 Mär. 2020
Glad to be of help.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by