-1

I have eight points(four pairs) (x1,y1), (x2,y2), (x3,y3), (x4,y4). How can I make the transparent bounding box on the image using these points?

Aadnan Farooq A
  • 626
  • 4
  • 8
  • 19

1 Answers1

0

Even though the question is not fully clear, this should give the right direction.

I assume you have an arbitrarily rotated rectangle of four points being tuples of the form (x_n, y_n) as shown in the example:

The information about the type of object that is to handled is not given. Consequently, both pixel- and polygon based solutions are provided.

Pixel-based Solution

This solution requires some image processing before the computation of the bounding box. Useful links regarding this topic is given for example via these two links:

The gray scale image is read, converted to gray-scale, and binarized.

rgbImage = imread('Rectangle.png');
grayImage = rgb2gray(rgbImage);
bwImage = imbinarize(grayImage);

Based on this image, Matlab's regionprops function can be used
(https://de.mathworks.com/help/images/ref/regionprops.html#d123e242895). Via the 'BoundingBox' property, the object(s) coordinates are returned.

stats = regionprops(bwImage,'BoundingBox');

The coordinates are visualized as follows:

figure
imshow(bwImage)
hold on
rectangle('Position', stats.BoundingBox,...
   'EdgeColor','r', 'LineWidth', 3);

Polygon-based Solution

The second solution is based on polygons which can make use of the polygon data type (https://de.mathworks.com/help/matlab/ref/polyshape.html).

To get a comparable example, the polygon can be rotated.

pgon = polyshape([0 2.5 2.5 0],[0 0 2 2]);
pgon = rotate(pgon, 35, [0 0]);

The bounding box can computed via the boundingbox function (https://de.mathworks.com/help/matlab/ref/polyshape.boundingbox.html).

[xlim,ylim] = boundingbox(pgon);

Finally, it can be visualized through the points.

figure
plot(pgon)
axis equal
hold on
plot(xlim,ylim,'ro',xlim,fliplr(ylim),'ro');
matthlud
  • 75
  • 6
  • Thanks for the answer. What I want is there is an image which has multi objects in it. I have eights points for each of the objects in that. I have to draw rectangle (bounding box) around that. – Aadnan Farooq A Aug 31 '21 at 08:47
  • I would highly recommend to have closer look into Matlab's `regionprops()` documentation. When correctly segmented, multiple objects are inferred. Also, it would be helpful if you could attach an example image to your question. – matthlud Aug 31 '21 at 21:17
  • No i am not after that. I know the region and points of region of interest. I just needt to draw rectangle around that – Aadnan Farooq A Sep 01 '21 at 08:56
  • As shown in my answer, it should be easily doable with the `rectangle('Position',[x y u v])` function. You just need to calculate the location (`x=min(x), y=min(y)`), width (`u=max(x)-min(x)` and height `v=max(y)-min(y)`. – matthlud Sep 01 '21 at 10:15