如何使用OpenCV检测简单的几何形状 [英] How to detect simple geometric shapes using OpenCV

查看:932
本文介绍了如何使用OpenCV检测简单的几何形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目需要在iOS上检测图像中的简单几何形状.

I have this project where I need (on iOS) to detect simple geometric shapes inside an image.

搜索互联网后,我得出结论,最好的工具是OpenCV.问题是直到两个小时前,我还不知道什么是OpenCV,而且我甚至从未远程进行任何涉及图像处理的事情.我的主要经验是JS/HTML,C#,SQL,Objective-C ...

After searching the internet I have concluded that the best tool for this is OpenCV. The thing is that up until two hours ago I had no idea what OpenCV is and I have never even remotely did anything involving image processing. My main experience is JS/HTML,C#,SQL,Objective-C...

我从哪里开始?

我找到了这个 answer 我能够消化并且已经阅读了其他东西,我知道OpenCV应该返回带有点/角的形状数组,这是真的吗?另外,它将如何表示一个圆或半圆? 还有形状方向呢?

I have found this answer that I was able to digest and by reading already other stuff, I understand that OpenCV should return an Array of shapes with the points/corners, is this true? Also how will it represent a circle or a half circle? Also what about the shape orientation?

您知道有任何演示iOS功能的Demo iOS项目吗?

Do you know of any Demo iOS project that can demonstrate a similar functionality?

推荐答案

如果只有这些常规形状,则有一个简单的过程,如下所示:

If you have only these regular shapes, there is a simple procedure as follows :

  1. 在图像中查找轮廓(图像应为您所提问题的二进制图像)
  2. 使用approxPolyDP函数估算每个轮廓.
  3. 首先,检查所有形状的近似轮廓中的元素数量.是要识别形状.例如,正方形将具有4,五边形将具有5. (我的圈数为16,半圈数为9.)
  4. 现在分配颜色,运行测试图像的代码,检查其编号,并用相应的颜色填充.
  1. Find Contours in the image ( image should be binary as given in your question)
  2. Approximate each contour using approxPolyDP function.
  3. First, check number of elements in the approximated contours of all the shapes. It is to recognize the shape. For eg, square will have 4, pentagon will have 5. Circles will have more, i don't know, so we find it. ( I got 16 for circle and 9 for half-circle.)
  4. Now assign the color, run the code for your test image, check its number, fill it with corresponding colors.

下面是我在Python中的示例:

Below is my example in Python:

import numpy as np
import cv2

img = cv2.imread('shapes.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

ret,thresh = cv2.threshold(gray,127,255,1)

contours,h = cv2.findContours(thresh,1,2)

for cnt in contours:
    approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
    print len(approx)
    if len(approx)==5:
        print "pentagon"
        cv2.drawContours(img,[cnt],0,255,-1)
    elif len(approx)==3:
        print "triangle"
        cv2.drawContours(img,[cnt],0,(0,255,0),-1)
    elif len(approx)==4:
        print "square"
        cv2.drawContours(img,[cnt],0,(0,0,255),-1)
    elif len(approx) == 9:
        print "half-circle"
        cv2.drawContours(img,[cnt],0,(255,255,0),-1)
    elif len(approx) > 15:
        print "circle"
        cv2.drawContours(img,[cnt],0,(0,255,255),-1)

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

下面是输出:

请记住,它仅适用于常规形状.

Remember, it works only for regular shapes.

或者,您也可以使用houghcircles来查找圈子.您可以在此处找到教程.

Alternatively to find circles, you can use houghcircles. You can find a tutorial here.

关于iOS ,OpenCV开发人员将于今年夏天开发一些iOS示例,因此请访问其网站:www.code.opencv.org并与他们联系.

Regarding iOS, OpenCV devs are developing some iOS samples this summer, So visit their site : www.code.opencv.org and contact them.

您可以在此处找到其教程的幻灯片: http://code.opencv.org/svn/gsoc2012/ios/trunk/doc/CVPR2012_OpenCV4IOS_Tutorial.pdf

You can find slides of their tutorial here : http://code.opencv.org/svn/gsoc2012/ios/trunk/doc/CVPR2012_OpenCV4IOS_Tutorial.pdf

这篇关于如何使用OpenCV检测简单的几何形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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