Bathtub
Bathtub

Reputation: 111

How to remove white borders around X-ray image with OpenCV Python

I have thousands of X-ray images of the left hand. All of these images have thin white borders surrounding them.

I want to create a function (OpenCV/numpy) that will allow me to just auto remove/crop these thin white borders out or replace them with black/gray color.

enter image description here

enter image description here

First image is the original image, second image is the output with white borders removed. Is this even possible?

Note: For some images, the white borders aren't always surrounding the image, instead there can be white bars on the sides of the image --> Examples

Upvotes: 1

Views: 777

Answers (2)

user1196549
user1196549

Reputation:

Look carefully, the image was just cropped. It is smaller.

enter image description here

Upvotes: 2

Hihikomori
Hihikomori

Reputation: 990

This code will do the job:

import cv2


def fix_border(image,x_pad,y_pad,col):
    image=image.copy()
    image[:y_pad,:]=col
    image[-y_pad:,:]=col
    image[:,:x_pad] = col
    image[ :,-x_pad:] = col
    return image


img = cv2.imread("Path to image")

img = fix_border(img,20,20,(50,50,50))
cv2.imshow("",img)
cv2.waitKey()

Upvotes: 1

Related Questions