使用 OpenCV 进行三角形检测

Triangle detection using OpenCV

我有以下示例图片:

我想用白色填充角落里的这些三角形。我如何使用 OpenCV 检测它们?当然,在这个特定的示例中,我可以只依靠渐变或亮度。不过,以后的图像不会那么完美,所以我在考虑一些形状检测。

听说shape通常可以用Hough变换等检测出来。但是我不知道应该从什么开始。

OpenCV 中的轮廓检测没有帮助,因为它找到了太多候选对象。 我尝试使用大小为 3 的 approxPolyDP,但也没有结果(没有找到这样的对象)。

这些三角形永远是三角形,但它们不需要每次都触及柱线。它们总是位于图像的边缘。他们之间的面积大致相同。

我希望能够检测三角形并在某个容器中收集与这些三角形对应的点。

我可以用下面的代码检测三角形。我找到图像中的所有轮廓,然后使用 approxPolyDP,我能够找到三角形。

import cv2
import numpy as np
image_obj = cv2.imread('image.jpg')

gray = cv2.cvtColor(image_obj, cv2.COLOR_BGR2GRAY)

kernel = np.ones((4, 4), np.uint8)
dilation = cv2.dilate(gray, kernel, iterations=1)

blur = cv2.GaussianBlur(dilation, (5, 5), 0)


thresh = cv2.adaptiveThreshold(blur, 255, 1, 1, 11, 2)

# Now finding Contours         ###################
_, contours, _ = cv2.findContours(
    thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
coordinates = []
for cnt in contours:
        # [point_x, point_y, width, height] = cv2.boundingRect(cnt)
    approx = cv2.approxPolyDP(
        cnt, 0.07 * cv2.arcLength(cnt, True), True)
    if len(approx) == 3:
        coordinates.append([cnt])
        cv2.drawContours(image_obj, [cnt], 0, (0, 0, 255), 3)

cv2.imwrite("result.png", image_obj)

输出图片

您可以在坐标列表中获取轮廓。