修剪灰度图像的空白

2024-03-28 18:21:49 发布

您现在位置:Python中文网/ 问答频道 /正文

image like this

我有一个图像集,图像是这样的。在

如何使用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)

Tags: 代码图像imagenumpy内容datadirfiles
2条回答

此操作将按行顺序修剪上方和下方的空白行(实际上,它将修剪所有完整的白色行):

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

如果只需要修剪上下页边距,可以执行以下操作:

^{pr2}$

我找到了一种使用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]

相关问题 更多 >