如何去除手写mnist数据中的边框

问题描述 投票:0回答:1

enter image description here

就像这张图片,我想删除边框,但我不知道如何删除它们,, 如何才能让只有 0 和 1 值的图像具有 0 到 255 的值?

enter image description here

我尝试过: 我把图像中的数字放在中间。 计算每个图像的水平和垂直投影。 使用高斯、轮廓

machine-learning mnist
1个回答
0
投票

这是我尝试解决您的问题的方法,尝试一下看看是否有效。

import cv2
import numpy as np

# Read the image
image = cv2.imread('your_image.png', cv2.IMREAD_GRAYSCALE)

# Compute horizontal and vertical projections
horizontal_projection = np.sum(image, axis=1)
vertical_projection = np.sum(image, axis=0)

# Find non-zero indices in the projections
non_zero_rows = np.where(horizontal_projection > 0)[0]
non_zero_cols = np.where(vertical_projection > 0)[0]

# Crop the image based on non-zero indices
cropped_image = image[non_zero_rows[0]:non_zero_rows[-1]+1, non_zero_cols[0]:non_zero_cols[-1]+1]

# Display or save the cropped image
cv2.imshow('Cropped Image', cropped_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

要获得仅包含 0 和 1 值的图像,其值为 1 到 255,您可以 做这样的事情。

scaled_image = original_image * 255

这会将 0 映射到 1 到 1 到 255,但请确保图像的数据类型支持此缩放,例如使用类似的东西。

scaled_image = (original_image * 255).astype(np.uint8)
© www.soinside.com 2019 - 2024. All rights reserved.