轮廓检测
輪廓(Contours),指的是有相同顏色或者密度,連接所有連續(xù)點(diǎn)的一條曲線。檢測(cè)輪廓的工作對(duì)形狀分析和物體檢測(cè)與識(shí)別都非常有用。
在輪廓檢測(cè)之前,首先要對(duì)圖片進(jìn)行二值化或者Canny邊緣檢測(cè)。在OpenCV中,尋找的物體是白色的,而背景必須是黑色的,因此圖片預(yù)處理時(shí)必須保證這一點(diǎn)。
cv2.findContours函數(shù)
Python版示例如下,也可以參考【OpenCV-Python教程(11、輪廓檢測(cè))】【Contours : Getting Started】
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
1. 完整例子
import cv2#讀入圖片 img = cv2.imread("1.png")# 必須先轉(zhuǎn)化成灰度圖 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# 二值化 ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINAEY)# 尋找輪廓 contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)# 畫出輪廓,-1,表示所有輪廓,畫筆顏色為(0, 255, 0),即Green,粗細(xì)為3 cv2.drawContours(img, contours, -1, (0, 255, 0), 3)# 顯示圖片 cv2.namedWindow("Contours", cv2.NORMAL_WINDOW) cv2.imshow("Contours", img)# 等待鍵盤輸入 cv2.waitKey(0) cv2.destroyAllWindows()- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
2. 參數(shù)解釋
這里的findContours函數(shù),有三個(gè)參數(shù)
- thresh -> 要尋找輪廓的圖片,注意這里的輪廓會(huì)直接改變?cè)趖hresh上,記得備份
- cv2.RETR_TREE -> 表示輪廓檢索模式(Contour retrieval mode)為,檢索所有的輪廓,且重組為一個(gè)有層次的嵌套輪廓。層次信息返回在hierarchy中。
- cv2.CHAIN_APPROX_SIMPLE -> 表示輪廓近似方法(Contour approximation method)。SIMPLE可以這樣理解,假如一個(gè)矩形有1000個(gè)點(diǎn),但是現(xiàn)在只用四個(gè)角的點(diǎn)表示就行了,即去掉冗余信息。
返回值也有兩個(gè),contours?和?hierarchy
對(duì)contours的理解如下
print "找到 %d 個(gè)輪廓" %(len(contours)) print "第 0 個(gè)輪廓有 %d 個(gè)點(diǎn)" %(len(contours[0]))# 畫出第0個(gè)輪廓 cv2.drawContours(img, contours, 0, (0, 255, 0), 3) cv2.imshow("first contours", img)# 畫出第1個(gè)輪廓 cv2.drawContours(img, contours, 1, (0, 255, 0), 3) cv2.imshow("second contours", img)# 畫出所有的輪廓 cv2.drawContours(img, contours, -1, (0, 255, 0), 3) cv2.imshow("all contours", img)- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
對(duì) hierarchy 的深究,可以參考這里:【Contours Hierarchy】
輪廓特征(Contour Features)
查找到輪廓以后,我們可以得出輪廓的一些特征信息,也可以在輪廓上做一些簡單的操作,參考Python教程:【Contour Features】
1. 面積和周長示例
# 尋找輪廓 contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)# 取第 0 個(gè)輪廓 cnt = contours[0]# 輪廓面積 area = cv2.contourArea(cnt)# 周長,或者說,弧長;第二個(gè)參數(shù)的True表示該輪廓是否封閉 perimeter = cv2.arcLength(cnt, True)- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
2. 輪廓近似
輪廓近似(Contour Approximation),要理解概念,先來看下面的三張圖。第一張是找到的輪廓;第二張近似的幅度很大,忽略了很多的細(xì)節(jié);第三細(xì)節(jié)多一點(diǎn).
OpenCV中是用 cv2.approxPolyDP()函數(shù)來進(jìn)行輪廓的近似的。見代碼:
# 假設(shè)取第30個(gè)輪廓為例 cnt = contours[30]# 10%,即0.1的精確度 epsilon = 0.1 * cv2.arcLength(cnt, True) approx = cv2.approxPolyDP(cnt, epsilon, True)# 這里是第二張,10%的精確度 cv2.imshow("10% approximation", approx)- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
同理,第三張可以用1%的精確度來得到。
3. 計(jì)算凸包
涉及凸多面體和凹多面體的概念,不多解釋。如下圖,本來是一個(gè)手掌的形狀,現(xiàn)在用最小的凸多面體把它包起來。其中,凸進(jìn)去(Bulge Inside)的部分,稱為凸包缺陷(Convexity Defects),即箭頭處,即偏導(dǎo)的局部最大值處。
函數(shù)調(diào)用,以后用到再來細(xì)究吧!
hull = cv2.convexHull(cnt) cv2.imshow("hull", hull)- 1
- 2
- 1
- 2
4. 矩形邊框
矩形邊框(Bounding Rectangle)是說,用一個(gè)最小的矩形,把找到的形狀包起來。還有一個(gè)帶旋轉(zhuǎn)的矩形,面積會(huì)更小,效果見下圖
上代碼
# 用綠色(0, 255, 0)來畫出最小的矩形框架 x, y, w, h = cv2.boundingRect(cnt) cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)# 用紅色表示有旋轉(zhuǎn)角度的矩形框架 rect = cv2.minAreaRect(cnt) box = cv2.cv.BoxPoints(rect) box = np.int0(box) img = cv2.drawContours(img, [box], 0, (0, 0, 255), 2)- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
PS:其他的形狀,如?封閉的圓形,橢圓,直線?等,例子見這里【Contour Features】,原理差不多,不再贅述。
總結(jié)
- 上一篇: Opencv EmguCv 基本识别步骤
- 下一篇: EmguCV 一些基本操作