代码含注释
import cv2
import os
import numpy as np
#PicName = raw_input("please input name of picture:")
path = os.path.join(os.getcwd(),'pic/0.jpg') #可以用下一行代码来
#path = os.path.join(os.getcwd(), PicName)#os.getwd 获取当前目录
print path
img = cv2.imread(path, 1) #读入存储的格式是 numpy,1表示彩色图片
cv2.imshow("1", img)
cv2.waitKey(0)
print img.shape
shape = img.shape
height = shape[0]
width = shape[1]
MatSrc = np.float32([[0, 0],[height-1, 0],[0, width-1]])#源文件的三个点(三点确定一个面)
# matDst = np.float32([[50,50], [300, height-200], [width-300,100]])
# MatSrc = np.float32([[0, 0], []])
matDst = np.float32([[height -1, width-1], [0, width-1], [height-1, 0]]) #要映射到哪三个点的位置
matAffine = cv2.getAffineTransform(MatSrc, matDst) #合成映射矩阵
dst = cv2.warpAffine(img, matAffine, (width, height))
print dst.shape
cv2.imshow("dd", dst)
cv2.waitKey(0)
旋转代码
import cv2
import os
import numpy as np
#PicName = raw_input("please input name of picture:")
path = os.path.join(os.getcwd(),'pic/0.jpg') #可以用下一行代码来
#path = os.path.join(os.getcwd(), PicName)#os.getwd 获取当前目录
print path
img = cv2.imread(path, 1) #读入存储的格式是 numpy,1表示彩色图片
cv2.imshow("1", img)
cv2.waitKey(0)
print img.shape
shape = img.shape
height = shape[0]
width = shape[1]
# MatSrc = np.float32([[0, 0],[height-1, 0],[0, width-1]])#源文件的三个点(三点确定一个面)
# matDst = np.float32([[50,50], [300, height-200], [width-300,100]])
# MatSrc = np.float32([[0, 0], []])
# matDst = np.float32([[height -1, width-1], [0, width-1], [height-1, 0]]) #要映射到哪三个点的位置
matAffine = cv2.getRotationMatrix2D((height * 0.5, width * 0.5), 45, 0.5) # 描述旋转的中心点,旋转的角度,缩放的系数
# matAffine = cv2.getAffineTransform(MatSrc, matDst) #合成映射矩阵
dst = cv2.warpAffine(img, matAffine, (height, width))#若大小和原来的一样,先width ,再height
print dst.shape
cv2.imshow("dd", dst)
cv2.waitKey(0)
缩放
import cv2
import os
import numpy as np
#PicName = raw_input("please input name of picture:")
path = os.path.join(os.getcwd(),'pic/0.jpg') #可以用下一行代码来
#path = os.path.join(os.getcwd(), PicName)#os.getwd 获取当前目录
print path
img = cv2.imread(path, 1) #读入存储的格式是 numpy,1表示彩色图片
cv2.imshow("1", img)
cv2.waitKey(0)
print img.shape
shape = img.shape
height = shape[0]
width = shape[1]
# # MatSrc = np.float32([[0, 0],[height-1, 0],[0, width-1]])#源文件的三个点(三点确定一个面)
# # matDst = np.float32([[50,50], [300, height-200], [width-300,100]])
# # MatSrc = np.float32([[0, 0], []])
# # matDst = np.float32([[height -1, width-1], [0, width-1], [height-1, 0]]) #要映射到哪三个点的位置
# matAffine = cv2.getRotationMatrix2D((height * 0.5, width * 0.5), 45, 0.5) # 描述旋转的中心点,旋转的角度,缩放的系数
# # matAffine = cv2.getAffineTransform(MatSrc, matDst) #合成映射矩阵
# dst = cv2.warpAffine(img, matAffine, (height, width))#若大小和原来的一样,先width ,再height
# print dst.shape
dst = cv2.resize(img,(int(height * 0.5),int( width * 0.5)))# 第二个参数要求必须是int ,表示缩放的大小
print dst.shape
cv2.imshow("dd", dst)
cv2.waitKey(0)#