python 图像平移和旋转的实例


Posted in Python onJanuary 10, 2019

如下所示:

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)

以上这篇python 图像平移和旋转的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python实现文件路径和url相互转换的方法
Jul 06 Python
Python实现SSH远程登陆,并执行命令的方法(分享)
May 08 Python
python shell根据ip获取主机名代码示例
Nov 25 Python
python实现linux下抓包并存库功能
Jul 18 Python
Linux下Pycharm、Anaconda环境配置及使用踩坑
Dec 19 Python
基于pandas中expand的作用详解
Dec 17 Python
python ubplot使用方法解析
Jan 10 Python
关于TensorFlow新旧版本函数接口变化详解
Feb 10 Python
Python连接SQLite数据库并进行增册改查操作方法详解
Feb 18 Python
python支持多继承吗
Jun 19 Python
Selenium及python实现滚动操作多种方法
Jul 21 Python
使用AJAX和Django获取数据的方法实例
Oct 25 Python
Python设计模式之解释器模式原理与用法实例分析
Jan 10 #Python
详解pandas安装若干异常及解决方案总结
Jan 10 #Python
Python 从一个文件中调用另一个文件的类方法
Jan 10 #Python
关于python下cv.waitKey无响应的原因及解决方法
Jan 10 #Python
Python设计模式之迭代器模式原理与用法实例分析
Jan 10 #Python
Python设计模式之桥接模式原理与用法实例分析
Jan 10 #Python
Python基础教程之异常详解
Jan 10 #Python
You might like
thinkphp的CURD和查询方式介绍
2013/12/19 PHP
php模仿asp Application对象在线人数统计实现方法
2015/01/04 PHP
php获取客户端电脑屏幕参数的方法
2015/01/09 PHP
php通过排列组合实现1到9数字相加都等于20的方法
2015/08/03 PHP
ExtJS4 组件化编程,动态加载,面向对象,Direct
2011/05/12 Javascript
Google Maps API地图应用示例分享
2014/10/23 Javascript
基于JS实现移动端访问PC端页面时跳转到对应的移动端网页
2020/12/24 Javascript
jQuery对象的链式操作用法分析
2016/05/10 Javascript
Bootstrap企业网站实战项目4
2016/10/14 Javascript
JavaScript中的this陷阱的最全收集并整理(没有之一)
2017/02/21 Javascript
NodeJS仿WebApi路由示例
2017/02/28 NodeJs
jquery实现的table排序功能示例
2017/03/10 Javascript
node.js中EJS 模板快速入门教程
2017/05/08 Javascript
ES6 javascript中class静态方法、属性与实例属性用法示例
2017/10/30 Javascript
微信小程序实现聊天对话(文本、图片)功能
2018/07/06 Javascript
vue-image-crop基于Vue的移动端图片裁剪组件示例
2018/08/28 Javascript
jQuery选择器选中最后一个元素,倒数第二个元素操作示例
2018/12/10 jQuery
es6数值的扩展方法
2019/03/11 Javascript
阿望教你用vue写扫雷小游戏
2020/01/20 Javascript
webpack+vue.js构建前端工程化的详细教程
2020/05/10 Javascript
Vue实现背景更换颜色操作
2020/07/17 Javascript
[47:04]LGD vs infamous Supermajor小组赛D组 BO3 第二场 6.3
2018/06/04 DOTA
用python写个自动SSH登录远程服务器的小工具(实例)
2017/06/17 Python
Python3利用SMTP协议发送E-mail电子邮件的方法
2017/09/30 Python
TensorFlow深度学习之卷积神经网络CNN
2018/03/09 Python
Python读写及备份oracle数据库操作示例
2018/05/17 Python
用python编写第一个IDA插件的实例
2018/05/29 Python
Pytorch to(device)用法
2020/01/08 Python
HTML5 Canvas draw方法制作动画效果示例
2013/07/11 HTML / CSS
韩国现代百货官网:Hmall
2018/03/21 全球购物
L’Artisan Parfumeur官网:法国香水品牌
2020/08/11 全球购物
程序员机试试题汇总
2012/03/07 面试题
毕业自我鉴定范文
2013/11/06 职场文书
学校先进集体事迹材料
2014/05/31 职场文书
2015年政府采购工作总结
2015/05/21 职场文书
2016秋季校长开学典礼致辞
2015/11/26 职场文书