python - 在 OpenCV 中填充圆圈

标签 python algorithm opencv

我已经为此苦苦挣扎了一段时间。我一直在尝试在 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/

相关文章:

python - backend_qt5.py "' figure' 是一个未知的关键字参数,在 matplotlib

algorithm - 嵌套 for 循环中的迭代次数?

ios - 未找到框架——OpenCV

python - 有没有一种标准的方法来归因于/引用 python 代码?

Python从函数调用中的args的嵌套字典中提取值

algorithm - 决策制定 - ELECTRE 方法

opencv - 为什么 OpenCV 中的结构元素是不对称的?

opencv - 将 1 channel 图像转换为 3 channel

python - 编写此循环的 Pythonic 方式是什么?

java - 计算一个字符串中有多少个回文