1

I have following images:

Mask mask2 mask3

I want to extract coordinates of bounding rectangle of largest yellow section. minAreaRect doesnt help, since it doenst take a perspective, so it is not fitted correctly. There are another examples of masks, I want to transform.

At the end I want to have coordinates of 4 corners of largest shape there.

As you can see mask will not be 100% rectangle, is this even possible in opencv?

I tried to do cannyedge detection, then extract contours as lines (dilate/erode), then I can get contour of yellow area, but still dont know how to extract that corners then, because they are rounded.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Kristián Stroka
  • 698
  • 1
  • 8
  • 23
  • If you have the contour you can get a bounding box around it, using cv2.minAreaRect(..): https://docs.opencv.org/3.4/dd/d49/tutorial_py_contour_features.html I think you are looking for the 'rotated rect' – user_cr May 31 '22 at 11:15
  • 1
    well rotated rect works well if there is no perspective in image... for example if you will take first picture, then minarearect will not give you proper informations – Kristián Stroka May 31 '22 at 13:26
  • [this](https://i.imgur.com/TAr9Ntk.png) is my result, after filtering the contours by size. Is that not what you want? (I don't understand your 'perspective' problem) – user_cr May 31 '22 at 13:35
  • as you can see in right top corner, it is not on exact specific position, should be little left and little down – Kristián Stroka May 31 '22 at 15:22
  • [This](https://stackoverflow.com/questions/41138000/fit-quadrilateral-tetragon-to-a-blob) might help. – beaker May 31 '22 at 15:23
  • Threshold on color. Then get largest contour. Then get the convex hull. Then use approxPolyDP() to reduce to 4 vertices. – fmw42 May 31 '22 at 16:22
  • @kristián-stroka No it shouldn't, a rectangle has 90 degree corners. You are confusing a rectangle with a trapezoid. [relevant question](https://stackoverflow.com/questions/20454380/building-a-rectangle-with-a-group-of-points-in-opencv) – user_cr Jun 01 '22 at 08:23
  • 2
    @user_cr Allow me to rephrase it. Kristian wants a quadrilateral that can encloses the largest contour properly, it doesn't have to be a rectangle. After getting the 4 corners of the quadrilateral, those points can be used to change the perspective – Jeru Luke Jun 01 '22 at 10:57
  • @KristiánStroka were you able to find any solution? I tried 3 different approaches using: 1. modifying `cv2.minAreaRect()`, 2. using K-means on contours points, 3. using angle difference between successive points. But I still could not get any headway – Jeru Luke Jun 06 '22 at 06:46
  • 1
    @JeruLuke not yet, I have "temporary solution" where Iˇm doing this: 1. I did minarearect around largest contour in image, then fetching those corners, and create square around them with +- 10% of image. After that I apply that mask to my contour with overlaying squares, so only lines of contour remains ... and by that lines I did cv2.fitline with DIST_HUBER, and found all intersects of lines ... for most cases it works, but it is little bit hacky, because if contour has weird side lines (as my last image has top edge), then that contour lines with fitline will not work or are not success – Kristián Stroka Jun 06 '22 at 06:53

0 Answers0