Computing centroid of a detection box after computing NCC between a template image and another image

1 Ansicht (letzte 30 Tage)
Currently I am working on an object tracking project and I am using template matching to find my "measured position" of my object. I have a template image of my object which then I am computing NCC (normalized cross correlation) between the template and image, then a detection box is drawn on the object I am trying to track in the image. I would like to compute the centroid of the detection box to use it as an estimate for the "measured position" (so get an x and y coordinate in the image) but currently i keep getting the following error and I am not sure what is wrong.
Error using regionprops
Expected input number 1, L, to be one of these types:
uint8, uint16, uint32, int8, int16, int32, single, double, categorical
Instead its type was images.roi.Rectangle.
Error in regionprops (line 236)
validateattributes(L, supportedTypes, supportedAttributes, ...
The following is my code:
dog = im2gray(imread('108.jpg'));
temp = im2gray(imread('template6.jpg'));
montage({dog,temp})
c = normxcorr2(temp,dog);
surf(c)
figure;
plot(c);
shading flat
[ypeak,xpeak] = find(c==max(c(:)));
yoffSet = ypeak-size(temp,1);
xoffSet = xpeak-size(temp,2);
imshow(dog)
v = drawrectangle(gca,'Position',[xoffSet,yoffSet,size(temp,2),size(temp,1)], ...
'FaceAlpha',0);
Ibw = im2bw(dog);
Ibw = imfill(Ibw,'holes');
stat = regionprops(v,'centroid');
for x = 1: numel(stat)
plot(stat(x).Centroid(1),stat(x).Centroid(2),'ro');
end
I appreciate the help guys!

Akzeptierte Antwort

Matt J
Matt J am 24 Nov. 2022
Bearbeitet: Matt J am 24 Nov. 2022
Centroid=[xoffSet,yoffSet] + [size(temp,2),size(temp,1)]/2;
plot(Centroid(:,1),Centroid(:,2),'ro');
  3 Kommentare
Matt J
Matt J am 24 Nov. 2022
Instead of summarizing for us what you did and the results you got, you should get into the habit of running your code directly in your post, as I have done below. That will give us a full picture of what is happening. In any case, my example below should clarify what steps are needed and the result you can expect.
[x,y,h,w]=deal(50,20,20,30);
imshow(imread('cameraman.tif'));
drawrectangle(gca,'Position',[x,y,h,w], 'FaceAlpha',0);
Centroid=[x,y]+[h,w]/2;
hold on
plot(Centroid(:,1),Centroid(:,2),'ro','MarkerFaceColor','y');
hold off

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 24 Nov. 2022
Please attach your images with the paperclip icon after you read this:
so I can run your code.
In the meantime, see how I do it in the attached demo.
v is not a binary image or a labeled image like regionprops want. You need to make a mask
[rows, columns, numberOfColorChannels] = size(dog)
y1 = ypeak-size(temp,1);
x1 = xpeak-size(temp,2);
y2 = ypeak+size(temp,1);
x2 = xpeak+size(temp,2);
mask = false(rows, columns);
mask(y1:y2, x1:x2) = true;
  3 Kommentare
Nizar Sharkas
Nizar Sharkas am 24 Nov. 2022
I would like to amend my code to where the I can get the centroid coordinates (x and y) of the detection box (in blue). Also, if i wanted to see the coordinates of the box's corners, how can I do that?
Thanks a bunch!
Image Analyst
Image Analyst am 24 Nov. 2022
I think you totally overlooked my answer. Did you see the part where I said:
Please attach your images with the paperclip icon after you read this:
so I can run your code.
That is one way. How else can we work with your images? I don't want screenshots either. I want the actual .PNG files. Both the main scene and te target/template file.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Image Processing and Computer Vision finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by