【发布时间】:2021-04-13 14:55:40
【问题描述】:
我正在尝试准备几张从不同角度拍摄的乐谱图像,以便我可以进行 OMR。
我尝试过变换和旋转它们,但没有帮助,我想我需要调整图像的视角
我试过了:
def rotate_image(mat, angle):
height, width = mat.shape[:2] # image shape has 3 dimensions
image_center = (width/2, height/2) # getRotationMatrix2D needs coordinates in reverse order (width, height) compared to shape
rotation_mat = cv2.getRotationMatrix2D(image_center, angle, 1.)
# rotation calculates the cos and sin, taking absolutes of those.
abs_cos = abs(rotation_mat[0,0])
abs_sin = abs(rotation_mat[0,1])
# find the new width and height bounds
bound_w = int(height * abs_sin + width * abs_cos)
bound_h = int(height * abs_cos + width * abs_sin)
# subtract old image center (bringing image back to origo) and adding the new image center coordinates
rotation_mat[0, 2] += bound_w/2 - image_center[0]
rotation_mat[1, 2] += bound_h/2 - image_center[1]
# rotate image with the new bounds and translated rotation matrix
rotated_mat = cv2.warpAffine(mat, rotation_mat, (bound_w, bound_h), borderValue=[255,255,255,0])
return rotated_mat
# the first call
def our_rotate(RGBImage):
angle = getAngle(RGBImage)
rot = rotate_image(RGBImage,angle)
return rot
【问题讨论】:
-
“我尝试了很多方法来修复这张图片的视角”,展示你尝试过的How to Ask
-
我将通过编写我尝试过的所有算法来继续编辑帖子
-
如果您可以标记或检测四个角,则使用 opencv 的单应变换应该可以很好地处理这个问题。稍后我会发布更详细的回复作为答案。
标签: python python-3.x opencv image-processing scikit-image