python - 垂直组合轮廓并获得凸包 - opencv - python
问题描述
我有这样的角色图像:
得到后contours
输出convexHull
是这样的:
为此,我使用了以下代码:
import cv2
img = cv2.imread('input.png', -1)
ret, threshed_img = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY),
127, 255, cv2.THRESH_BINARY)
image, contours, hier = cv2.findContours(threshed_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for cnt in contours:
# get convex hull
hull = cv2.convexHull(cnt)
cv2.drawContours(img, [hull], -1, (0, 0, 255), 1)
cv2.imwrite("output.png", img)
正如您在下图中看到的,已识别出的轮廓与原始字符垂直对齐。但是那些与原始的核心角色是分开的。(这些实际上是僧伽罗语的修饰语 - සිංහල)
现在我想将那些垂直对齐的轮廓与核心角色合并。最终输出应如下所示。我怎样才能有效地做到这一点?
解决方案
您可以尝试使用具有垂直矩形形状的内核执行形态学操作。这样,原始字符之上的某些字符将被合并为一个。
rect_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 30))
threshed = cv2.morphologyEx(th2, cv2.MORPH_CLOSE, rect_kernel)
cv2.imshow('threshed', threshed)
imgContours, Contours, Hierarchy = cv2.findContours(threshed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for cnt in Contours:
hull = cv2.convexHull(cnt)
cv2.drawContours(img2, [hull], -1, (0, 0, 255), 1)
cv2.imshow('convex hull', img2)
推荐阅读
- acumatica - 发布自定义后,默认销售订单/报价单菜单消失
- c++ - 如何在 nodejs 插件中返回 cv::Mat
- variables - Eloqua 中提供了哪些动态内容?
- c++ - 如何获得变长数组
- ruby - 具有重复对应的站点棱镜映射元素
- wpf - MVVM 视图模型组织
- python - 使用自定义域时 Python Flask 会话不起作用
- java - java.lang.NoClassDefFoundError: com/csvreader/CsvReader 使用 javacsv
- jquery - 如何使用ajax和CI修复分页链接,单击时不更新
- azure-devops - 我可以使用此工具从 OnPrem 迁移到 Cloud 吗?