def skew(img):
"""
This function detects skew in images. It turn the image so that the baseline of image is straight.
:param img: the image
:return: rotated image
"""
# coordinates of bottom black pixel in every column
black_pix = np.zeros((2, 1))
# Look at image column wise and in every column from bottom to top pixel. It stores the location of the first black
# pixel in every column
for columns in range(img.shape[1]):
for pixel in np.arange(img.shape[0]-1, -1, -1):
if img[pixel][columns] == 255:
black_pix = np.concatenate((black_pix, np.array([[pixel], [columns]])), axis=1)
break
# Calculate linear regression to detect baseline
mean_x = np.mean(black_pix[1][:])
mean_y = np.mean(black_pix[0][:])
k = black_pix.shape[1]
a = (np.sum(black_pix[1][:] * black_pix[0][:]) - k * mean_x * mean_y) / (np.sum(black_pix[1][:] * black_pix[1][:]) - k * mean_x * mean_x)
# Calculate angle by looking at gradient of linear function + data augmentation
angle = np.arctan(a) * 180 / np.pi #+ random.uniform(-1, 1) #TODO dataug
# Rotate image and use Nearest Neighbour for interpolation of pixel
rows, cols = img.shape
M = cv.getRotationMatrix2D((cols / 2, rows / 2), angle, 1)
img_rot = cv.warpAffine(img, M, (cols, rows), flags=cv.INTER_NEAREST)
return img_rot
preprocessor_eval.py 文件源码
python
阅读 21
收藏 0
点赞 0
评论 0
评论列表
文章目录