How to draw a rectangle around a blob?
Show older comments
Hello, I am very new to MATLAB programming
I have a binary blob which changes its position with each frame. I have managed to get lower and higher bounds for each frame (Horizontal and Vertical).
vertprofile = any(binaryImage>0,1);
bbox_x_low = find(vertprofile, 1, 'first');
box_x_high = find(vertprofile, 1, 'last');
horzprofile = any(binaryImage>0,2);
bbox_y_low = find(horzprofile, 1, 'first');
bbox_y_high = find(horzprofile, 1, 'last');
My question is how can I draw a rectangle within these bounds?
also,
rectangle('Position',[1 2 5 6])
in this command how can I replace numbers with variables.
for example I have variables as, a=1;b=2;c=5;d=6 which changes with every frame.... and using the above command how can I draw rectangle by putting these variables instead numbers.
Thank you
Answers (2)
Harish Ramachandran
on 19 Dec 2017
Hi Sachin,
1. Using Rectangle: You can use variable names instead of values in the 'rectangle' command. You have the values for the following:
- x_low, y_low - First two arguments for the rectangle command
- (x_high - x_low), (y_high - y_low) - Final two arguments for the rectangle command
x_low = 1;
y_low = 2;
x_high = 6;
y_high = 8;
axis([0 10 0 10]);
rectangle('Position',[x_low y_low (x_high-x_low) (y_high-y_low)])
is equivalent to
axis([0 10 0 10]);
rectangle('Position',[1 2 5 6]);
4 Comments
Sachin Dalvi
on 17 Jan 2018
Harish Ramachandran
on 18 Jan 2018
I don't have any idea about the inputs you are providing.
However, take the inputs which I am providing:
x_low = 1;
y_low = 2;
x_high = 6;
y_high = 8;
The rectangle function works. The arguments to the rectangle function is a vector containing 4 values. In the event that I increase the arguments to the rectangle by 1 as in the case below:
x_low = [1 4];
y_low = 2;
x_high = 6;
y_high = 8;
rectangle('Position',[x_low y_low (x_high-x_low) (y_high-y_low)])
Error using rectangle
Value must be a 4 element vector
So please check the dimensions of x_low, y_low, x_high, y_high and make sure it is a scalar value.
Sachin Dalvi
on 22 Jan 2018
Harish Ramachandran
on 1 Feb 2018
Sorry for the delayed response. I am not sure what the issue is. Based on the values you provided -
x_high = 724;
y_high = 569;
x_low = 265;
y_low = 1;
rectangle('Position',[x_low y_low (x_high-x_low) (y_high-y_low)]);
I am able to draw a rectangle using those values. Can you provide a copy of the code / input? I will try my best to help you out.
Sachin Dalvi
on 6 Feb 2018
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
