python - 使用 OpenCV 对图像进行 mask 和平滑处理

标签 python opencv image-processing

以下代码,解释为here ,有效地找到图像中对象的轮廓,并用白色填充这些轮廓,同时将背景设置为黑色。

import cv2
import numpy as np
# Read image
im_in = cv2.imread('bee-02.png', cv2.IMREAD_GRAYSCALE)
th, im_th = cv2.threshold(im_in, 250, 255, cv2.THRESH_BINARY_INV)
# Copy the thresholded image.
im_floodfill = im_th.copy()
# Mask used to flood filling.
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.imshow("Foreground", im_out)
cv2.waitKey(0)

下面是它的作用的示例:

Image of a bee =>

enter image description here

增强上述代码以使图像的外部轮廓平滑的好方法是什么?如果可能的话,我想使用一种可以指定平滑渐变的方法。我猜想,一种方法是在应用 mask 功能之前应用严重模糊,但大概有更好的方法。

最佳答案

我会调查morphological opening and closing运营。具体来说,我会尝试使用一个漂亮的大圆盘运算符进行形态关闭,您可能会得到接近您想要的东西。

它们将直接对您获得的二进制数据进行操作(比模糊要便宜得多),并且可能会在简化或“模糊”视觉轮廓方面实现您正在寻找的效果。

关于python - 使用 OpenCV 对图像进行 mask 和平滑处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41385609/

相关文章:

iphone - UIImage 调整大小而不损失质量

python - 如何拆分堆栈中的行并为 Pandas 中的每个行执行 `select`?

python - 有谁知道使用 python 实现谷歌身份验证的任何好的完整资源?

python - 使用 dateutil 从 PST 转换为 UTC 的问题

matlab - 如何将图像写入文件

opencv - 在opencv中旋转裁剪

python - 在 Windows 7 32 位上安装带有 Python 绑定(bind)的 Thrift

python - 连接两个Numpy图像数组时出现问题。引发错误 “only integer scalar arrays can be converted to a scalar index”

visual-c++ - 如何使用 opencv 和 vs2012 摆脱 LNK2019 Unresolved external symbol 错误

python - OpenCV 和 Python : arithmetic operations between scalar and all pixels and channels