【问题标题】:Detect angle and rotate image using HoughLine Transform使用 HoughLine 变换检测角度和旋转图像
【发布时间】:2020-08-19 22:03:37
【问题描述】:

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

我将 HoughLine 与 opencv 一起使用。

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

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

所以,当我尝试用这条线旋转这张图片然后显示它时:

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

这是输出:

这个结果与框架应该是水平的旋转不一致。 有什么建议吗?我做错了什么?

【问题讨论】:

标签: python image opencv image-processing hough-transform


【解决方案1】:

在 ndimage.rotate 中以度为单位的角度。

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

【讨论】:

  • 它可以工作,但它怎么可能是水平而不是垂直呢?这是行代码的结果:prnt.sc/sbks0a
  • 请测试:img_rotated = ndimage.rotate(image1, 180*theta/3.1415926+90) 或 img_rotated = ndimage.rotate(image1, 180*theta/3.1415926-90)
  • 非常感谢,我曾多次尝试这样做。
猜你喜欢
  • 2014-10-02
  • 2018-03-25
  • 1970-01-01
  • 2021-04-12
  • 2012-07-02
  • 1970-01-01
  • 2014-09-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多