环形抓手的角度与opencv python

2024-04-16 14:04:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道拉环的角度,但我还不能。你知道吗

用opencvpython做这个有什么想法吗?你知道吗

Photo


Tags: 角度opencvpython
1条回答
网友
1楼 · 发布于 2024-04-16 14:04:38

你可以试着用HoughLinesP()来找出那条线,然后计算出这条线的角度。如果需要旋转图像,则可以将其传递给ndimage.rotate()

import numpy as np
import cv2
import math
from scipy import ndimage

img_before = cv2.imread('find_angle.jpg')
img_gray = cv2.cvtColor(img_before, cv2.COLOR_BGR2GRAY)
img_edges = cv2.Canny(img_gray, 100, 100, apertureSize=3)
lines = cv2.HoughLinesP(img_edges, 1, math.pi / 180.0, 100, minLineLength=80, maxLineGap=5)

angles = []

for x1, y1, x2, y2 in lines[0]:
    cv2.line(img_before, (x1, y1), (x2, y2), (255, 0, 0), 3)
    angle = math.degrees(math.atan2(y2 - y1, x2 - x1))
    angles.append(90 + angle)

median_angle = np.median(angles)
img_rotated = ndimage.rotate(img_before, median_angle)

cv2.imshow("Result", img_rotated)   
key = cv2.waitKey(0)

cv2.imwrite('rotated.jpg', img_rotated)    

这将为您提供使用11.876角度的输出图像:

rotate ring pull

不过,对于其他图像,这种方法还需要进一步调整。你知道吗

相关问题 更多 >