简体   繁体   中英

Finding rectangular object from canny image

Canny 边缘检测

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

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. We find contours then filter using contour area to isolate the rectangular contour. Next we find the distorted bounding rectangle with cv2.minAreaRect and draw this onto a blank mask.

  3. Find corners. We use the Shi-Tomasi Corner Detector already implemented as cv2.goodFeaturesToTrack for corner detection. Take a look at this for an explanation of each parameter.


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_TREE, 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)
        box = cv2.boxPoints(rect)
        box = np.int0(box)
        cv2.fillPoly(mask, [box], (255,255,255))

# Find corners
corners = cv2.goodFeaturesToTrack(mask,4,.8,100)
offset = 15
for corner in corners:
    x,y = corner.ravel()
    cv2.circle(image,(x,y),5,(36,255,12),-1)
    x, y = int(x), int(y)
    cv2.rectangle(image, (x - offset, y - offset), (x + offset, y + offset), (36,255,12), 3)
    print("({}, {})".format(x,y))

cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.imshow('mask', mask)
cv2.waitKey()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM