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

问题描述 投票:0回答:1

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

我在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

rho:311.0

enter image description hereenter image description here

所以,当我尝试使用此行旋转此图像然后显示它时:

img_rotated = ndimage.rotate(image1, theta)
cv2_imshow(img_rotated)

这是输出:

enter image description here

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

python image opencv image-processing hough-transform
1个回答
0
投票
img_rotated = ndimage.rotate(image1, 180*theta/3.1415926)
© www.soinside.com 2019 - 2024. All rights reserved.