簡體   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