首页 > 解决方案 > 如何使用python在opencv中封闭不规则的图形轮廓并用5px的点填充它?

问题描述

这是我的

输入图像

我想得到这个

输出图像

但问题是我无法包围轮廓,我应该如何添加这些点?Open cv 有没有这样的功能来处理这个问题?所以基本上,第一个问题是如何包围这个图像第二个,如何添加点。

谢谢

标签: pythonopencv3.0contourflood-fill

解决方案


这是在 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)

形态连接图像:

在此处输入图像描述

轮廓蒙版图片:

在此处输入图像描述

平铺圆圈:

在此处输入图像描述

结果:

在此处输入图像描述


推荐阅读