如何使用opencv在图像中找到近似三角形 [英] How to find approximately triangles in image by using opencv

查看:72
本文介绍了如何使用opencv在图像中找到近似三角形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这张图片:

我想找到这样的等腰直角三角形:

And I want to find isosceles right triangles like this:

我该怎么做?感谢您的帮助.

How can I do it? Thanks for your help.

推荐答案

您可以尝试模糊图像,转换为灰度并将不同的阈值应用于图像.下一步是找到并排列您的轮廓,并可能使用限制尺寸将它们过滤掉.

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)

结果:

注意:

变换到不同的颜色空间并应用不同的阈值可以提高结果.

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

这篇关于如何使用opencv在图像中找到近似三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆