How to find corner (x, y) coordinate points on image Python OpenCV?
Asked Answered
G

2

6

Canny Edge Detection

This is a truck container image but from the top view. First, I need to find the rectangle and know each corner position. The goal is to know the dimension of the container.

Goodard answered 13/3, 2020 at 8:16 Comment(4)
What did you try up to now?Tearoom
well i tried to remove unnecesary area by applying a ROI so there is only a square image, but i need to know the corner position.Goodard
Which rectangle, the outer rectangle or the inner one? Please post your code, to show what you have tried.Optometrist
Post the original image from which you did the Canny edge detection. Perhaps another approach might be better.Viscous
L
7

Here's a simple approach:

  1. Obtain binary image. Load image, convert to grayscale, Gaussian blur, then Otsu's threshold.

  2. Find distorted bounding rectangle contour and corners. We find contours then filter using contour area to isolate the rectangular contour. Next we find the distorted bounding rectangle with cv2.minAreaRect() and the corners with cv2.boxPoints()


Detected bounding rectangle -> Mask -> Detected corners

Corner points

(188, 351)
(47, 348)
(194, 32)
(53, 29)

Code

import cv2
import numpy as np

# Load image, grayscale, blur, Otsu's threshold
image = cv2.imread('1.png')
mask = np.zeros(image.shape[:2], dtype=np.uint8)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

# Find distorted bounding rect
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    area = cv2.contourArea(c)
    if area > 5000:
        # Find distorted bounding rect
        rect = cv2.minAreaRect(c)
        corners = cv2.boxPoints(rect)
        corners = np.int0(corners)
        cv2.fillPoly(mask, [corners], (255,255,255))
        
        # Draw corner points
        corners = corners.tolist()
        print(corners)
        for corner in corners:
            x, y = corner
            cv2.circle(image, (x, y), 5, (36,255,12), -1)

cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.imshow('mask', mask)
cv2.waitKey()
Lastly answered 13/3, 2020 at 21:44 Comment(4)
Why using a corner detector (which is always difficult to tune) when you already know the corners ???Cardamom
Wot you don't know the corner coordinates beforehand so use cv2.goodFeaturesToTrack to get the corner coordinatesLastly
Come on, minAreaRect !Cardamom
@YvesDaoust Changed! Thanks for catching that unnecessary stepLastly
C
0

minAreaRect applied to the contour is your best friend. There is no need for a corner detector.

https://docs.opencv.org/3.4/d3/dc0/group__imgproc__shape.html#ga3d476a3417130ae5154aea421ca7ead9

Cardamom answered 9/5, 2022 at 8:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.