python 图像平移和旋转的实例
作者:cc0587 时间:2021-03-06 23:59:49
如下所示:
import cv2
import math
import numpy as np
def move(img):
height, width, channels = img.shape
emptyImage2 = img.copy()
x=20
y=20
for i in range(height):
for j in range(width):
if i>=x and j>=y:
emptyImage2[i,j]=img[i-x][j-y]
else:
emptyImage2[i,j]=(0,0,0)
return emptyImage2
img = cv2.imread("e:\\lena.bmp")
cv2.namedWindow("Image")
SaltImage=move(img)
cv2.imshow("Image",img)
cv2.imshow("ss",SaltImage)
cv2.waitKey(0)
旋转:
import cv2
import math
import numpy as np
def XRotate(image, angle):
h, w, channels = image.shape
anglePi = angle * math.pi / 180.0
cosA = math.cos(anglePi)
sinA = math.sin(anglePi)
X1 = math.ceil(abs(0.5 * h * cosA + 0.5 * w * sinA))
X2 = math.ceil(abs(0.5 * h * cosA - 0.5 * w * sinA))
Y1 = math.ceil(abs(-0.5 * h * sinA + 0.5 * w * cosA))
Y2 = math.ceil(abs(-0.5 * h * sinA - 0.5 * w * cosA))
hh = int(2 * max(Y1, Y2))
ww = int(2 * max(X1, X2))
emptyImage2 = np.zeros((hh, ww, channels), np.uint8)
for i in range(hh):
for j in range(ww):
x = cosA * i + sinA * j - 0.5 * ww * cosA - 0.5 * hh * sinA + 0.5 * w
y = cosA * j- sinA * i+ 0.5 * ww * sinA - 0.5 * hh * cosA + 0.5 * h
x = int(x)
y = int(y)
if x > -1 and x < h and y > -1 and y < w :
emptyImage2[i, j] = image[x, y]
return emptyImage2
image = cv2.imread("e:\\lena.bmp")
iXRotate12 = XRotate(image, 30)
cv2.imshow('image', image)
cv2.imshow('iXRotate12', iXRotate12)
cv2.waitKey(0)
来源:https://blog.csdn.net/u010096025/article/details/53780602
标签:python,图像,平移,旋转
0
投稿
猜你喜欢
URL编码与SQL注入
2007-09-26 12:41:00
利用Django-environ如何区分不同环境
2022-01-10 10:52:24
Python OpenCV实现姿态识别的详细代码
2023-05-27 23:42:31
教学演示-UBB,剪贴板,textRange及其他
2008-01-27 13:46:00
将mysql转换到oracle必须了解的50件事
2010-07-05 12:15:00
Python玩转Excel的读写改实例
2022-01-27 19:59:47
Linux操作系统中如何安装MySQL数据库
2007-10-26 15:58:00
如何让新安装的MySQL数据库变得更安全
2009-01-04 13:19:00
发一个自己用的JS框架(试用版^_^)
2011-09-11 18:53:24
很有创意的鼠标指针风筝
2007-09-22 09:24:00
我的页面制作方法
2008-03-23 13:51:00
利用Python脚本实现自动刷网课
2023-04-20 12:39:16
Python with用法实例
2022-09-07 19:34:05
基于Python绘制3D立体爱心图案的示例详解
2021-04-03 18:05:09
Gregarius中文日期格式问题解决办法
2023-11-18 09:51:00
将tensorflow模型打包成PB文件及PB文件读取方式
2022-07-02 22:47:33
FrontPage创建HTML/ASP混合页面
2008-05-08 14:26:00
Python特效之文字成像方法详解
2021-08-09 09:34:06
IIS6.0下ASP的新增功能
2007-08-22 14:55:00
python 根据正则表达式提取指定的内容实例详解
2023-07-18 01:23:48