Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python OpenCV find all triangles in image

I trying to find all triangle in image with that code without success

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

So from this picture

So from this

I want get this ( Look at licence plate, I filled with red lines triangles)

enter image description here

That what I get now

enter image description here

like image 672
Dmitrij Holkin Avatar asked Feb 03 '26 15:02

Dmitrij Holkin


1 Answers

If the triangles are always the same colour you can preprocess the image to only display that colour, then use the code you have already written to find those triangles.

This link should get you started:

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

Hope this helps

like image 125
Aphire Avatar answered Feb 06 '26 07:02

Aphire



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!