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程序的方法
Apr 21 Python
Python实现在线程里运行scrapy的方法
Apr 07 Python
Python操作MongoDB数据库PyMongo库使用方法
Apr 27 Python
Mac 上切换Python多版本
Jun 17 Python
python实现将excel文件转化成CSV格式
Mar 22 Python
python读取tif图片时保留其16bit的编码格式实例
Jan 13 Python
Python实现aes加密解密多种方法解析
May 15 Python
什么是Python变量作用域
Jun 03 Python
基于TensorFlow的CNN实现Mnist手写数字识别
Jun 17 Python
详解Python 函数参数的拆解
Sep 02 Python
如何基于pandas读取csv后合并两个股票
Sep 25 Python
如何使用python自带IDLE的几种方法
Oct 10 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
PHP array操作10个小技巧分享
2011/06/23 PHP
PHP OPP机制和模式简介(抽象类、接口和契约式编程)
2014/06/09 PHP
Codeigniter出现错误提示Error with CACHE directory的解决方案
2014/06/12 PHP
ThinkPHP实现ajax仿官网搜索功能实例
2014/12/02 PHP
php使用curl简单抓取远程url的方法
2015/03/13 PHP
PHP打印输出函数汇总
2016/08/28 PHP
php实现登陆模块功能示例
2016/10/20 PHP
CodeIgniter整合Smarty的方法详解
2017/08/25 PHP
php实现登录页面的简单实例
2019/09/29 PHP
深入理解JavaScript编程中的原型概念
2015/06/25 Javascript
js日期相关函数dateAdd,dateDiff,dateFormat等介绍
2016/09/24 Javascript
基于Angularjs+mybatis实现二级评论系统(仿简书)
2017/02/13 Javascript
从零开始学习Node.js系列教程二:文本提交与显示方法
2017/04/13 Javascript
nodeJS微信分享
2017/12/20 NodeJs
Vue实现底部侧边工具栏的实例代码
2018/09/03 Javascript
JS删除String里某个字符的方法
2021/01/06 Javascript
Vue 2.0 侦听器 watch属性代码详解
2019/06/19 Javascript
JQuery插件tablesorter表格排序实现过程解析
2020/05/28 jQuery
TypeScript魔法堂之枚举的超实用手册
2020/10/29 Javascript
Vue指令实现OutClick的示例
2020/11/16 Javascript
Python记录详细调用堆栈日志的方法
2015/05/05 Python
详解python中executemany和序列的使用方法
2017/08/12 Python
python使用fork实现守护进程的方法
2017/11/16 Python
TensorFlow实现卷积神经网络
2018/05/24 Python
python中的变量如何开辟内存
2018/06/26 Python
python实现linux下抓包并存库功能
2018/07/18 Python
对python中assert、isinstance的用法详解
2019/11/27 Python
美国南加州的原创极限运动潮牌:Vans(范斯)
2016/08/05 全球购物
埃弗顿足球俱乐部官方网上商店:Everton Direct
2018/01/13 全球购物
纽约市的奢华内衣目的地:Anya Lust
2019/08/02 全球购物
出纳会计岗位职责
2014/03/12 职场文书
装修施工安全责任书
2014/07/24 职场文书
群众路线党员个人整改措施
2014/10/27 职场文书
2015年小学教科研工作总结
2015/07/20 职场文书
升学宴祝酒词
2015/08/11 职场文书
教师岗位说明书
2015/09/30 职场文书