How to find the intersection of the diagonals of a bounding box?
3 次查看(过去 30 天)
显示 更早的评论
I need to find the intersection of the diagonals of the of the bounding box, i also need to find the co-ordinates of the same. This is the code I'm using. I need it find out the centre of the ROI, that being hand in my case.
clear all;
close all;
clc;
%intitial setup to acquire image
vid = videoinput('winvideo', 1, 'YUY2_640x480');
%set the FPT to infinity
set(vid, 'FramesPerTrigger', Inf);
%set colour space to RGB
set(vid, 'ReturnedColorspace', 'rgb');
%set frame grab interval as 5
vid.FrameGrabInterval = 5;
%start the acquisation
start(vid);
while(vid.FramesAcquired<=100);
%get the snapshot of the frame
data= getsnapshot(vid);
%subtract gray from individual component
diff_im = imsubtract(data(:,:,1), rgb2gray(data));
%median filtering, each pixel contains median value in the 3 by 3 nhbd
%around the corresponding pixel in the input
diff_im = medfilt2(diff_im, [3,3]);
%adjust image intensity values of colourmap
diff_im = imadjust(diff_im);
%compute global threshold level used to convert bin image
level = graythresh(diff_im);
%convert to binary image
bw = im2bw(diff_im,level);
%fill holes
bw5 = imfill (bw, 'holes');
%contains labels for the connected objects in BW
bw6 = bwlabel(bw5, 8);
%compute area centroid and boundingbox
stats = regionprops(bw6, ['basic']);
s = regionprops(bw6, 'centroid');
[N,M] = size(stats);
if (bw==0)
break;
else
tmp = stats(1);
for i = 2 : N
if stats(i).Area > tmp.Area
tmp = stats(i);
end
end
bb = tmp.BoundingBox;
bc = tmp.Centroid;
imshow(data)
hold on
rectangle('Position', bb, 'EdgeColor', 'r', 'LineWidth', 2)
for z= 1:numel(s)
plot(s(z).Centroid(1), s(z).Centroid(2), 'ro');
end
hold off
end
end
stop(vid);
flushdata(vid);
0 个评论
回答(1 个)
Image Analyst
2014-7-14
Bounding boxes always always align with the image edges, so the center and where the diagonals intersect will always simply be the average of the x and y coordinates.
3 个评论
Image Analyst
2014-7-14
xMiddle = (xLeft + xRight) / 2;
yMiddle = (yTop + yBottom) / 2;
xLeft, etc. are the bounding box coordinates, which may change at each frame but you're getting them from the .Boundingbox property of your regionprops() output.
DGM
2023-5-24
If you're dealing with a boundingbox from regionprops or something:
S = regionprops(inpict,'boundingbox');
bbox = S.BoundingBox;
boxcenter = bbox(1:2) + bbox(3:4)/2 % [x y]
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!