python - 填充轮廓圆

标签 python image opencv image-processing python-imaging-library

我有一组图像,其中圆圈绘制为白色轮廓。但是,我想用白色填充整个圆圈。什么是快速的方法呢?以下是图片示例:

Sample image

我曾尝试使用嵌套循环来实现这一点,但这需要花费很多时间,而且我有大约 150 万张图像。以下是我的代码:

roundRobinIndex = 0
new_image = np.zeros((img_w, img_h))
for row in range(540):
    for column in range(800):
        if image[row,column] == 255:
            roundRobinIndex = (roundRobinIndex + 1) % 2
        if roundRobinIndex == 1:
            new_image[row, column] = 255

最佳答案

使用cv2.fillPoly()填充圆形轮廓

enter image description here

import cv2

image = cv2.imread('1.png', 0)
thresh = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cv2.fillPoly(image, cnts, [255,255,255])

cv2.imshow('image', image)
cv2.waitKey()

注意:由于输入图像已经是二值图像,可以移除 Otsu 的阈值以获得稍微更快的性能,您可以直接在灰度图像上找到轮廓

关于python - 填充轮廓圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58174115/

相关文章:

python - 减少在 Python 中运行多个 group by 的时间

C++ SVG库写入路径数据

c++ - OpenCV 4.2.0 FileStorage段错误

python - 单元测试中的模拟日志处理程序

python - Flask:通过写入客户端来流式传输数据?

Python:如何通过 SSH 和 paramiko 复制文件而不使用 sftp

c# - WPF 图像源绑定(bind)

安卓 : Automatically remove unused images from project bundle

image-processing - 如何使用openCV去除图像中的阴影?

c++ - 如何在 OpenCV 上创建一个 Mat 来制作直方图