基于python使用OpenCV進行物體輪廓排序
1 引言
在進行圖像處理過程中,我們經(jīng)常會遇到一些和物體輪廓相關的操作,比如求目標輪廓的周長面積等,我們直接使用Opencv的findContours函數(shù)可以很容易的得到每個目標的輪廓,但是可視化后, 這個次序是無序的,如下圖左側所示:

本節(jié)打算實現(xiàn)對物體輪廓進行排序,可以實現(xiàn)從上到下排序或者從左倒右排序,達到上圖右側的可視化結果.
2 栗子
2.1 讀取圖像
首先,我們來讀取圖像,并得到其邊緣檢測圖,代碼如下:
image = cv2.imread(args['image'])
accumEdged = np.zeros(image.shape[:2], dtype='uint8')
for chan in cv2.split(image):
?? ?chan = cv2.medianBlur(chan, 11)
? ? edged = cv2.Canny(chan, 50, 200)
? ? accumEdged = cv2.bitwise_or(accumEdged, edged)
cv2.imshow('edge map', accumEdged)運行結果如下:

左側為原圖,右側為邊緣檢測圖.
2.2 獲取輪廓
opencv-python中查找圖像輪廓的API為:findContours 函數(shù),該函數(shù)接收二值圖像作為輸入,可輸出物體外輪廓、內外輪廓等等.
代碼如下:
cnts = cv2.findContours(accumEdged.copy(), cv2.RETR_EXTERNAL, ?cv2.CHAIN_APPROX_SIMPLE)
cnts = grab_contours(cnts)
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5]
orig = image.copy()
# unsorted
for (i, c) in enumerate(cnts):
? ? orig = draw_contour(orig, c, i)
cv2.imshow('Unsorted', orig)
cv2.imwrite("./Unsorted.jpg", orig)運行結果如下:

需要注意的是,在OpenCV2.X版本,函數(shù)findContours返回兩個值,
函數(shù)聲明如下:
contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
但是在OpenCV3以上版本,該函數(shù)的聲明形式如下:
image, contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
所以為了適配兩種模式,我們實現(xiàn)函數(shù)grab_contours 來根據(jù)不同的版本,選擇對應的返回輪廓的下標位置,
代碼如下:
def grab_contours(cnts): ? ? # if the length the contours tuple returned by cv2.findContours ? ? # is '2' then we are using either OpenCV v2.4, v4-beta, or ? ? # v4-official ? ? if len(cnts) == 2: ? ? ? ? cnts = cnts[0] ? ? # if the length of the contours tuple is '3' then we are using ? ? # either OpenCV v3, v4-pre, or v4-alpha ? ? elif len(cnts) == 3: ? ? ? ? cnts = cnts[1] ? ? return cnts
2.3 輪廓排序
通過上述步驟,我們得到了圖像中的所有物體的輪廓,接下來我們定義函數(shù)sort_contours函數(shù)來實現(xiàn)對輪廓進行排序操作,該函數(shù)接受method參數(shù)來實現(xiàn)按照不同的次序對輪廓進行排序,比如從左往右,或者從右往左.
代碼如下:
def sort_contours(cnts, method='left-to-right'): ? ? # initialize the reverse flag and sort index ? ? reverse = False ? ? i = 0 ? ? # handle if sort in reverse ? ? if method == 'right-to-left' or method == 'bottom-to-top': ? ? ? ? reverse = True ? ? # handle if sort against y rather than x of the bounding box ? ? if method == 'bottom-to-top' or method == 'top-to-bottom': ? ? ? ? i = 1 ? ? boundingBoxes = [cv2.boundingRect(c) for c in cnts] ? ? (cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes), key=lambda b: b[1][i], reverse=reverse)) ? ? return (cnts, boundingBoxes)
上述代碼的核心思想為先求出每個輪廓的外接矩形框,然后通過對外接框按照x或y坐標排序進而來實現(xiàn)對輪廓的排序.
調用代碼如下:
# sorted
(cnts, boundingboxes) = sort_contours(cnts, method=args['method'])
for (i, c) in enumerate(cnts):
? ? image = draw_contour(image, c, i)
cv2.imshow('Sorted', image)
cv2.waitKey(0)運行結果如下:
2.4 其他結果
利用上述代碼,我們也可以實現(xiàn)從左往右的排序,如下所示:

3 總結
本文利用OpenCV實現(xiàn)了對物體輪廓按指定順序進行排序的功能,并給出了完整的代碼示例.
到此這篇關于基于python使用OpenCV進行物體輪廓排序的文章就介紹到這了,更多相關OpenCV進行物體輪廓排序內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python Pandas pandas.read_sql函數(shù)實例用法
在本篇文章里小編給大家整理的是一篇關于Python Pandas pandas.read_sql函數(shù)詳解內容,有需要的朋友們可以學習下。2021-06-06
python+opencv圖像分割實現(xiàn)分割不規(guī)則ROI區(qū)域方法匯總
這篇文章主要介紹了python+opencv圖像分割實現(xiàn)分割不規(guī)則ROI區(qū)域方法匯總,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04
pandas 數(shù)據(jù)類型轉換的實現(xiàn)
這篇文章主要介紹了pandas 數(shù)據(jù)類型轉換的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12
OpenCV實現(xiàn)手勢虛擬拖拽的使用示例(附demo)
本文主要介紹了OpenCV實現(xiàn)手勢虛擬拖拽的使用示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-11-11
conda管理Python虛擬環(huán)境的實現(xiàn)
本文主要介紹了conda管理Python虛擬環(huán)境的實現(xiàn),主要包括使用conda工具創(chuàng)建、查看和刪除Python虛擬環(huán)境,具有一定的參考價值,感興趣的可以了解一下2024-01-01

