Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
176 views
in Technique[技术] by (71.8m points)

How to find approximately triangles in image by using opencv

I have this image: Original image

And I want to find isosceles right triangles like this: Triangle found

How can I do it? Thanks for your help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could try to blur your image, convert in to gray scale and apply differnet threshold to your image. Next step is to find and arrange your contours and maybe filter them out with limiting sizes.

import cv2
import numpy as np

img = cv2.imread('triangle.png')
blur = cv2.GaussianBlur(img,(5,5),0)
values = [30, 40, 50, 60, 70, 80, 90]
gray_image = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)
for i in values:
    ret, threshold = cv2.threshold(gray_image,i,255,cv2.THRESH_BINARY)
    im, contours, hierarchy = cv2.findContours(threshold,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
    area = sorted(contours, key=cv2.contourArea, reverse=True)
    for j in range(1, len(area)):
        contour = area[j]
        size = cv2.contourArea(contour)
        if 10 < float(size) < 140000:
            cv2.drawContours(img, [contour], -1, (0,255,0), 2)
cv2.imshow('img', img)

Result:

enter image description here

Note:

Transformation to different color space and applying different threshold could improve the resut.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...