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!
0 个评论
采纳的回答
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
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
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 个评论
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 Center 和 File Exchange 中查找有关 Image Processing and Computer Vision 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!