- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用以下代码从图像 (input.png) 中提取最里面的轮廓
(我正在使用 Python 3.6.3和 opencv-python==3.4.0.12)
输入.png
import copy
import cv2
BLACK_THRESHOLD = 200
THIN_THRESHOLD = 10
ANNOTATION_COLOUR = (0, 0, 255)
img = cv2.imread('input.png')
orig = copy.copy(img)
gray = cv2.cvtColor(img, 6)
thresh = cv2.threshold(gray, thresh=BLACK_THRESHOLD, maxval=255, type=cv2.THRESH_BINARY_INV)[1]
# Find the contours
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
hierarchy = hierarchy[0] # get the actual inner list of hierarchy descriptions
idx = 0
# For each contour, find the bounding rectangle and extract it
for component in zip(contours, hierarchy):
currentContour = component[0]
currentHierarchy = component[1]
x, y, w, h = cv2.boundingRect(currentContour)
roi = img[y+2:y + h-2, x+2:x + w-2]
# Skip thin contours (vertical and horizontal lines)
if h < THIN_THRESHOLD or w < THIN_THRESHOLD:
continue
if h > 300 and w > 300:
continue
if h < 40 or w < 40:
continue
if currentHierarchy[3] > 0:
# these are the innermost child components
idx += 1
cv2.imwrite(str(idx) + '.png', roi)
如您所见,提取的图像没有任何特定顺序。因此,为了解决这个问题,我根据它们的x 轴坐标排序轮廓。下面是代码:
import copy
import cv2
BLACK_THRESHOLD = 200
THIN_THRESHOLD = 10
ANNOTATION_COLOUR = (0, 0, 255)
img = cv2.imread('input.png')
orig = copy.copy(img)
gray = cv2.cvtColor(img, 6)
thresh = cv2.threshold(gray, thresh=BLACK_THRESHOLD, maxval=255, type=cv2.THRESH_BINARY_INV)[1]
# Find the contours
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
# Sort Contours on the basis of their x-axis coordinates in ascending order
def sort_contours(cnts, method="left-to-right"):
# initialize the reverse flag and sort index
reverse = False
i = 0
# handle if we need to sort in reverse
if method == "right-to-left" or method == "bottom-to-top":
reverse = True
# handle if we are sorting against the y-coordinate rather than
# the x-coordinate of the bounding box
if method == "top-to-bottom" or method == "bottom-to-top":
i = 1
# construct the list of bounding boxes and sort them from top to
# bottom
boundingBoxes = [cv2.boundingRect(c) for c in cnts]
(cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes),
key=lambda b: b[1][i], reverse=reverse))
# return the list of sorted contours
return cnts
sorted_contours = sort_contours(contours)
idx = 0
# For each contour, find the bounding rectangle and extract it
for component in sorted_contours:
currentContour = component
x, y, w, h = cv2.boundingRect(currentContour)
roi = img[y + 2:y + h - 2, x + 2:x + w - 2]
# Skip thin contours (vertical and horizontal lines)
if h < THIN_THRESHOLD or w < THIN_THRESHOLD:
continue
if h > 300 and w > 300:
continue
if h < 40 or w < 40:
continue
idx += 1
print(x, idx)
cv2.imwrite(str(idx) + '.png', roi)
结果:
这已经完美地对等高线进行了排序。但现在如您所见,我得到了所有轮廓(这就是每个数字有两个副本的原因)因为我没有使用层次结构 但当我花了一些时间调试时,我意识到只有等高线被排序,而不是它们相关的层次结构。因此,任何人都可以告诉我如何将层次结构与轮廓一起排序,以便我只能获得已排序轮廓的最内层轮廓。谢谢!
最佳答案
让我们从您的第一个脚本开始,因为它给您带来了不错的结果,只是排序不正确。
观察到基于层次结构的唯一决定(当您决定是否将给定轮廓视为数字时)是 currentHierarchy[3] > 0
为什么我们不从仅选择开始匹配该标准的轮廓,并仅对该子集进行进一步处理(不必再关心层次结构)。
# Find the contours
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
hierarchy = hierarchy[0] # get the actual inner list of hierarchy descriptions
# Grab only the innermost child components
inner_contours = [c[0] for c in zip(contours, hierarchy) if c[1][3] > 0]
现在我们只剩下我们感兴趣的轮廓,我们只需要对它们进行排序。我们可以重用您原始排序功能的简化版本:
# Sort Contours on the basis of their x-axis coordinates in ascending order
def sort_contours(contours):
# construct the list of bounding boxes and sort them from top to bottom
boundingBoxes = [cv2.boundingRect(c) for c in contours]
(contours, boundingBoxes) = zip(*sorted(zip(contours, boundingBoxes)
, key=lambda b: b[1][0], reverse=False))
# return the list of sorted contours
return contours
并得到排序的轮廓:
sorted_contours = sort_contours(inner_contours)
最后,我们要过滤掉垃圾并输出正确标记的良好轮廓:
MIN_SIZE = 40
MAX_SIZE = 300
THIN_THRESHOLD = max(10, MIN_SIZE)
PADDING = 2
# ...
idx = 0
# For each contour, find the bounding rectangle and extract it
for contour in sorted_contours:
x, y, w, h = cv2.boundingRect(contour)
roi = img[(y + PADDING):(y + h - PADDING), (x + PADDING):(x + w - PADDING)]
# Skip thin contours (vertical and horizontal lines)
if (h < THIN_THRESHOLD) or (w < THIN_THRESHOLD):
continue
if (h > MAX_SIZE) and (w > MAX_SIZE):
continue
idx += 1
cv2.imwrite(str(idx) + '.png', roi)
完整脚本(使用 Python 2.7.x 和 OpenCV 3.4.1)
import cv2
BLACK_THRESHOLD = 200
MIN_SIZE = 40
MAX_SIZE = 300
THIN_THRESHOLD = max(10, MIN_SIZE)
FILE_NAME = "numbers.png"
PADDING = 2
# ============================================================================
# Sort Contours on the basis of their x-axis coordinates in ascending order
def sort_contours(contours):
# construct the list of bounding boxes and sort them from top to bottom
boundingBoxes = [cv2.boundingRect(c) for c in contours]
(contours, boundingBoxes) = zip(*sorted(zip(contours, boundingBoxes)
, key=lambda b: b[1][0], reverse=False))
# return the list of sorted contours
return contours
# ============================================================================
img = cv2.imread(FILE_NAME)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Don't use magic numbers
thresh = cv2.threshold(gray, thresh=BLACK_THRESHOLD, maxval=255, type=cv2.THRESH_BINARY_INV)[1]
# Find the contours
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
hierarchy = hierarchy[0] # get the actual inner list of hierarchy descriptions
# Grab only the innermost child components
inner_contours = [c[0] for c in zip(contours, hierarchy) if c[1][3] > 0]
sorted_contours = sort_contours(inner_contours)
idx = 0
# For each contour, find the bounding rectangle and extract it
for contour in sorted_contours:
x, y, w, h = cv2.boundingRect(contour)
roi = img[(y + PADDING):(y + h - PADDING), (x + PADDING):(x + w - PADDING)]
# Skip thin contours (vertical and horizontal lines)
if (h < THIN_THRESHOLD) or (w < THIN_THRESHOLD):
continue
if (h > MAX_SIZE) and (w > MAX_SIZE):
continue
idx += 1
cv2.imwrite(str(idx) + '.png', roi)
及其生成的图像:
关于python - 在 Python 的 OpenCv 中使用已排序的轮廓对关联的层次结构进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49958640/
最近几天,我们考虑使用 Solr 作为我们的首选搜索引擎。 我们需要的大多数功能都是开箱即用的,或者可以轻松配置。 然而,我们绝对需要的一项功能似乎在 Solr 中被很好地隐藏(或缺失)了。 我会试着
我是 Sequelize 的新手,并且一直在探索关联。我正在使用 mysql 5.6 并 Sequelize ^4.42.0。我正在尝试创建两个简单的表:PRJS 和 TASKS 并将一些数据插入这些
关联、聚合和组合之间有什么区别?请从实现的角度解释一下。 最佳答案 对于两个对象,Foo 和 Bar 可以定义关系 关联 - 我与一个对象有关系。 Foo 使用 Bar public class Fo
这两种 hasOne 语法有什么区别? class Project { ....... ............ static hasOne = Employee // static h
对于当前的项目,我想使用遗传算法 - 目前我查看了 jenetics 库。 如何强制某些基因相互依赖?我想将 CSS 映射到基因上,例如我有基因指示是否显示图像,以及如果它也是各自的高度和宽度。因此,
关联、聚合和组合之间有什么区别?请从实现的角度解释一下。 最佳答案 对于两个对象,Foo 和 Bar 可以定义关系 关联 - 我与一个对象有关系。 Foo 使用 Bar public class Fo
假设我有一个名为“学生”的表格,其中包含姓名、手机、电子邮件、首选类(class)、首选学校、性别、年龄、地址、资格、职称、家庭电话、工作电话等列 我想从 Students 表中选择数据并插入到 2
问题标题有点困惑。我有一级员工和一级项目。一名或多名员工正在从事一个或多个项目。在这个关联中,我只有一个从具有*多重性的员工类到具有*多重性的项目类的链接。现在有另一种实现。每个项目只有一名经理,属于
到目前为止,我有一个程序采用一组随机点、站点,并围绕这些点形成适当的 Voronoi 图,表示为角和边的图形。它还为我提供了 Delaunay 三角剖分作为另一个以所有站点为节点的图形(尽管我不知道这
实现IComMethodEvents时你得到三个事件。 OnMethodCall OnMethodException OnMethodReturn 我的目标是记录 COM+ 组件中每个方法的调用时间。
我正在处理这个问题。我正在创造数学问题,每一个都有回应。例如。 如果我的问题是关于“5x + 15 = 2 的结果?”,我将只等待一个答案(整数)。 如果我的问题是关于“给我这个形状的面积和许可”,我
我正在寻找一种数据结构来保存唯一元素的无序集合,它将支持以下操作 在集合中任意位置插入/删除元素 查询元素是否存在 访问一个随机元素 天真地,1 和 2 建议使用关联容器,例如unordered_se
是否可以在 LINQ 中使用类似 ContactAddress.Contact 的内容,而无需在 SQL Server 中在这两者之间创建外键关系(通过 Contact.Id ContactAddr
我一直在谷歌搜索,但不明白调用 javax.persistence.criteria.Subquery 和 Criteria API 的方法相关的结果是什么。 http://www.objectdb.
我正在关注 Chris McCord 的“Programming Phoenix”一书,在第 6 章中,在 User 之间创建了一个关系。和一个 Video . 尝试使用 mix phoenix.se
我在 XAML 中有一个 ItemsControl,我在其中为每个组显示一个扩展器,以便我可以展开/折叠该组。我想保持 IsExpanded 的状态属性(以及可能与组标题显示相关的其他设置)。通常你只
Oracle 11 中是否有内置方法来检查 varchar2 字段中值的相关性?例如,给定一个简单的表,如下所示: MEAL_NUM INGREDIENT --------------------
是否可以在没有 JPA 在数据库中创建外键的情况下设置多对一关联? 这些表归另一个系统所有,并以异步方式填充。因此我们不能在数据库中使用 FK。仍然,几乎总是,最终是一种关系。 @ManyToOne(
我一直在使用NHibernate,使用Fluent NHibernate进行映射。我解决了很多问题,并开始认为自己在nhibernate中经验丰富。 但是,此错误非常奇怪。 这是我的模型: p
我正在开发一个 Typescript Sequelize 项目,其中我的 /models/index.ts 文件具有以下“导入此目录中的所有模型”功能: var basename = path.bas
我是一名优秀的程序员,十分优秀!