1 获取图片info
2 创建空白模板
3 计算每一个位置的像素值
import cv2
import numpy as np
img =cv2.imread(r'C:\Users\Administrator\Desktop\222\333.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
mode=imgInfo[2]
print(imgInfo)
dstHeight=int(height/2)
dstWidth=int(width/2)
dstImage=np.zeros((dstHeight,dstWidth,3),np.uint8)
#3表明每一个像素有三个基本颜色组成 uint8:0-255
for i in range(0,dstHeight):
for j in range(0,dstWidth):
iNew = int(i*(height*1.0/dstHeight))
jNew = int(j*(height*1.0/dstHeight))
dstImage[i,j]=img[iNew,jNew]
cv2.imshow('img',img)
cv2.imshow('dstImage',dstImage)
cv2.waitKey(0)
(354, 500, 3)
缩放后:
cv2.resize(InputArray src, OutputArray dst, Size, fx, fy, interpolation)
参数解释:
InputArray src | 输入图片 |
OutputArray dst | 输出图片 |
Size | 输出图片尺寸 |
fx, fy | 沿x轴,y轴的缩放系数 |
interpolation | 插入方式 |
interpolation 选项所用的插值方法:
INTER_NEAREST |
最近邻插值 |
INTER_LINEAR |
双线性插值(默认设置) |
INTER_AREA |
使用像素区域关系进行重采样。 |
INTER_CUBIC |
4x4像素邻域的双三次插值 |
INTER_LANCZOS4 |
8x8像素邻域的Lanczos插值 |
注意:
1.输出尺寸格式为(宽,高)
2.默认的插值方法为:双线性插值
import cv2 as cv
# 读入原图片
img = cv.imread(r'C:\Users\Administrator\Desktop\222\333.jpg')
# 打印出图片尺寸
print(img.shape)
# 将图片高和宽分别赋值给x,y
x, y = img.shape[0:2]
# 显示原图
cv.imshow('OriginalPicture', img)
# 缩放到原来的3分之一,输出尺寸格式为(宽,高)
img_test1 = cv.resize(img, (int(y / 3), int(x / 3)))
cv.imshow('resize0', img_test1)
# cv.waitKey()
# 最近邻插值法缩放
# 缩放到原来的四分之一
img_test2 = cv.resize(img, (0, 0), fx=0.25, fy=0.25, interpolation=cv.INTER_NEAREST)
cv.imshow('resize1', img_test2)
cv.waitKey()
cv.destroyAllWindows()
(354, 500, 3)
import cv2
img =cv2.imread(r'C:\Users\Administrator\Desktop\222\333.jpg',1)
dstImage=img[100:200,100:300]
cv2.imshow('img',img)
cv2.imshow('dstImage',dstImage)
cv2.waitKey(0)
import cv2
import numpy as np
img =cv2.imread(r'C:\Users\Administrator\Desktop\222\333.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
matShift = np.float32([[1,0,100],[0,1,200]])#2*3
dstImage=cv2.warpAffine(img,matShift,(width,height))
#1 img 2 移位矩阵 3 图片info
#移位 和矩阵运算
cv2.imshow('img',img)
cv2.imshow('dstImage',dstImage)
cv2.waitKey(0)
cv2.warpAffine移位原理
[[1,0,100],[0,1,200]] 分成 2* 2 和 2*1 矩阵
A:[[1,0],[0,1]] 2*2
B:[[100],[200]] 2*1
C:[x,y]
A* C+B=[1* x+0* y,0* x+1* y]+[100,200]=[x+100,y+200]
以上原理可直接应用在图片缩放上
[[0.5,0,0],[0,0.5,0]] 这样就将原图缩小一半
import cv2
import numpy as np
img =cv2.imread(r'C:\Users\Administrator\Desktop\222\333.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
dst=np.zeros(imgInfo,np.uint8)
for i in range(0,height):
for j in range(0,width-100):
dst[i,j+100]=img[i,j]
cv2.imshow('img',img)
cv2.imshow('dst',dst)
cv2.waitKey(0)
import cv2
import numpy as np
img =cv2.imread(r'C:\Users\Administrator\Desktop\222\333.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
dst=np.zeros((height*2,width,3),np.uint8)
for i in range(0,height):
for j in range(0,width):
dst[i,j]=img[i,j]
dst[height*2-i-1,j]=img[i,j]
for i in range(0,width):
dst[height,i]=(255,0,0)
cv2.imshow('dst',dst)
cv2.waitKey(0)
import cv2
import numpy as np
img =cv2.imread(r'C:\Users\Administrator\Desktop\222\333.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
#src 3 ->dst 3 (左上角 左下角 右上角)
matSrc = np.float32([[0,0],[0,height-1],[width-1,0]])
#新的位置
matDst = np.float32([[20,20],[100,height-100],[width-100,100]])
#组合
matAffine = cv2.getAffineTransform(matSrc,matDst)
#第一个参数是 是原图三个角的位置 ,第二个参数是目标图三个角的位置
dst = cv2.warpAffine(img,matAffine,(width,height))
cv2.imshow('img',img)
cv2.imshow('matDst',dst)
cv2.waitKey(0)
import cv2
import numpy as np
img =cv2.imread(r'C:\Users\Administrator\Desktop\222\333.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
matRotate =cv2.getRotationMatrix2D((height*0.5,width*0.5),45,0.5)
#得到旋转矩阵 1 center旋转中心点 2 angle旋转角度 3 scale 缩放系数
dst=cv2.warpAffine(img,matRotate,(height,width))
cv2.imshow('dst',dst)
cv2.waitKey(0)