我得到了一个图像集,图像是这样的。
我如何使用python去除图片下面的白色部分,因为它不包含任何有用的内容?
我将图像读取到python中的numpy数组。
我的代码是这样的:
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)
发布于 2018-05-30 04:56:26
这会在上边和下边逐行修剪空白(实际上它修剪了所有整行):
trimmed = image[np.where(~np.all(image == 255, axis=1))]
如果你只需要修剪顶部和底部的页边距,你可以这样做:
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]
发布于 2018-05-30 06:44:49
我找到了一种使用openCV3和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]
https://stackoverflow.com/questions/50592552
复制相似问题