python用opencv实现图像旋转_Python OpenCV实例:图像旋转(数学公式简单实现)

#coding:utf-8

'''

图像旋转

'''

import numpy as np

import cv2

def image_rotate(image,angle):

alpha = angle * np.pi / 180.0

# 旋转矩阵

rotate_matrix = [

[np.cos(alpha),-1 * np.sin(alpha),0],

[np.sin(alpha), np.cos(alpha),0],

[0,0,1]

]

rows = image.shape[0]

cols = image.shape[1]

# 图像旋转后顶点计算

a1 = cols * rotate_matrix[0][0]

b1 = cols * rotate_matrix[1][0]

a2 = cols * rotate_matrix[0][1] + rows * rotate_matrix[0][1]

b2 = cols * rotate_matrix[1][0] + rows * rotate_matrix[1][1]

a3 = rows * rotate_matrix[0][1]

b3 = rows * rotate_matrix[1][1]

# 计算极值点

x_min = min(min(min(0.0,a1),a2),a3)

x_max = max(max(max(0.0,a1),a2),a3)

y_min = min(min(min(0.0,b1),b2),b3)

y_max = max(max(max(0.0,b1),b2),b3)

out_rows = int(np.abs(x_max - x_min))

out_cols = int(np.abs(y_max - y_min))

print('src size:(%d,%d)' %(rows,cols))

print('new size:(%d,%d)' % (out_rows,out_cols))

dist = np.zeros((out_rows,out_cols,image.shape[2]),image.dtype)

for i in range(out_rows):

for j in range(out_cols):

x = int((j + x_min) * rotate_matrix[0][0] - (i + y_min) * rotate_matrix[0][1])

y = int(-1 * (j + x_min) * rotate_matrix[1][0] + (i + y_min) * rotate_matrix[1][1])

if x >= 0 and y >= 0 and x < rows and y < cols:

# print('i = %d,j = %d;x = %d,y = %d' % (i,j,x,y))

dist[i,j] = image[x,y]

return dist

image = cv2.imread('datas/l1.jpg')

result = image_rotate(image,180)

cv2.imshow('src',image)

cv2.imshow('rotate:60',cv2.resize(result,(int(result.shape[0] / 2),int(result.shape[1] / 2))))

cv2.waitKey()

cv2.destroyAllWindows()

注意:本文归作者所有,未经作者允许,不得转载

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值