python 图像平移和旋转的实例

yipeiwu_com5年前Python基础

如下所示:

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快乐学数学Github万星神器Manim简介

使用Python快乐学数学Github万星神器Manim简介

高考在即,笔者想为孩子以后能够快乐学习数学、学习编程找到一个比较合适的项目,经过一番比较发现github上的万星项目manim(https://github.com/3b1b/manim...

python树莓派红外反射传感器

python树莓派红外反射传感器

本文实例为大家分享了python树莓派红外反射传感器的程序,供大家参考,具体内容如下 1、工具 rpi3,微雪ARPI600,Infrared Reflective Sensor 2、基...

python ftplib模块使用代码实例

这篇文章主要介绍了python ftplib模块使用代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Python中默认安装的f...

Python的Django框架中URLconf相关的一些技巧整理

提供视图配置选项 如果你发布一个Django的应用,你的用户可能会希望配置上能有些自由度。 这种情况下,为你认为用户可能希望改变的配置选项添加一些钩子到你的视图中会是一个很好的主意。 你...

Python字符串格式化%s%d%f详解

关于讨论输出格式化的问题,小编不是一时兴起,之前学习python的时候就经常遇到输出时“%d”,一直没有仔细学习,今天又看到了,下面分享一个简单实例,python输出99乘法表: #...