在檢測物體的輪廓時,我們通常會使用到opencv中的findcontour和drawcontour,比較常用而且效果不錯。那么findcontour是基于什么原理來實現輪廓的提取呢?
輪廓的提取與描述
在目標識別中我們首先要把感興趣的目標提取出來,而一般常見的步驟都是通過顏色或紋理提取出目標的前景圖(一幅黑白圖像,目標以白色顯示在圖像中),接下來我們要對前景圖進行分析進一步地把目標提取出來,而這里常常用到的就是提取目標的輪廓。
OpenCV里提取目標輪廓的函數是findContours,它的輸入圖像是一幅二值圖像,輸出的是每一個連通區域的輪廓點的集合:vector《vector《Point》》。外層vector的size代表了圖像中輪廓的個數,里面vector的size代表了輪廓上點的個數。下面我們通過實例來看函數的用法。
view plain copy print?
int main()
{
using namespace cv;
Mat image=imread(“。。/shape.png”);
cvtColor(image,image,CV_BGR2GRAY);
vector《vector《Point》》 contours;
// find
findContours(image,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
// draw
Mat result(image.size(),CV_8U,Scalar(0));
drawContours(result,contours,-1,Scalar(255),2);
namedWindow(“contours”);
imshow(“contours”,result);
waitKey();
return 0;
}
int main()
{
using namespace cv;
Mat image=imread(“。。/shape.png”);
cvtColor(image,image,CV_BGR2GRAY);
vector《vector《Point》》 contours;
// find
findContours(image,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
// draw
Mat result(image.size(),CV_8U,Scalar(0));
drawContours(result,contours,-1,Scalar(255),2);
namedWindow(“contours”);
imshow(“contours”,result);
waitKey();
return 0;
}
上面程序中包含了2個函數,第一個是查找輪廓函數,它的第三個參數說明查找輪廓的類型,這里我們使用的是外輪廓,還可以查找所有輪廓,即
包括一些孔洞的部分,像圖像人物胳膊與腰間形成的輪廓。第4個參數說明了輪廓表示的方法,程序中的參數說明輪廓包括了所有點,也可以用其
他參數讓有點直線的地方,只保存直線起始與終點的位置點,具體參數用法可以參考手冊里函數的介紹。
第二個函數drawContours是一個畫輪廓的函數,它的第3個參數程序里設置-1表示所有的輪廓都畫,你也可以指定要畫的輪廓的序號。
提取到輪廓后,其實我們更關心的是如果把這些輪廓轉換為可以利用的特征,也就是涉及到輪廓的描述問題,這時就有多種方法可以選擇,比如矢
量化為多邊形、矩形、橢圓等。OpenCV里提供了一些這樣的函數。
[cpp] view plain copy print?
// 輪廓表示為一個矩形
Rect r = boundingRect(Mat(contours[0]));
rectangle(result, r, Scalar(255), 2);
// 輪廓表示為一個圓
float radius;
Point2f center;
minEnclosingCircle(Mat(contours[1]), center, radius);
circle(result, Point(center), static_cast《int》(radius), Scalar(255), 2);
// 輪廓表示為一個多邊形
vector《Point》 poly;
approxPolyDP(Mat(contours[2]), poly, 5, true);
vector《Point》::const_iterator itp = poly.begin();
while (itp != (poly.end() - 1))
{
line(result, *itp, *(itp + 1), Scalar(255), 2);
++itp;
}
line(result, *itp, *(poly.begin()), Scalar(255), 2);
// 輪廓表示為凸多邊形
vector《Point》 hull;
convexHull(Mat(contours[3]), hull);
vector《Point》::const_iterator ith = hull.begin();
while (ith != (hull.end() - 1))
{
line(result, *ith, *(ith + 1), Scalar(255), 2);
++ith;
}
line(result, *ith, *(hull.begin()), Scalar(255), 2);
// 輪廓表示為一個矩形
Rect r = boundingRect(Mat(contours[0]));
rectangle(result, r, Scalar(255), 2);
// 輪廓表示為一個圓
float radius;
Point2f center;
minEnclosingCircle(Mat(contours[1]), center, radius);
circle(result, Point(center), static_cast《int》(radius), Scalar(255), 2);
// 輪廓表示為一個多邊形
vector《Point》 poly;
approxPolyDP(Mat(contours[2]), poly, 5, true);
vector《Point》::const_iterator itp = poly.begin();
while (itp != (poly.end() - 1))
{
line(result, *itp, *(itp + 1), Scalar(255), 2);
++itp;
}
line(result, *itp, *(poly.begin()), Scalar(255), 2);
// 輪廓表示為凸多邊形
vector《Point》 hull;
convexHull(Mat(contours[3]), hull);
vector《Point》::const_iterator ith = hull.begin();
while (ith != (hull.end() - 1))
{
line(result, *ith, *(ith + 1), Scalar(255), 2);
++ith;
}
line(result, *ith, *(hull.begin()), Scalar(255), 2);
程序中我們依次畫了矩形、圓、多邊形和凸多邊形。最終效果如下:
對連通區域的分析到此遠遠沒有結束,我們可以進一步計算每一個連通區域的其他屬性,比如:重心、中心矩等特征,這些內容以后有機會展開來寫。
以下幾個函數可以嘗試:minAreaRect:計算一個最小面積的外接矩形,contourArea可以計算輪廓內連通區域的面積;pointPolygenTest可以
用來判斷一個點是否在一個多邊形內。mathShapes可以比較兩個形狀的相似性,相當有用的一個函數。
評論