原图
图片缩放
#缩放1
import cv2
img=cv2.imread('E:/python_cv/01.jpg',1)
img_info=img.shape
print(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 可选择插值方法
interpolation 选项 | 所用的插值方法 |
---|
INTER_NEAREST | 最近邻插值 |
INTER_LINEAR | 双线性插值(默认设置) |
INTER_AREA | 使用像素区域关系进行重采样。 它可能是图像抽取的首选方法,因为它会产生无云纹理的结果。 但是当图像缩放时,它类似于INTER_NEAREST方法。 |
INTER_CUBIC | 4x4像素邻域的双三次插值 |
INTER_LANCZOS4 | 8x8像素邻域的Lanczos插值 |
#缩放2
import cv2
import numpy as np
img=cv2.imread('E:/python_cv/01.jpg',1)
img_info=img.shape
print(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-255
for 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)
#缩放3
import cv2
import numpy as np
img=cv2.imread('E:/python_cv/01.jpg',1)
img_info=img.shape
print(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到300
import cv2
import numpy as np
img=cv2.imread('E:/python_cv/01.jpg',1)
img_info=img.shape
dst=img[100:200,100:300]
cv2.imshow('image_dst',dst)
cv2.waitKey(0)
![](https://i-blog.csdnimg.cn/blog_migrate/1569ef333eeca0e2dd62fc4d215657a2.png)
图像移位
#图像移位
import cv2
import numpy as np
img=cv2.imread('E:/python_cv/01.jpg',1)
img_info=img.shape
image_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 cv2
import numpy as np
img=cv2.imread('E:/python_cv/01.jpg',1)
img_info=img.shape
image_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 cv2
import numpy as np
img=cv2.imread('E:/python_cv/01.jpg',1)
img_info=img.shape
image_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 cv2
import numpy as np
img=cv2.imread('E:/python_cv/01.jpg',1)
img_info=img.shape
image_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)
![](https://i-blog.csdnimg.cn/blog_migrate/1a8505564752f7d51622b3032136d88f.png)
图像旋转
import cv2
import numpy as np
img=cv2.imread('E:/python_cv/01.jpg',1)
img_info=img.shape
image_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 3scale
dst=cv2.warpAffine(img,mat_rotate,(image_height,image_weight))
cv2.imshow('image_dst',dst)
cv2.waitKey(0)