Reputation: 111
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.
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
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