python - 手写数字图像在轮廓和调整大小后无法识别
问题描述
我正在使用以下代码对 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])
如果您在 thresh 图像中注意到 5 正确显示。而在轮廓和调整大小之后,它看起来不正确,因此被预测为 3。
这个不正确的图像可能是什么原因,或者我该如何修复它?
解决方案
推荐阅读
- sql - 无法使用转换为变量赋值
- python - 如何使用 sklearn.cluster.MeanShift 按升序排列均值移位集群标签?
- azure-devops - 已安装市场扩展,但在 https://dev.azure.com/ 中不可用
- python - TensorFlow 无缘无故分批
- php - 尝试在 laravel voyager 中编辑和添加帖子时出错
- linux - rust 程序的特定功能仅适用于 Windows
- c# - 如何禁用主菜单中的所有 ui 按钮?
- node.js - Express JS res.send() 然后 res.redirect
- python - 检查字符串是否为大于 1 的数字
- python - 在 python 烧瓶之间传递文件