我使用 opencv 的minAreaRect来校正 mnist 数字。它适用于大多数数字,但在某些情况下,未正确检测 minAreaRect 并导致数字进一步倾斜。
此代码使用的
图像:输入图像:

minAreaRect 图像:

偏斜图像:
但是,为此效果不佳:
输入图像:
minAreaRect 图像:
偏斜图像:
我想在这里提到我确实使用过:#coords = np.column_stack(np.where(thresh>0)) 但是,这根本不起作用。请使用 opencv 的 minAreaRect(Preferred) 函数提出解决方案。而且我已经用多张图像进行了测试,我确实知道问题出在 min Area Rectangle的形成上,在第二个示例中,很明显 min Area 矩形不可见(因为它穿过数字本身)。
代码如下:
import numpy as np
import cv2
image=cv2.imread('MNIST/mnist_png/testing/9/73.png')#for 4##5032,6780 #8527,2436,1391
gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)## for 9 problem with 4665,8998,73,7
gray=cv2.bitwise_not(gray)
Gblur=cv2.blur(gray,(5,5))
thresh=cv2.threshold(Gblur,0,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
#cv2.imshow("gray_thresh_blur",thresh)
#Finding Contours will be used to draw the min area rectangle
_,contours,_=cv2.findContours(thresh.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)
cnt1 = contours[0]
cnt=cv2.convexHull(contours[0])
angle = cv2.minAreaRect(cnt)[-1]
print("Actual angle …Run Code Online (Sandbox Code Playgroud)