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 );}