Python OpenCV 找到图像中的所有三角形

Python OpenCV find all triangles in image

我试图用该代码找到图像中的所有三角形,但没有成功

import numpy as np
import cv2

img = cv2.imread('2.jpg')

for gray in cv2.split(img):
    canny = cv2.Canny(gray,50,200)

    contours,hier = cv2.findContours(canny,1,2)
    for cnt in contours:
        approx = cv2.approxPolyDP(cnt,0.02*cv2.arcLength(cnt,True),True)
        if len(approx)==3:
            cv2.drawContours(img,[cnt],0,(0,255,0),2)
            tri = approx

for vertex in tri:
    cv2.circle(img,(vertex[0][0],vertex[0][1]),5,255,-1)

cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

所以从这张图来看

我想要这个(看车牌,我用红线三角形填充)

这就是我现在得到的

如果三角形始终是相同的颜色,您可以预处理图像以仅显示该颜色,然后使用您已经编写的代码找到这些三角形。

这个link应该让你开始:

http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_imgproc/py_colorspaces/py_colorspaces.html

希望对您有所帮助