繁体   English   中英

如何去除嘈杂图像的背景并提取透明对象?

[英]How to remove the background of a noisy image and extract transparent objects?

我有一个无法解决的图像处理问题。 我有一组 375 张图像,如下所示 (1)。 我正在尝试删除背景,因此要进行“背景减法”(或“前景提取”)并仅获得纯背景(黑色/白色/...)上的废物。

(1) 图像示例

我尝试了很多东西,包括来自 OpenCV 的 createBackgroundSubtractorMOG2 或阈值。 我还尝试通过从前景中减去背景来逐个像素地删除背景,因为我有一组 237 个背景图像(2)(没有浪费的地毯,但与带有物体的图像有点偏移)。 背景图像的亮度也有变化。

(2) 背景图像示例

这是我能够测试的代码示例,它为我提供了以下 (3) 和 (4) 的结果。 我使用 Python 3.8.3。

# Function to remove the sides of the images
def delete_side(img, x_left, x_right):
    for i in range(img.shape[0]):
        for j in range(img.shape[1]):
            if j<=x_left or j>=x_right:
                img[i,j] = (0,0,0)
    return img

# Intialize the background model
backSub = cv2.createBackgroundSubtractorMOG2(history=250, varThreshold=2, detectShadows=True)

# Read the frames and update the background model
for frame in frames:
    if frame.endswith(".png"):
        filepath = FRAMES_FOLDER + '/' + frame
        img = cv2.imread(filepath)
        img_cut = delete_side(img, x_left=190, x_right=1280)
        gray = cv2.cvtColor(img_cut, cv2.COLOR_BGR2GRAY)
        mask = backSub.apply(gray)
        newimage = cv2.bitwise_or(img, img, mask=mask)
        img_blurred = cv2.GaussianBlur(newimage, (5, 5), 0)
        gray2 = cv2.cvtColor(img_blurred, cv2.COLOR_BGR2GRAY)
        _, binary = cv2.threshold(gray2, 10, 255, cv2.THRESH_BINARY)
        final = cv2.bitwise_or(img, img, mask=binary)
        newpath = RESULT_FOLDER + '/' + frame
        cv2.imwrite(newpath, final)

我受到 Stackoverflow 或其他人发现的许多其他案例的启发(例如: 删除图像中小于 n 大小(噪声)的像素 - 打开 CV python )。

(3) 上面代码得到的结果

(4) 将 varThreshold 参数增加到 10 时的结果

不幸的是,结果图片上仍然有很多噪音。

作为“背景减法”的初学者,我没有获得最佳解决方案的所有关键。 如果有人想以更有效和更干净的方式完成这项任务(是否有特殊方法来处理透明物体的情况?可以更有效地消除物体上的噪音吗?等等),我很感兴趣:)谢谢

感谢您的回答。 有关信息,我只是更改方法并使用带有 2 个标签(前景、背景)的分割模型 (U-Net) 来识别背景。 它运作良好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM