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.

Kristián Stroka
  • 616
  • 1
  • 7
  • 21
  • 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

0 Answers0