Home > OS >  Remove Background Image with python (openCV)
Remove Background Image with python (openCV)

Time:12-02

I am new to computer version, I am trying to remove the background of the image given and make it white background. I have tried most of the codes shared here but non is working on my image.

  • Image to remove background

    desired output:

    After Removal of background (Reference Image)

    CodePudding user response:

    Chanda Steven wrote:

    I am new to computer version, I am trying to remove the background of the image given and make it white background.


    If that is all you want to do, then the following is a simple way to do that in Python/OpenCV/Numpy.

    (But your desired result looks like an inverted result. So I am not sure what you want.)

    If making the background white is all you want. Then convert the input to gray. Copy the input and use Numpy to change the background to white where gray is close to black.

    Input:

    enter image description here

    import cv2
    import numpy as np
    
    # read image
    img = cv2.imread("a_blob.jpg")
    
    # convert img to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    
    # change black to white
    result = img.copy()
    result[gray<=2] = (255,255,255)
    
    # write results to disk
    cv2.imwrite("a_blob_white.jpg", result)
    
    # show results
    cv2.imshow("RESULT", result)
    cv2.waitKey(0)
    

    Result:

    enter image description here

    CodePudding user response:

    If you remove background you not have your desider output.

    enter image description here

    The object in the image have a different processing, similar negative, but isn't negative.

    If it was negative you got this result :
    negative_image

    From output image it's very difficult understand what operations have been carried out.

  • Related