python - 如何确定图像的角度以检查图像是垂直的还是水平的?
问题描述
我正在尝试将所有图像对齐为垂直/水平。我必须使用以下代码旋转图像:
import cv2
import imutils
import numpy as np
def rotate_bound(image, angle):
# grab the dimensions of the image and then determine the
# center
(h, w) = image.shape[:2]
(cX, cY) = (w // 2, h // 2)
# grab the rotation matrix (applying the negative of the
# angle to rotate clockwise), then grab the sine and cosine
# (i.e., the rotation components of the matrix)
M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
cos = np.abs(M[0, 0])
sin = np.abs(M[0, 1])
# compute the new bounding dimensions of the image
nW = int((h * sin) + (w * cos))
nH = int((h * cos) + (w * sin))
# adjust the rotation matrix to take into account translation
M[0, 2] += (nW / 2) - cX
M[1, 2] += (nH / 2) - cY
# perform the actual rotation and return the image
return cv2.warpAffine(image, M, (nW, nH))
image = cv2.imread("10247.png")
# loop over the rotation angles again, this time ensuring
# no part of the image is cut off
dim=(600,400)
im=rotate_bound(image,30)
im = cv2.resize(im, dim, interpolation = cv2.INTER_AREA)
cv2.imshow("Rotated (Correct)", im)
cv2.waitKey(0)
cv2.destroyWindow("Rotated (Correct)")
for angle in np.arange(0, 360, 15):
rotated = imutils.rotate_bound(image, angle)
dim=(800,600)
resized = cv2.resize(rotated, dim, interpolation = cv2.INTER_AREA)
cv2.imshow("Rotated (Correct)", resized)
cv2.waitKey(0)
cv2.destroyWindow("Rotated (Correct)")
我希望在图像垂直/水平时停止旋转。我该如何阻止它?如何确定当前角度?
解决方案
您可以使用 MediaPipe介绍一些AI 。从那里你得到手指的地标并旋转,例如食指向上。
类似的操作可能是将图像转换为黑白,并使用形态学操作 侵蚀将手臂缩小为仅线条。
然后使用霍夫线变换得到手指,对它们进行排序(从左到右),得到与食指垂直的角度并旋转。
推荐阅读
- angular - 如何使用 Angular CLI 7.0.0-RC2 正确创建库项目
- java - Java 正则表达式是否支持排序序列?
- c++ - 你能专门化 std::unique_lock
- xslt - 有条件地修改多个同名子项中的文本
- javascript - Javascript:通过 ajax 传递创建的图像。
- .net - 暂停 GACUTIL.exe 列表
- javascript - 使用 d3.nest 实现 D3 树图 json
- python - 如何使用exec根据Python3中的字典将列表分配给变量?
- python - 如何将主题公开给 django 中的所有用户?
- python - 将生成器中的项目附加到列表