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

조회 수: 3 (최근 30일)
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!

채택된 답변

Matt J
Matt J 2022년 11월 24일
편집: Matt J 2022년 11월 24일
Centroid=[xoffSet,yoffSet] + [size(temp,2),size(temp,1)]/2;
plot(Centroid(:,1),Centroid(:,2),'ro');
  댓글 수: 3
Matt J
Matt J 2022년 11월 24일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2022년 11월 24일
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
Nizar Sharkas
Nizar Sharkas 2022년 11월 24일
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 2022년 11월 24일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Image Processing and Computer Vision에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by