Search code examples
pythondeep-learningcomputer-visionimage-segmentationbackground-subtraction

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


I have an image processing problem that I can't solve. I have a set of 375 images like the one below (1). I'm trying to remove the background, so to make "background substraction" (or "foreground extraction") and get only the waste on a plain background (black/white/...).

(1) Image example

I tried many things, including createBackgroundSubtractorMOG2 from OpenCV, or threshold. I also tried to remove the background pixel by pixel by subtracting it from the foreground because I have a set of 237 background images (2) (the carpet without the waste, but which is a little bit offset from the image with the objects). There are also variations in brightness on the background images.

(2) Example of a background image

Here is a code example that I was able to test and that gives me the results below (3) and (4). I use 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)

I was inspired by many other cases found on Stackoverflow or others (example: removing pixels less than n size(noise) in an image - open CV python).

(3) The result obtained with the code above

(4) Result when increasing the varThreshold argument to 10

Unfortunately, there is still a lot of noise on the resulting pictures.

As a beginner in "background substraction", I don't have all the keys to get an optimal solution. If someone would have an idea to do this task in a more efficient and clean way (Is there a special method to handle the case of transparent objects? Can noise on objects be eliminated more effectively? etc.), I'm interested :) Thanks


Solution

  • Thanks for your answers. For information, I simply change of methodology and use a segmentation model (U-Net) with 2 labels (foreground, background), to identify the background. It works quite well.