To retrieve height and width of more than one rectangle rectangle drawn and store it in a matrix

1 次查看(过去 30 天)
Hi Guys I am drawing rectangle in an image using following code
[L Ne]=bwlabel(BW2);
propied=regionprops(L,'BoundingBox');
hold on
for x=1:size(propied,1)
rectangle('Position',propied(x).BoundingBox,'EdgeColor','g','LineWidth',2)
end
hold off
Now if there are two rectangles formed in my image and I want to know their height, width, start and end point and store it in a matrix. (data Of both the rectangles)
I dont know how to do that. plz help me.

回答(2 个)

Jan
Jan 2011-4-4
hold on
RectH = zeros(1, 10);
for x = 1:10
rectangle('Position', (rand(1, 4) * 0.5), ...
'Tag', 'myRect');
end
Different methods to get the Positions:
Use the vector of handles, if it is available already:
PosList = get(RectH, 'Position');
If you have the AXES handle only:
RectH = findobj(allchild(AxesH), 'flat', 'Type', 'rectangle');
Or:
RectH = findobj(allchild(AxesH), 'flat', 'Tag', 'myRect');
If you have the FIGURE handle only:
RectH = findobj(allchild(FigH), 'Tag', 'myRect');

Karan Ratnaparkhi
Hey Jan Simon there are 3 rectangles formed in my image and i hav 2 get d positions of each of them and store the whole data in an array. Findobj finds d first rectangle it encounters and even if i am running a for loop for all rectangles i am getting position of first rectangle only, in array! Is der ny method to increment findobj to go for the next rectangle?
  1 个评论
Karan Ratnaparkhi
After the rectangles are drawn using above code my nxt code is
Q=zeros(Ne,4);
for i=1:Ne
rects = findobj(allchild(gca), 'flat', 'Type', 'rectangle');
%rects = findall(gcf,'type','rectangle');
for K = 1:length(rects)
for x=1:Ne
for y=1:4
rpos = get(rects(K),'Position');
fprintf(1, 'Rectangle #%d: height %g, width %g at (%g,%g)\n', ...
rpos(3), rpos(4), rpos(1), rpos(2));
Q(x,y)=rpos(y);
end
end
end
end
Q(1:Ne,1:4)
Bt i am getting value in Q as:
19.5000 9.5000 2.0000 1.0000
19.5000 9.5000 2.0000 1.0000
19.5000 9.5000 2.0000 1.0000
19.5000 9.5000 2.0000 1.0000

请先登录,再进行评论。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by