OpenCV支持大量的轮廓、边缘、边界的相关函数,相应的函数有moments、HuMoments、findContours、drawContours、approxPolyDP、arcLength、boundingRect、contourArea、convexHull、fitEllipse、fitLine、isContourConvex、minAreaRect、minEnclosingCircle、 mathcShapes、pointPolygonTest。
下面这个程序用到的函数有,简单介绍其功能如下:
findContours:找到图像中轮廓
approxPolyDP:对多边形曲线做近似
boundingRect:计算并返回包围轮廓点集的最小矩形
minEnclosingCircle:计算并返回包围轮廓点集的最小圆形及其半径
drawContours:根据轮廓点集和轮廓结构画出轮廓#include "stdafx.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
Mat src; Mat src_gray;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345);
/// 函数声明
void thresh_callback(int, void* );
int main( int argc, char** argv )
{
/// 载入原图像, 返回3通道图像
src = imread( "boundrect.jpg" );
/// 转化成灰度图像并进行平滑
cvtColor( src, src_gray, CV_BGR2GRAY );
blur( src_gray, src_gray, Size(3,3) );
/// 创建窗口
char* source_window = "Source";
namedWindow( source_window, CV_WINDOW_AUTOSIZE );
imshow( source_window, src );
createTrackbar( " Threshold:", "Source", &thresh, max_thresh, thresh_callback );
thresh_callback( 0, 0 );
waitKey(0);
return(0);
}
void thresh_callback(int, void* )
{
Mat threshold_output;
vector<vector<Point> > contours; //轮廓数组(非矩形数组),每个轮廓是一个Point型的vector
vector<Vec4i> hierarchy; //见下面findContours的解释
/// 使用Threshold二值
threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY );
/// 找到轮廓
//contours参数为检测的轮廓数组,每一个轮廓用一个point类型的vector表示
//hiararchy参数和轮廓个数相同,每个轮廓contours[ i ]对应4个hierarchy元素hierarchy[ i ][ 0 ] ~hierarchy[ i ][ 3 ],
//分别表示后一个轮廓、前一个轮廓、父轮廓、内嵌轮廓的索引编号,如果没有对应项,该值设置为负数。
//CV_RETR_TREE:建立一个等级树结构的轮廓
//
findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
/// 多边形逼近轮廓 + 获取矩形和圆形边界框
vector<vector<Point> > contours_poly( contours.size() ); //近似后的轮廓点集
vector<Rect> boundRect( contours.size() ); //包围点集的最小矩形vector
vector<Point2f>center( contours.size() ); //包围点集的最小圆形vector
vector<float>radius( contours.size() ); //包围点集的最小圆形半径vector
for( int i = 0; i < contours.size(); i++ )
{
approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true ); //对多边形曲线做适当近似,contours_poly[i]是输出的近似点集
boundRect[i] = boundingRect( Mat(contours_poly[i]) ); //计算并返回包围轮廓点集的最小矩形
minEnclosingCircle( contours_poly[i], center[i], radius[i] ); //计算并返回包围轮廓点集的最小圆形及其半径
}
/// 画多边形轮廓 + 包围的矩形框 + 圆形框
Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
for( int i = 0; i< contours.size(); i++ )
{
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) ); //随机颜色
// drawContours( drawing, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
drawContours( drawing, contours_poly, i, color, 1, 8, hierarchy, 0, Point() ); //根据轮廓点集contours_poly和轮廓结构hierarchy画出轮廓
rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 ); //画矩形,tl矩形左上角,br右上角
circle( drawing, center[i], (int)radius[i], color, 2, 8, 0 ); //画圆形
}
/// 显示在一个窗口
namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
imshow( "Contours", drawing );
}