5

I am using OpenCV4Android version 2.4.11. I am reading frames from the camera and I detect any rectangular shapes in the frame. Then I try to draw a semi-transparent rectangle around a detected object.

what i want to do is, to draw a semi-transparent rectangale given the four corners of the detected object. But, in openCV you can draw a rectangle by spcifying only two points of it "the topLeft and BottomRight".

please let me know how to draw a rectangle by spcifying the the four corners of it NOT ONLY by spcifying the topLeft and BottomRight corners.

the below posted image is to show you my attempts and to show you that what i want is to draw a rectangle around the four detected corners "red, green, blue, white"

image:

enter image description here

Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • You'll have to draw a polygon specifying the four points. Have a look [here](http://docs.opencv.org/2.4/doc/tutorials/core/basic_geometric_drawing/basic_geometric_drawing.html), there is a section about drawing polygons – Klaus Nov 07 '16 at 08:41
  • @Klaus can you please provide an example how to draw the polygon? i have googled it but as for the java API for opencv i need to spceify a list of matOfPoints while i have only Point of the corners – Amrmsmb Nov 07 '16 at 08:45
  • I'm not able to figure your problem however [here](http://stackoverflow.com/questions/9082204/opencv-draw-a-white-filled-polygon) is a link to a similar question on SO. Look at the question not the answer, the difference is just that in the answer he does some translation with the points which I suppose you don't need. – Klaus Nov 07 '16 at 08:50

2 Answers2

5

OpenCV doesn't provide a rectangle drawing function, but you can generate the top-left and bottom-right points using the 4 points that you've computed:

Suppose your 4 points are - (tlx,tly),(trx,try),(blx,bly) and (brx,bry) where tl is top-left and br is bottom right.

Then you can calculate:

x1=min(tlx,trx,brx,blx);//top-left pt. is the leftmost of the 4 points
x2=max(tlx,trx,brx,blx);//bottom-right pt. is the rightmost of the 4 points
y1=min(tly,try,bry,bly);//top-left pt. is the uppermost of the 4 points
y2=max(tly,try,bry,bly);//bottom-right pt. is the lowermost of the 4 points

This is assuming that the point (0,0) occurs at the top left. Now you can use:

rectangle(src, Point(x1,y1), Point(x2,y2),Color,Thickness,other_params);
Saransh Kejriwal
  • 2,467
  • 5
  • 15
  • 39
4

Same idea as @Saransh, but compiles for me:

auto x1 = std::min(tlx, std::min(trx, std::min(brx, blx))); // top-left pt. is the leftmost of the 4 points
auto x2 = std::max(tlx, std::max(trx, std::max(brx, blx))); // bottom-right pt. is the rightmost of the 4 points
auto y1 = std::min(tly, std::min(try, std::min(bry, bly))); //top-left pt. is the uppermost of the 4 points
auto y2 = std::max(tly, std::max(try, std::max(bry, bly))); //bottom-right pt. is the lowermost of the 4 points
Daniel McLean
  • 451
  • 7
  • 10