⏳ 1 mins read time
Revealjs presentation available in GitPitch
Deskewing simple grayscale images can be achieved using image moments (distance and intensity of pixels).
def deskew(img): m = cv2.moments(img) if abs(m['mu02']) < 1e-2: # no deskewing needed. return img.copy() # Calculate skew based on central momemts. skew = m['mu11']/m['mu02'] # Calculate affine transform to correct skewness. M = np.float32([[1, skew, -0.5*SZ*skew], [0, 1, 0]]) # Apply affine transform img = cv2.warpAffine(img, M, (SZ, SZ), flags=cv2.WARP_INVERSE_MAP | cv2.INTER_LINEAR) return img
This deskewing of simple grayscale images can be achieved using image moments. OpenCV has an implementation of moments and it comes in handy while calculating useful information like centroid, area, skewness of simple images with black backgrounds.
It turns out that a measure of the skewness is the given by the ratio of the two central moments ( mu11 / mu02 ). The skewness thus calculated can be used in calculating an affine transform that deskews the image.
Increase image contrast using the image’s histogram.
and share it with your friends: