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设计】。

相关文章

Django 拆分model和view的实现方法

Django 拆分model和view的实现方法

在标准的dgango项目中,自动生成的目录结构会包括models.py和views.py两个文件,分别在里面写model的代码和controller的代码,但是所有的代码都写在一个文件里...

Python自动化完成tb喵币任务的操作方法

Python自动化完成tb喵币任务的操作方法

2019双十一,tb推出了新的活动,商店喵币,看了一下每天都有几个任务来领取喵币,从而升级店铺赚钱,然而我既想赚红包又不想干苦力,遂使用python来进行手机自动化操作,目测全网首发!...

详解Pytorch 使用Pytorch拟合多项式(多项式回归)

详解Pytorch 使用Pytorch拟合多项式(多项式回归)

使用Pytorch来编写神经网络具有很多优势,比起Tensorflow,我认为Pytorch更加简单,结构更加清晰。 希望通过实战几个Pytorch的例子,让大家熟悉Pytorch的使用...

Python虚拟环境的原理及使用详解

Python的虚拟环境极大地方便了人们的生活。本指南先介绍虚拟环境的基础知识以及使用方法,然后再深入介绍虚拟环境背后的工作原理。 注意:本指南在macOS Mojave系统上使用最新版本...

Python中函数的参数定义和可变参数用法实例分析

本文实例讲述了Python中函数的参数定义和可变参数用法。分享给大家供大家参考。具体如下: 刚学用Python的时候,特别是看一些库的源码时,经常会看到func(*args, **kwa...