【问题标题】:Trim the whitespace of the grayscale image [closed]修剪灰度图像的空白[关闭]
【发布时间】:2018-11-08 14:13:43
【问题描述】:

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)

【问题讨论】:

标签: python image numpy deep-learning


【解决方案1】:

这会逐行修剪上方和下方的空白(实际上它会修剪任何完整的白色行):

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]

【讨论】:

  • 谢谢,我找到了一种方法,可以找到最合适的窗口来修剪两边不需要的白色部分
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2019-12-02
    • 2012-08-07
    • 1970-01-01
    • 2012-03-19
    • 2021-08-15
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    • 2018-08-26
    相关资源
    最近更新 更多