我使用opencv的minAreaRect来对mnist位数进行去偏斜。它对于大多数数字都工作良好,但是在某些情况下,minAreaRect无法正确检测到,并且导致数字进一步倾斜。
此代码可用于的图像:
输入图片:
minAreaRect图片:
偏斜图像:
但是,这并不能很好地工作:
输入图片:
minAreaRect图片:
偏斜图像:
我想在这里提到我确实使用过的方法:#coords = np.column_stack(np.where(thresh> 0))但是,这根本没有用。 请提出使用opencv的minAreaRect(Preferred)函数的解决方案。 我已经测试了几张图像,但我确实知道问题出在最小面积矩形的形成上,在第二个示例中,很明显最小面积矩形不可见(因为它通过数字本身)。
代码如下:
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 is:"+str(angle))
rect = cv2.minAreaRect(cnt)
p=np.array(rect[1])
#print(p[0])
if p[0] < p[1]:
print("Angle along the longer side:"+str(rect[-1] + 180))
act_angle=rect[-1]+180
else:
print("Angle along the longer side:"+str(rect[-1] + 90))
act_angle=rect[-1]+90
#act_angle gives the angle with bounding box
if act_angle < 90:
angle = (90 + angle)
print("angleless than -45")
# otherwise, just take the inverse of the angle to make
# it positive
else:
angle=act_angle-180
print("grter than 90")
# rotate the image to deskew it
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated = cv2.warpAffine(image,M,(w,h),flags=cv2.INTER_CUBIC,borderMode=cv2.BORDER_REPLICATE)
box = cv2.boxPoints(rect)
print(box)
box = np.int0(box)
print(box)
p=cv2.drawContours(thresh,[box],0,(0,0,255),1)
print("contours"+str(p))
cv2.imwrite("post/MinAreaRect9.png",p)
cv2.imwrite("post/Input_9.png", image)
cv2.imwrite('post/Deskewed_9.png', rotated)