opencv+python 巧用霍夫变换实现图片旋转角度计算
opencv+python 巧用霍夫变换实现图片旋转角度计算
sskicgah 发表于2个月前
opencv+python 巧用霍夫变换实现图片旋转角度计算
• 发表于 2个月前
• 阅读 29
• 收藏 1
• 评论 0

``````# -*- coding: utf-8 -*-
import cv2
import numpy as np
from matplotlib import pyplot as plt
import math

#入参：弧度
w = src.shape[1]
h = src.shape[0]
angle = radian * 180 / np.pi
# now calculate new image width and height
# ask OpenCV for the rotation matrix
rot_mat = cv2.getRotationMatrix2D((nw*0.5, nh*0.5), angle, scale)
# calculate the move from the old center to the new center combined
# with the rotation
rot_move = np.dot(rot_mat, np.array([(nw-w)*0.5, (nh-h)*0.5,0]))
# the move only affects the translation, so update the translation
# part of the transform
rot_mat[0,2] += rot_move[0]
rot_mat[1,2] += rot_move[1]
return cv2.warpAffine(src, rot_mat, (int(math.ceil(nw)), int(math.ceil(nh))), flags=cv2.INTER_LANCZOS4)

def get_group(arr):
#按照4个弧度区间分组，返回不为空的分组数据
ret_arr = [[],[],[],[]]
for i in range(len(arr)):
ret_arr[0].append(arr[i])
ret_arr[1].append(arr[i])
ret_arr[2].append(arr[i])
else:
ret_arr[3].append(arr[i])

while [] in ret_arr:
ret_arr.remove([])

#print ret_arr
return ret_arr

def get_min_var_avg(arr):
#按照不同弧度区间分组，返回方差最小的一个分组的弧度平均值
group_arr = get_group(arr)
var_arr = []
if len(group_arr) <= 1:
var_arr = group_arr
else:
var_arr = [np.var(group_arr[i]) for i in range(len(group_arr))]
min_var = 10000
min_i = 0
for i in range(len(var_arr)):
if var_arr[i] < min_var:
min_var = var_arr[i]
min_i = i
#print min_var, i
avg = np.mean(group_arr[min_i])
return avg

#旋转弧度转换
else:

if reverse:

def rotate():

print image.shape

#高斯模糊
blur = cv2.GaussianBlur(image,(7,7),0)#自己调整，经验数据
cv2.imshow('image',blur)
cv2.waitKey(0)
#Canny边缘检测
canny = cv2.Canny(blur, 20, 150, 3)
lines = cv2.HoughLines(canny, 1, np.pi/180, 118)#自己调整，经验数据

#求平均弧度
l = len(lines[0])
print l

theta_arr = [lines[0][i][1] for i in range(l)]
print theta_arr
rotate_theta = get_min_var_avg(theta_arr)
print rotate_theta

#print lines

'''for line in lines[0]:
rho = line[0]
theta = line[1]

a = np.cos(theta)
b = np.sin(theta)
x0 = a*rho
y0 = b*rho
cv2.line(image, (int(x0 - 1000*b), int(y0 + 1000*a)), (int(x0 + 1000*b), int(y0 - 1000*a)), (0,255,0), 2)
#cv2.imshow('image',image)
#cv2.waitKey(0)'''

plt.imshow(img2)
plt.show()

if __name__ == '__main__':
rotate()``````

×