当前位置: 移动技术网 > IT编程>脚本编程>Python > python 图像平移和旋转的实例

python 图像平移和旋转的实例

2019年03月21日  | 移动技术网IT编程  | 我要评论

倭黑猩猩最臭名昭著,福州癫痫病医院,老婆 只许我宠你

如下所示:

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 图像平移和旋转的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网