OpenCV识别形状

矩形识别

.利用opencv来识别图片中的矩形 
其中遇到的问题主要是识别轮廓时矩形内部的形状导致轮廓不闭合。 

1. 对输入灰度图片进行高斯滤波 
2. 做灰度直方图,提取阈值,做二值化处理 
3. 提取图片轮廓 
4. 识别图片中的矩形 
5. 提取图片中的矩形

常用函数


(1)approxPolyDP 多边形逼近 

    approxPolyDP 主要功能是把一个连续光滑曲线折线化,对图像轮廓点进行多边形拟合


(2)boundingRect 最小外接矩形 
(3) minEnclosingCircle 最小外接圆形 
(4)arcLength 计算周长 
(5)contourArea 计算面积 
(6)FitEllipse 最小外接椭圆 
(7)cvFindDominantPoints 寻找关键点 
(8)轮廓匹配 
有10个图案,包括三角形,方形和圆。你把这三种图案识别出来,每一种图形画上不同颜色的轮廓,并提取出每个轮廓的重心坐标。

0.通过摄像头或是加载图片的方式获取到源图;
1.调用erode , dilate函数进行腐蚀膨胀,去掉图片上面黑色字体的影响(注意内核元素的定义)!
2.调节HSV的值,通过morphologyEx函数连接一些连通域;
3.通过第2步的操作得到只保留与目标形状相近部分的图片;
4.通过cvFindContours函数检测所有轮廓;
5.通过GetAreaMaxContour函数找到最大的轮廓(加上轮廓面积条件做判断,优化识别效果);
6.如果是圆,通过cvDrawContours函数画出轮廓;如果是矩形或是三角形,通过cvApproxPoly函数进行多边形逼近;
7.如果是圆的话,用handlecicle函数进行识别,参数根据实际图片进行调节。矩形与三角形的判断方法可参考网上的
 

凸包检测

凸包检测是给定二维平面上的点集,凸包就是将最外层的点连接起来构成的凸多边形,它是能包含点集中的所有点的。理解物体形状或轮廓的一种比较有用的方法便是计算一个物体的凸包,然后计算其凸缺陷。
OpenCV中提供了函数convesHull()用于对物体轮廓凸包进行检测,对形状凸包缺陷分析时使用convexityDefects()函数,每个缺陷区包含四个特征量:起始点、结束点、距离和最远点。
下面介绍这两个函数:
 

void convexHull(InputArray points, OutputArray hull, bool clockwise=false, bool returnPoints=true )
points – Input 2D point set, stored in std::vector or Mat.
hull – Output convex hull. It is either an integer vector of indices or vector of points. In the first case, the hull elements are 0-based indices of the convex hull points in the original array (since the set of convex hull points is a subset of the original point set). In the second case, hull elements aree the convex hull points themselves.
clockwise – Orientation flag. If it is true, the output convex hull is oriented clockwise. Otherwise, it is oriented counter-clockwise. The usual screen coordinate system is assumed so that the origin is at the top-left corner, x axis is oriented to the right, and y axis is oriented downwards.
returnPoints – Operation flag. In case of a matrix, when the flag is true, the function returns convex hull points. Otherwise, it returns indices of the convex hull points. When the output array is std::vector, the flag is ignored, and the output depends on the type of the vector: std::vector<int> implies returnPoints=true, std::vector<Point> implies returnPoints=false.

ConvexityDefects(const CvArr* contour, const CvArr* convexhull, CvMemStorage* storage=NULL )
contour – Input contour.
convexhull – Convex hull obtained using ConvexHull2() that should contain pointers or indices to the contour points, not the hull points themselves (the returnPoints parameter in ConvexHull2() should be zero).
storage – Container for the output sequence of convexity defects. If it is NULL, the contour or hull (in that order) storage is used.

convexhull可以得到vector<vector<Point> >和vector<vector<int>>两种类型结果,ConvexityDefects中的convechull凸包检测函数convexhull应该为vector<vector<int> >类型,否则不能通过ASSERT检查;参数convexityDefects为输出参数,检测到的结果为vector<vector<Vec4i> >类型,Vec4i中存储了起始点、结束点、距离及最远点到凸包的距离。
 

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值