欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python 图像平移和旋转的实例

程序员文章站 2023-12-06 15:00:22
如下所示: import cv2 import math import numpy as np def move(img): height, width...

如下所示:

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