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之入门(五)缩进和选择
May 27 Python
python中类的一些方法分析
Sep 25 Python
Python实现基于权重的随机数2种方法
Apr 28 Python
python下MySQLdb用法实例分析
Jun 08 Python
python+django+sql学生信息管理后台开发
Jan 11 Python
遗传算法python版
Mar 19 Python
Django Web开发中django-debug-toolbar的配置以及使用
May 06 Python
Python类装饰器实现方法详解
Dec 21 Python
python内置函数sorted()用法深入分析
Oct 08 Python
用Python去除图像的黑色或白色背景实例
Dec 12 Python
pytorch 归一化与反归一化实例
Dec 31 Python
如何在keras中添加自己的优化器(如adam等)
Jun 19 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_MySQL教程-第一天
2007/03/18 PHP
php设计模式 Observer(观察者模式)
2011/06/26 PHP
基于php和mysql的简单的dao类实现crud操作功能
2014/01/27 PHP
php写入数据到CSV文件的方法
2015/03/14 PHP
详解PHP素材图片上传、下载功能
2019/04/12 PHP
通过Javascript读取本地Excel文件内容的代码示例
2014/04/08 Javascript
详解Javascript 装载和执行
2014/11/17 Javascript
jQuery常用数据处理方法小结
2015/02/20 Javascript
BootStrap glyphicon图标无法显示的解决方法
2016/09/06 Javascript
JavaScript 继承详解(六)
2016/10/11 Javascript
seajs模块之间依赖的加载以及模块的执行
2016/10/21 Javascript
easyUI下拉列表点击事件使用方法
2017/05/18 Javascript
jquery实现一个全局计时器(商城可用)
2017/06/30 jQuery
详解VUE-地区选择器(V-Distpicker)组件使用心得
2018/05/07 Javascript
Python内置函数OCT详解
2016/11/09 Python
Python简单实现网页内容抓取功能示例
2018/06/07 Python
Python在图片中插入大量文字并且自动换行
2019/01/02 Python
用pycharm开发django项目示例代码
2019/06/13 Python
解决tensorflow打印tensor有省略号的问题
2020/02/04 Python
Django ModelForm操作及验证方式
2020/03/30 Python
matplotlib实现数据实时刷新的示例代码
2021/01/05 Python
CSS3 渐变(Gradients)之CSS3 线性渐变
2016/07/08 HTML / CSS
html5理解head_动力节点Java学院整理
2017/07/13 HTML / CSS
美国韩国化妆品和护肤品购物网站:Beautytap
2018/07/29 全球购物
荷兰街头时尚之家:Funkie House
2019/03/18 全球购物
法国低价在线宠物商店:bitiba.fr
2020/07/03 全球购物
WEB控件及HTML服务端控件能否调用客户端方法?如果能,请解释如何调用?
2015/08/25 面试题
安全员岗位职责
2013/11/11 职场文书
教师的实习自我鉴定
2013/12/17 职场文书
师范教师专业大学生职业生涯规划范文
2014/03/02 职场文书
教师求职信范文
2014/05/24 职场文书
开展创先争优活动总结
2014/08/28 职场文书
面试感谢信范文
2015/01/22 职场文书
生活小常识广播稿
2015/08/19 职场文书
管理者日常工作必备:22条企业管理流程模板!
2019/07/12 职场文书
python使用openpyxl库读写Excel表格的方法(增删改查操作)
2021/05/02 Python