使用Python和OpenCV对轮廓线进行排序
Alright, so at this point you have been exposed to contours pretty heavily on the PyImageSearch blog.
好吧,那么在这一点上,你已经在PyImageSearch博客上大量地接触到了轮廓线。
We used contours to build a kick-ass mobile document scanner.
我们用轮廓线建立了一个踢脚的移动文件扫描仪。
Contours enabled us detect barcodes in images.
轮廓线使我们能够检测图像中的条形码。
And we even leveraged the power of contours to find the distance from a camera to object or marker.
我们甚至还利用了轮廓线的力量来寻找从摄像头到物体或标记的距离。
But there still remains a sticking question that we have not addressed: how in the world do we sort contours from left-to-right, top-to-bottom, etc.
但仍有一个我们没有解决的症结问题:我们究竟如何 从左到右、从上到下对轮廓进行排序, 等等。
Oddly enough OpenCV does not provide a built-in function or method to perform the actual sorting of contours.
奇怪的是,OpenCV并没有提供一个内置的函数或方法来进行实际的轮廓排序。
But no worries.
但不用担心。
In the rest of this blog you’ll be sorting contours using Python and OpenCV like a pro.
在本博客的其余部分,你将像专家一样使用Python和OpenCV对轮廓线进行分类。
Read on to find out more…
继续阅读以了解更多...
OpenCV and Python versions:
In order to run this example, you’ll need Python 2.7 and OpenCV 2.4.X.
OpenCV和Python版本:
为了运行这个例子,你需要Python 2.7和OpenCV 2.4.X。
By the end of this blog article you’ll be able to:
在这篇博客文章结束时,你将能够。
-
Sort contours according to their size/area, along with a template to follow to sort contours by any other arbitrary criteria.
根据轮廓的大小/面积进行排序,同时还有一个模板,可以按照任何其他任意的标准对轮廓进行排序。
-
Sort contoured regions from left-to-right, right-to-left, top-to-bottom, and bottom-to-top using only a single function.
从左到右、从右到左、从上到下、从下到上对轮廓区域进行排序,只用一个 单一函数.
So let’s go ahead and get started. Open up your favorite code editor, name it
因此,让我们继续并开始吧。打开你喜欢的代码编辑器,将其命名为
sorting_contours.py
and let’s get started: → Launch Jupyter Notebook on Google Colab
sorting_contours.py
,让我们开始吧。→ 在Google Colab上启动Jupyter笔记本
Sorting Contours using Python and OpenCV
使用Python和OpenCV对轮廓线进行排序
# import the necessary packages
#导入必要的软件包
def sort_contours(cnts, method="left-to-right"):
def sort_contours(cnts, method="left-to-right"):
# initialize the reverse flag and sort index
#初始化反向标志和排序索引
# handle if we need to so...