Search code examples
pythonopencvimage-processingdetection

remove the element attached to the image border


I'm using OpenCV to detect Pneumonia in chest-x-ray using Image Processing, so I need to remove the attached area to the image border to get the lung only, can anyone help me code this in python?

This image explains what I want this image after applying this methods: resized, Histogram Equalization, otsu Thresholded and inverse binary Thresholded, morphological processes(opening then closing)

This is the Original Image Original Image


Solution

  • This is how I would approach the problem in Python/OpenCV. Add a white border all around, flood fill it with black to replace the white, then remove the extra border.

    Input:

    enter image description here


    import cv2
    import numpy as np
    
    # read image
    img = cv2.imread('lungs.jpg')
    h, w = img.shape[:2]
    
    # convert to gray
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    
    # add 1 pixel white border all around
    pad = cv2.copyMakeBorder(gray, 1,1,1,1, cv2.BORDER_CONSTANT, value=255)
    h, w = pad.shape
    
    # create zeros mask 2 pixels larger in each dimension
    mask = np.zeros([h + 2, w + 2], np.uint8)
    
    # floodfill outer white border with black
    img_floodfill = cv2.floodFill(pad, mask, (0,0), 0, (5), (0), flags=8)[1]
    
    # remove border
    img_floodfill = img_floodfill[1:h-1, 1:w-1]    
    
    # save cropped image
    cv2.imwrite('lungs_floodfilled.png',img_floodfill)
    
    # show the images
    cv2.imshow("img_floodfill", img_floodfill)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    enter image description here