gpt4 book ai didi

python - 在 OpenCV 中填充圆圈

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:28:43 24 4
gpt4 key购买 nike

我已经为此苦苦挣扎了一段时间。我一直在尝试在 Python 中的 OpenCV 中找出某种方法来填充完全黑白图像中的圆圈。

需要说明的是,此图像已使用自适应阈值处理进行了阈值处理,但现在我有了这些我希望能够填充的圆环。理想情况下,无论用于填充圆圈的算法都应该能够用于我的两组图片包括。

如果有人可以在这方面提供任何指导,我将不胜感激。

算法之前: Before Algorithm

算法后: After Algorithm

算法之前: Before Algorithm

算法后: After Algorithm

最佳答案

在 Google 中进行简单搜索即可得到 this article ,这正是您的问题的答案。

我为您的输入采纳了该解决方案:

import cv2
import numpy as np

# Read image
im_in = cv2.imread("circles.jpg", cv2.IMREAD_GRAYSCALE)

# Threshold
th, im_th = cv2.threshold(im_in, 127, 255, cv2.THRESH_BINARY)

# Copy the thresholded image
im_floodfill = im_th.copy()

# Mask used to flood filling.
# NOTE: the size needs to be 2 pixels bigger on each side than the input image
h, w = im_th.shape[:2]
mask = np.zeros((h+2, w+2), np.uint8)

# Floodfill from point (0, 0)
cv2.floodFill(im_floodfill, mask, (0,0), 255)

# Invert floodfilled image
im_floodfill_inv = cv2.bitwise_not(im_floodfill)

# Combine the two images to get the foreground
im_out = im_th | im_floodfill_inv

# Display images.
cv2.imwrite("circles_filled.png", im_out)

输入文件circles.png:

Circles

输出文件circles_filled.png:

Filled circles

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com