【问题标题】:How to find corner (x, y) coordinate points on image Python OpenCV?从 Canny 图像中查找矩形对象
【发布时间】:2020-06-25 06:21:53
【问题描述】:

这是一个卡车集装箱图像,但从顶视图。首先,我需要找到矩形并知道每个角的位置。目标是知道容器的尺寸。

【问题讨论】:

  • 您到现在为止尝试了什么?
  • 好吧,我尝试通过应用 ROI 来去除不必要的区域,所以只有一个方形图像,但我需要知道角位置。
  • 哪个矩形,外矩形还是内矩形?请发布您的代码,以显示您尝试过的内容。
  • 发布您进行 Canny 边缘检测的原始图像。也许另一种方法可能会更好。

标签: python image opencv image-processing computer-vision


【解决方案1】:

这是一个简单的方法:

  1. 获取二值图像。 Load image,转换为grayscaleGaussian blur,然后是Otsu's threshold

  2. 找到扭曲的边界矩形轮廓。我们find contours 然后使用contour area 过滤以隔离矩形轮廓。接下来,我们找到带有cv2.minAreaRect() 的扭曲边界矩形并将其绘制到空白蒙版上。

  3. 查找角点。我们使用已实现为 cv2.goodFeaturesToTrack() 的 Shi-Tomasi 角点检测器进行角点检测。查看this 了解每个参数的说明。


检测到的边界矩形-> 掩码-> 检测到的角

角点

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

代码

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 2012-10-25
    • 1970-01-01
    • 2010-11-12
    • 2021-08-10
    • 1970-01-01
    相关资源
    最近更新 更多