python - 如何使用python在opencv中封闭不规则的图形轮廓并用5px的点填充它?
问题描述
这是我的
我想得到这个
但问题是我无法包围轮廓,我应该如何添加这些点?Open cv 有没有这样的功能来处理这个问题?所以基本上,第一个问题是如何包围这个图像第二个,如何添加点。
谢谢
解决方案
这是在 Python/OpenCV 中执行此操作的一种方法。但是,如果不连接单独的区域,我将无法关闭您的虚线轮廓。但它会给你一些想法,如何继续你想做的大部分事情。
如果您在输入图像中有较大间隙的地方手动添加更多点,则可以将形态内核做得更小,以便它可以连接区域,而无需合并应该保持隔离的单独部分。
- 读取输入
- 转换为灰度
- 二进制阈值
- 应用形态关闭以尝试关闭虚线轮廓。不幸的是,它连接了不同的区域。
- 获取外部轮廓
- 在黑色背景上绘制白色填充轮廓作为蒙版
- 在白色背景上画一个黑色圆圈
- 将圆形图像平铺到输入的大小
- 用填充的轮廓图像掩盖平铺的圆形图像
- 保存结果
输入:
import cv2
import numpy as np
import math
# read input image
img = cv2.imread('island.png')
hh, ww = img.shape[:2]
# convert img to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# threshold
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)[1]
# use morphology to close figure
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (35,35))
morph = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, )
# find contours and bounding boxes
mask = np.zeros_like(thresh)
contours = cv2.findContours(morph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
for cntr in contours:
cv2.drawContours(mask, [cntr], 0, 255, -1)
# create a single tile as black circle on white background
circle = np.full((11,11), 255, dtype=np.uint8)
circle = cv2.circle(circle, (7,7), 3, 0, -1)
# tile out the tile pattern to the size of the input
numht = math.ceil(hh / 11)
numwd = math.ceil(ww / 11)
tiled_circle = np.tile(circle, (numht,numwd))
tiled_circle = tiled_circle[0:hh, 0:ww]
# composite tiled_circle with mask
result = cv2.bitwise_and(tiled_circle, tiled_circle, mask=mask)
# save result
cv2.imwrite("island_morph.jpg", morph)
cv2.imwrite("island_mask.jpg", mask)
cv2.imwrite("tiled_circle.jpg", tiled_circle)
cv2.imwrite("island_result.jpg", result)
# show images
cv2.imshow("morph", morph)
cv2.imshow("mask", mask)
cv2.imshow("tiled_circle", tiled_circle)
cv2.imshow("result", result)
cv2.waitKey(0)
形态连接图像:
轮廓蒙版图片:
平铺圆圈:
结果:
推荐阅读
- php - 在数据库中存储数组
- c# - 在 Xamarin 和 MVVM 中绑定 ObservableCollection Listview 时不刷新特定标签
- javascript - 边框底部的 CSS 转换延迟
- reporting-services - #SSRS输出中的计算值错误
- angularjs - 通过 MEAN 架构上的 Datatable 配置搜索字段
- javascript - 如何在 React 中的现有 DOM 中追加或插入 HTML 元素
- sql - sql teradata 过滤日期 - 数据库版本 Teradata 15.10.06.02 和提供程序版本 Teradata.Net 15.11.0.0
- angular - 延迟加载模块中的命名插座(角度 6.0.5)
- r - 如何使用ggplot制作虚线和图例
- python - Python:使用 OpenCV 进行字母修复以进行文本识别