new*_*ewt 3 opencv object-detection
我想用白色填充角落中的这些三角形。如何使用OpenCV检测到它们?当然,在这个特定样本中,我可以依靠渐变或亮度。然而,将来图像将不会如此完美地成形,因此我正在考虑某种形状检测。
我听说通常可以使用例如Hough变换来检测形状。但是我不知道我应该从什么开始。
OpenCV中的轮廓检测没有帮助,因为它发现了太多的候选对象。我尝试使用大小= 3的roxPolyDP,但是也没有结果(没有找到这样的对象)。
先感谢您。
UPD:这些三角形将始终是三角形,但它们不必每次都触摸条。它们将始终位于图像的边缘。它们之间共享大约相同的区域。
UPD2:我希望能够在一个容器中检测三角形并收集与这些三角形相对应的点。
我可以使用以下代码检测三角形。我找到图像中的所有轮廓,然后使用roxPolyDP,就能找到三角形。
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)
Run Code Online (Sandbox Code Playgroud)
您可以在坐标列表中获得轮廓。