0

image like this

I get a images set, and the images are like this.

How can I remove the beneath white part of the image using python, which doesn't contain any useful content?

I read the image to numpy array in python.

My code is like this:

data_dir = "/Users/leon/Projects/inpainting/data/"
images = []
files = glob.glob (data_dir + "*.jpg")
for file in files:
    image = cv2.imread(file, 0)
    images.append(image)
Hao Chen
  • 174
  • 1
  • 4
  • 13

2 Answers2

1

This trims white space row-wise both above and below (actually it trims any full white row):

trimmed = image[np.where(~np.all(image == 255, axis=1))]

If you need to trim just the top and bottom margins you can do:

empty_row_mask = np.all(image == 255, axis=1)
top = np.searchsorted(~empty_row_mask, True)
bottom = np.searchsorted(empty_row_mask, True)
trimmed = image[top:bottom]
filippo
  • 5,197
  • 2
  • 21
  • 44
  • thank you, I find a way which can find the most suitable window for trimming the undesired white part both sides – Hao Chen May 29 '18 at 22:46
0

I find a way to do it using openCV3 and python3.6

image_neg = cv2.bitwise_not(image) # invert the root to white
coords = cv2.findNonZero(image_neg) # find all the non-zero points (root)
x, y, w, h = cv2.boundingRect(coords) # find minimum spanning bounding box
rect = image[y:y+h, x:x+w]
Hao Chen
  • 174
  • 1
  • 4
  • 13