0
  • I have 4 red markers inside an image, I'm detecting their contours and getting a rectangular coordinates [x0,y0,w,h].

shape

markes

  • my goal is to identify the corners in this sequence:

desired

  • my problem is when I apply this cool answer I face a problem sometimes because of the sorting according to Y values is not always correct as you can see here:
import numpy as np
import cv2

boundRectsList = [(282, 236, 33, 44), (432, 235, 35, 46), (432, 68, 37, 44), (282, 68, 34, 46)]

# Sort the list based on ascending y values:
boundRectsSorted = sorted(boundRectsList, key=lambda x: x[1])

maskCopy = 255*np.ones((512,768,3), dtype=np.uint8)

# Rectangle dictionary:
# Each entry is an index of the currentRect list
# 0 - X, 1 - Y, 2 - Width, 3 - Height
# Additionally: -1 is 0 (no dimension):
pointsDictionary = {0: (2, 3),
                    1: (-1, 3),
                    2: (2, -1),
                    3: (-1, -1)}

# Store center rectangle coordinates here:
centerRectangle = [None]*4

# Process the sorted rects:
rectCounter = 0

for i in range(len(boundRectsSorted)):

    # Get sorted rect:
    currentRect = boundRectsSorted[i]

    # Get the bounding rect's data:
    rectX = currentRect[0]
    rectY = currentRect[1]
    rectWidth = currentRect[2]
    rectHeight = currentRect[3]

    # Draw sorted rect:
    cv2.rectangle(maskCopy, (int(rectX), int(rectY)), (int(rectX + rectWidth),
                             int(rectY + rectHeight)), (0, 255, 0), 5)

    # Get the inner points:
    currentInnerPoint = pointsDictionary[i]
    borderPoint = [None]*2

    # Check coordinates:
    for p in range(2):
        # Check for '0' index:
        idx = currentInnerPoint[p]
        if idx == -1:
            borderPoint[p] = 0
        else:
            borderPoint[p] = currentRect[idx]

    # Draw the border points:
    color = (0, 0, 255)
    thickness = 2
    centerX = rectX + borderPoint[0]
    centerY = rectY + borderPoint[1]
    radius = 10
    cv2.circle(maskCopy, (centerX, centerY), radius, color, thickness)

    # Mark the circle
    org = (centerX - 5, centerY + 5)
    font = cv2.FONT_HERSHEY_SIMPLEX
    cv2.putText(maskCopy, str(rectCounter), org, font,
            2, (0, 0, 0), 2, cv2.LINE_8)

    # Store the coordinates into list
    if rectCounter == 0:
        centerRectangle[0] = centerX
        centerRectangle[1] = centerY
    else:
        if rectCounter == 1:
            centerRectangle[2] = centerX - centerRectangle[0]
        else:
            if rectCounter == 2:
                centerRectangle[3] = centerY - centerRectangle[1]
    # Increase rectCounter:
    rectCounter += 1

cv2.namedWindow("Sorted Rects", cv2.WINDOW_NORMAL)
cv2.imshow("Sorted Rects", maskCopy)
cv2.waitKey(0)

result

Is there a proper way to get the upper sequence of the internal corners? thanks in advance.

Bilal
  • 3,191
  • 4
  • 21
  • 49

2 Answers2

3

I'd use a custom sort function which takes into account both, the x and the y coordinate, and allows for some tolerance w.r.t. the y coordinate. Here, I'd simply recycle the code from that earlier answer from me (also, details on the described function, see there):

import numpy as np
import cv2
from functools import cmp_to_key                                        # <--


def rect_sort(a, b):                                                    # <--
    if abs(a[1] - b[1]) <= 15:                                          # <--
        return a[0] - b[0]                                              # <--
    return a[1] - b[1]                                                  # <--


boundRectsList = [(282, 236, 33, 44), (432, 235, 35, 46), (432, 68, 37, 44), (282, 68, 34, 46)]

boundRectsSorted = sorted(boundRectsList, key=cmp_to_key(rect_sort))    # <--

# [...]

I get the desired output:

Output

----------------------------------------
System information
----------------------------------------
Platform:      Windows-10-10.0.16299-SP0
Python:        3.9.1
PyCharm:       2021.1
NumPy:         1.20.2
OpenCV:        4.5.1
----------------------------------------
HansHirse
  • 18,010
  • 10
  • 38
  • 67
1

If as mentioned in the question, you are targetting a quad in the generic case and not just a rectangle, then you should consider a more generic method that wouldn't just assume that you have minor differences in one of the axis.

1- Find the intersection point of the two diagonals (using simple line inters

2- The intersection point coordinates divide the space into 4 areas (left, top, right, down).

3- By comparing the corners' coordinates against the intersection point coordinates you can label them as (TL, TR .. ) or (0, 1, 2, 3)

Baraa
  • 1,476
  • 1
  • 16
  • 19