首页 > 解决方案 > 手写数字图像在轮廓和调整大小后无法识别

问题描述

我正在使用以下代码对 jpeg 图像进行阈值处理 -

image = cv.imread('/content/drive/My Drive/Colab Notebooks/digit-recognition/hand-written-digits-1.jpg')
grey = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
grey = cv.GaussianBlur(grey,(5,5),0)
# grey = cv.medianBlur(grey,5)
# thresh = cv.adaptiveThreshold(grey,255,cv.ADAPTIVE_THRESH_MEAN_C,cv.THRESH_BINARY_INV,11,2)
thresh = cv.adaptiveThreshold(grey,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY_INV,7,2)
contours, hierarchy = cv.findContours(thresh, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
# contours.sort(key=lambda x:get_contour_precedence(x, thresh.shape[1]))

plt.imshow(thresh, cmap="gray")
plt.show()
preprocessed_digits = []
for c in contours:
    x,y,w,h = cv.boundingRect(c)
    if w*h >= 130: 
        cv.rectangle(image, (x,y), (x+w, y+h), color=(0, 255, 0), thickness=2)
        digit = thresh[y:y+h, x:x+w]
        resized_digit = cv.resize(digit, (18,18))
        padded_digit = np.pad(resized_digit, ((5,5),(5,5)), "constant", constant_values=0)
        plt.imshow(padded_digit, cmap="gray")
        plt.show()
        xdigit = padded_digit.reshape(1,784)
        prediction = neigh.predict(xdigit)
        print("prediction = ",prediction[0])

这是脱粒图像 -
在此处输入图像描述

这是轮廓和调整大小后数字 5 的图像 -
在此处输入图像描述

如果您在 thresh 图像中注意到 5 正确显示。而在轮廓和调整大小之后,它看起来不正确,因此被预测为 3。
这个不正确的图像可能是什么原因,或者我该如何修复它?

标签: pythonimage-recognitionopencv-contour

解决方案


推荐阅读