700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python+opencv+图像几何变换(图片缩放 剪切 位移 镜像 放射变换 旋转)

python+opencv+图像几何变换(图片缩放 剪切 位移 镜像 放射变换 旋转)

时间:2019-09-09 09:28:11

相关推荐

python+opencv+图像几何变换(图片缩放 剪切 位移 镜像 放射变换 旋转)

原图

图片缩放

API

#缩放1import cv2img=cv2.imread('E:/python_cv/01.jpg',1)img_info=img.shapeprint(img_info)image_height=img_info[0]image_weight=img_info[1]image_mode=img_info[2]dstHeight=int(0.5*image_height)dstWeight=int(0.5*image_weight)dst=cv2.resize(img,(dstHeight,dstWeight))cv2.imshow('image_dst',dst)cv2.waitKey(0)

cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) → dst 可选择插值方法

自己实现

#缩放2import cv2import numpy as npimg=cv2.imread('E:/python_cv/01.jpg',1)img_info=img.shapeprint(img_info)image_height=img_info[0]image_weight=img_info[1]image_mode=img_info[2]dstHeight=int(0.5*image_height)dstWeight=int(0.5*image_weight)dst=np.zeros((dstHeight,dstWeight,3),np.uint8) #0-255for i in range(dstHeight):for j in range(dstWeight):img_i=int(i*(image_height)/dstHeight)img_j=int(j*(image_weight)/dstWeight)dst[i,j]=img[img_i,img_j]cv2.imshow('image_dst',dst)cv2.waitKey(0)

缩放矩阵

#缩放3import cv2import numpy as npimg=cv2.imread('E:/python_cv/01.jpg',1)img_info=img.shapeprint(img_info)image_height=img_info[0]image_weight=img_info[1]image_mode=img_info[2]mat_scale=np.float32([[0.5,0,0],[0,0.5,0]])dst=cv2.warpAffine(img,mat_scale,(int(image_height/2),int(image_weight/2)))cv2.imshow('image_dst',dst)cv2.waitKey(0)

图片剪切

#图像剪切(x-->100到200,y-->100到300import cv2import numpy as npimg=cv2.imread('E:/python_cv/01.jpg',1)img_info=img.shapedst=img[100:200,100:300]cv2.imshow('image_dst',dst)cv2.waitKey(0)

图像移位

#图像移位import cv2import numpy as npimg=cv2.imread('E:/python_cv/01.jpg',1)img_info=img.shapeimage_height=img_info[0]image_weight=img_info[1]cv2.imshow('image_src',img)#移位矩阵mat_shift=np.float32([[1,0,100],[0,1,200]])dst=cv2.warpAffine(img,mat_shift,(image_height,image_weight))cv2.imshow('image_dst',dst)cv2.waitKey(0)

#图像移位import cv2import numpy as npimg=cv2.imread('E:/python_cv/01.jpg',1)img_info=img.shapeimage_height=img_info[0]image_weight=img_info[1]cv2.imshow('image_src',img)dst=np.zeros(img.shape,np.uint8)for i in range(image_height-200):for j in range(image_weight-100):dst[i+200,j+100]=img[i,j]cv2.imshow('image_dst',dst)cv2.waitKey(0)

图像镜像

#图像镜像import cv2import numpy as npimg=cv2.imread('E:/python_cv/01.jpg',1)img_info=img.shapeimage_height=img_info[0]image_weight=img_info[1]cv2.imshow('image_src',img)dst=np.zeros(img.shape,np.uint8)for i in range(image_height):for j in range(image_weight):dst[i,j]=img[image_height-i-1,j]cv2.imshow('image_dst',dst)cv2.waitKey(0)

图像仿射变换

#仿射变换:位移 旋转 缩放import cv2import numpy as npimg=cv2.imread('E:/python_cv/01.jpg',1)img_info=img.shapeimage_height=img_info[0]image_weight=img_info[1]#src3-->dst3(左上角,左下角,右上角)mat_src=np.float32([[0,0],[0,image_height-1],[image_weight-1,0]])mat_dst=np.float32([[50,50],[300,image_height-200],[image_weight-300,100]])mat_Affine=cv2.getAffineTransform(mat_src,mat_dst)dst=cv2.warpAffine(img,mat_Affine,(image_height,image_weight))cv2.imshow('image_dst',dst)cv2.waitKey(0)

图像旋转

import cv2import numpy as npimg=cv2.imread('E:/python_cv/01.jpg',1)img_info=img.shapeimage_height=img_info[0]image_weight=img_info[1]mat_rotate=cv2.getRotationMatrix2D((image_height*0.5,image_weight*0.5),45,1) #center angle 3scaledst=cv2.warpAffine(img,mat_rotate,(image_height,image_weight))cv2.imshow('image_dst',dst)cv2.waitKey(0)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。