3

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

3
  • I don't see the difference between the two images. Can you elaborate? Commented Feb 24, 2015 at 6:38
  • Look at licence plate, I filled with red lines triangles Commented Feb 24, 2015 at 6:41
  • Oh! ok then. It wasn't very obvious. Perhaps you should add this to your post. I was staring at the background and the car. I didn't look at the license plate because it didn't occur to me to look there. BTW, it looks like that code only finds one triangle. tri keeps getting replaced when you detect three vertices for each polygon. Perhaps you should make a list and append these results when len(approx) == 3. Commented Feb 24, 2015 at 6:41

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.