我正在使用python中的OCR应用程序读取数字。我正在使用OpenCV在图像上找到轮廓,对其进行裁剪,然后针对MNIST数据集将图像预处理为28x28。我的图像不是方形的,因此在调整图像大小时似乎失去了很多质量。我可以尝试的任何提示或建议吗?
我尝试了http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_morphologic_ops/py_morphologic_ops.html中的一些技巧,例如“扩张”和“开放”。但这并不能使它变得更好,它只能使它变得模糊。
这是我使用的代码(查找轮廓,裁剪,调整大小,然后阈值,然后居中)
import numpy as np
import cv2
import imutils
import scipy
from imutils.perspective import four_point_transform
from scipy import ndimage
images = np.zeros((4, 784))
correct_vals = np.zeros((4, 10))
i = 0
def getBestShift(img):
cy, cx = ndimage.measurements.center_of_mass(img)
rows, cols = img.shape
shiftx = np.round(cols / 2.0 - cx).astype(int)
shifty = np.round(rows / 2.0 - cy).astype(int)
return shiftx, shifty
def shift(img, sx, sy):
rows, cols = img.shape
M = np.float32([[1, 0, sx], [0, 1, sy]]) …Run Code Online (Sandbox Code Playgroud)