Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding rectangular object from canny image

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.

like image 570
maping-id Avatar asked Mar 13 '20 08:03

maping-id


People also ask

How can I detect hollow shapes?

For that you should consider training a custom object detector. Any help on detecting hollow shapes or shapes with something else filled in it (like other shapes or text) but not solid colors (only outline of the shape) is appreciated.

How to sketch the edges of any object on a picture?

The idea today is to build an algorithm that can sketch the edges of any object present on a picture, using the Canny edge detection algorithm. First of all, let’s describe what is the Canny Edge Detector: The Canny edge detector is an edge detection operator that uses a multi-stage algorithm to detect a wide range of edges in images.

How to detect edges in an image using OpenCV?

Canny () Function in OpenCV is used to detect the edges in an image. Syntax: cv2.Canny (image, T_lower, T_upper, aperture_size, L2Gradient) aperture_size: Aperture size of the Sobel filter. L2Gradient: Boolean parameter used for more precision in calculating Edge Gradient. Reduce Noise using Gaussian Smoothing.

What is the basic syntax of CV2 canny?

Syntax: cv2.Canny (image, T_lower, T_upper, aperture_size, L2Gradient) aperture_size: Aperture size of the Sobel filter. L2Gradient: Boolean parameter used for more precision in calculating Edge Gradient. Reduce Noise using Gaussian Smoothing. Compute image gradient using Sobel filter. Apply Non-Max Suppression or NMS to just jeep the local maxima


1 Answers

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()
like image 139
nathancy Avatar answered Sep 18 '22 02:09

nathancy