gpt4 book ai didi

python - 使用HoughLine变换检测角度并旋转图像

转载 作者:行者123 更新时间:2023-12-02 17:22:18 25 4
gpt4 key购买 nike

我正在尝试旋转清晰可见的图像。

我在opencv中使用HoughLine。

这是带有以下代码的图像(working in google colab):

enter image description here

import numpy as np
import cv2
from scipy import ndimage
from google.colab.patches import cv2_imshow

image1 = cv2.imread('/content/rotate.png')
gray=cv2.cvtColor(image1,cv2.COLOR_BGR2GRAY)

edges = cv2.Canny(gray,50,150,apertureSize = 3)

canimg = cv2.Canny(gray, 50, 200)
lines= cv2.HoughLines(canimg, 1, np.pi/180.0, 250, np.array([]))
#lines= cv2.HoughLines(edges, 1, np.pi/180, 80, np.array([]))
for line in lines:
rho, theta = line[0]
a = np.cos(theta)
b = np.sin(theta)
x0 = a*rho
y0 = b*rho
x1 = int(x0 + 1000*(-b))
y1 = int(y0 + 1000*(a))
x2 = int(x0 - 1000*(-b))
y2 = int(y0 - 1000*(a))

cv2.line(image1,(x1,y1),(x2,y2),(0,0,255),2)
print(theta)
print(rho)
cv2_imshow(image1)
cv2_imshow(edges)

这是输出:

θ:0.9773844

相对湿度:311.0

enter image description here
enter image description here

因此,当我尝试使用此行旋转此图像然后显示时:
img_rotated = ndimage.rotate(image1, theta)
cv2_imshow(img_rotated)

这是输出:

enter image description here

此结果与框架应为水平方向的旋转不一致。
有什么建议吗?我究竟做错了什么?

最佳答案

在ndimage.rotate Angular (以度为单位)。

img_rotated = ndimage.rotate(image1, 180*theta/3.1415926)

关于python - 使用HoughLine变换检测角度并旋转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61609845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com