0

Here I have some quads in an image, and now I want to separate them into a single quad. Here is the image of the quads quads, and each shape like this I call a quad quad( a quad is formed by 4 points). Given that I've already had the coordinates of each line segment. For each line, I have their 2 endpoints, but I don't know which point is endpoint 1 or endpoint 2 endpoints of a line. If 2 lines are intersected, I also have their corner coordinate.
So in general, I have 3 lists:

  1. A list of endpoint 1 coordinates
  2. A list of endpoint 2 coordinates
  3. A list of corner coordinates if 2 lines are intersected
    The range of endpoint 1's list and endpoint 2's list is the same, but different to the corner's list.
    Could you give me some suggestions to separate the quads? This is the result that I want to achieve desire result

What I have tried:
I tried to get 4 coordinates for each quad:

  • for i in endpoint 1:
    -for j in corners: check if point 1 or point 2 close to the corner, then I have 2 points for a quad check if point 1 or 2 close to corner
    -for z in endpoint 2(or endpoint 1 is okay because they have the same range): find a line through a corner and point 2 or point 1, if a corner point belongs to this line, then I will have 3rd point for the quad.
    -for g in endpoint 2: check if 3rd point is close to point 1: then 4th point = point 2, otherwise = point 1.
    As you can see, there are so many for loops, and it even does not work. If you have any idea or suggestion, please help

Edit: This is the input image input image

Hai Luu
  • 43
  • 6
  • "A quad is formed by 4 lines" do you mean 4 lines or 4 points? – stateMachine Feb 23 '21 at 01:12
  • oh Im sorry, it is 4 points – Hai Luu Feb 23 '21 at 06:50
  • Please, provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) with your code an input images. In particular, how are you joining the points with lines to create the quads? – stateMachine Feb 23 '21 at 07:51
  • In your desired result image, there is some skeleton img. On that image, find all contours, get the convex hulls of each contour, and then follow this [answer](https://stackoverflow.com/a/11427501/11089932). – HansHirse Feb 23 '21 at 07:55
  • @eldesgraciado: I don't join the points with lines( maybe I misunderstood). For the original image, I used HoughLinesP to find the lines, this method gives me all the endpoints of each line, but there are many lines that are parallel to each other, so I have to combine all of them, then I had 1 single line as you can see. From that, I find the intersection points if they are intersected. – Hai Luu Feb 23 '21 at 09:06
  • @HansHirse: I think my image is a binary image, and find contours is just applied on the color image – Hai Luu Feb 23 '21 at 09:08
  • From [`findContours`](https://docs.opencv.org/4.5.1/d3/dc0/group__imgproc__shape.html#gadf1ad6a0b82947fa1fe3c3d497f260e0): _Finds contours in a binary image._ – HansHirse Feb 23 '21 at 09:17
  • The now provided input image only shows quads. Do you only have quads in your images or are there any other shapes? If not, simply finding contours is sufficient. The above linked answer fails on the given input, because for two quads the convex hull is a nearly perfect triangle. What a pity! But before investing more effort, please clarfiy, whether your images only contains quads. – HansHirse Feb 23 '21 at 09:29

0 Answers0