Search code examples
imageopencvscikit-imagemedicalimage-preprocessing

How to remove a part of XRAY png image using skimage or opencv?


Chest X-Ray Image I need to remove the extra black part in the png image using skimage on OpenCV. Can someone guide to how to do it? I am new to image processing. Thanks in advance.


Solution

  • You can solve this by finding the first and last rows / columns that are not fully black. With these you can easily create a new image that only holds the region of interest.

    The code below does what you want using openCV.

    Result: Result

    ! Note the resize function. Remove it for the final processing.

    import numpy as np
    import cv2
    
    # Load a color image as grayscale
    img = cv2.imread('Xray.png',0)
    
    # resize to easily view (img is ~4000x4000), remove for final version
    img = cv2.resize(img,None,fx=0.2, fy=0.2, interpolation = cv2.INTER_CUBIC)
    
    # sum each row and each volumn of the image
    sumOfCols = np.sum(img, axis=0)
    sumOfRows = np.sum(img, axis=1)
    
    # Find the first and last row / column that has a sum value greater than zero, 
    # which means its not all black. Store the found values in variables
    for i in range(len(sumOfCols)):
        if sumOfCols[i] > 0:
            x1 = i
            print('First col: ' + str(i))
            break
    
    for i in range(len(sumOfCols)-1,-1,-1):
        if sumOfCols[i] > 0:
            x2 = i
            print('Last col: ' + str(i))
            break
    
    for i in range(len(sumOfRows)):
        if sumOfRows[i] > 0:
            y1 = i
            print('First row: ' + str(i))
            break
    
    for i in range(len(sumOfRows)-1,-1,-1):
        if sumOfRows[i] > 0:
            y2 = i
            print('Last row: ' + str(i))
            break
    
    # create a new image based on the found values
    roi = img[y1:y2,x1:x2]
    
    # save new image with region of interest
    cv2.imwrite('Xray_roi.png',roi)
    
    # display image / subimage and release resources when key is pressed
    cv2.imshow('full_image',img)
    cv2.imshow('RoI',roi)
    cv2.waitKey(0)
    cv2.destroyAllWindows()