我正在尝试旋转清晰可见的图像。
我在opencv中使用HoughLine。
这里是代码如下(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)
这是输出:
此结果与框架应为水平方向的旋转不一致。有什么建议吗?我在做什么错?
img_rotated = ndimage.rotate(image1, 180*theta/3.1415926)