python opencv实现切变换 不裁减图片

yipeiwu_com6年前Python基础

本文实例为大家分享了python opencv实现切变换的具体代码,供大家参考,具体内容如下

# -*- coding:gb2312 -*-
import cv2
from math import *
import numpy as np

img = cv2.imread("3-2.jpg")

height,width=img.shape[:2]

degreeAffine=45

#切变换最后结果
heightAffine=height
widthAffine=int(2*height*tan(radians(degreeAffine))+width)

# 随意两点得到仿射变换矩阵
# point1=np.float32([[50,50],[200,50],[50,200]])
# point2=np.float32([[50,50],[100,50],[50,200]])
# matAffine=cv2.getAffineTransform(point1,point2)


#如果结果是正的图片
tempY=(widthAffine+1)/2-width/2
tempX=(heightAffine+1)/2-height/2
temp=np.zeros((heightAffine,widthAffine,3),np.uint8)+255
temp[tempX:(tempX+height),tempY:(tempY+width)]=img
cv2.imshow("temp",temp)


#仿射变换矩阵
matAffine=np.zeros([2,3])
matAffine[0,0]=1
matAffine[0,1]=tan(radians(degreeAffine))
matAffine[0,2]=(widthAffine-width)/2
matAffine[1,0]=0
matAffine[1,1]=1
matAffine[1,2]=(heightAffine-height)/2


imgAfiine=cv2.warpAffine(img,matAffine,(widthAffine,heightAffine),borderValue=(255,255,255))

cv2.imwrite("temp.jpg",temp)
cv2.imwrite("imgAffine.jpg",imgAfiine)
cv2.imshow("imgAffine",imgAfiine)

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python 设置输出图像的像素大小方法

如下所示: plt.rcParams['savefig.dpi'] = 300 #图片像素 plt.rcParams['figure.dpi'] = 300 #分辨率 为了记住不...

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

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

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

Python使用MD5加密算法对字符串进行加密操作示例

本文实例讲述了Python使用MD5加密算法对字符串进行加密操作。分享给大家供大家参考,具体如下: # encoding: utf-8 from __future__ import...

Python 运行 shell 获取输出结果的实例

首先使用内置模块os. >>> import os >>> code = os.system("pwd && sleep 2") # /User/...

详解如何设置Python环境变量?

详解如何设置Python环境变量?

家好,我是Yivies!相信大家多多少少遇到过这样的情况吧?就是在安装了python之后想完整在命令提示符直接输入python就可以使用的操作,但是会出现输入了python之后找不到命令...