Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def deskew(img, SZ, bbox=16):
- m = cv2.moments(img)
- if abs(m['mu02']) < 1e-2 or m['m00'] < 1:
- return img.copy()
- skew = m['mu11']/m['mu02']
- cx, cy = m['m10']/m['m00'], m['m01']/m['m00']
- M1 = np.float32([
- [1, -skew],
- [0, 1]
- ])
- b1 = np.float32([
- [0.5*SZ*skew],
- [0]
- ])
- def deskew_pt((x, y)):
- return x-skew*y+0.5*SZ*skew, y
- _cx, _cy = deskew_pt((cx, cy))
- ctr = SZ/2
- margin = (SZ-bbox)/2
- if _cy <= img.shape[0]/2:
- # center of mass is above the center of the image
- # the bottom part is longer, so scaling will be counted from that
- scale = (bbox/2) / (img.shape[0]-1-_cy)
- vproj = deskew_pt((cx, img.shape[0]-1))
- hproj = deskew_pt((0, cy))
- src_tri = np.float32([(_cx, _cy), (_cx, vproj[1]), (hproj[0], _cy)])
- dst_tri = np.float32([(ctr, ctr), (ctr, SZ-1-margin), (ctr-(img.shape[1]/2)*scale, ctr)])
- else:
- # the top part is longer
- scale = (bbox/2) / _cy
- vproj = deskew_pt((cx, 0))
- hproj = deskew_pt((0, cy))
- src_tri = np.float32([(_cx, _cy), (_cx, vproj[1]), (hproj[0], _cy)])
- dst_tri = np.float32([(ctr, ctr), (ctr, margin), (ctr-(img.shape[1]/2)*scale, ctr)])
- MM = cv2.getAffineTransform(src_tri, dst_tri)
- M2 = MM[:, 0:2]
- b2 = MM[:, 2:]
- M = np.empty((2, 3))
- M[:, 0:2] = np.dot(M2, M1)
- M[:, 2:] = b2 + np.dot(M2, b1)
- img = cv2.warpAffine(img, M, (SZ, SZ), flags=cv2.INTER_CUBIC)
- return img
Add Comment
Please, Sign In to add comment