opencv图像处理(Image Processing)
图像变化(image transformations)
1. 移动(translation)
M = np.float32([[1,0,25],[0,1,50]])
shifted = cv2.warpAffine(image,M,(image.shape[1],image.shape[0]))
cv2.imshow("shifted down and right",shifted)
首先定义移动矩阵 M,表示移动(left,right,up,down) 多少像素
矩阵M是浮点型的数组
[1, 0, x]:x表示左右移动多少像素,
x值为正——像右移动,x值为负——向左移动
[0, 1, y]:y表示上下移动多少像素
y值为正——向下移动,y值为负——向上移动
利用 cv2.warpAffine() 进行变换
完整代码
import numpy as np
import argparse
import cv2
ap = argparse.ArgumentParser()
ap.add_argument("-i","--image",required = True,
help = "path to the image")
args = vars(ap.parse_args())
image = cv2.imread(args["image"])
cv2.imshow("original",image)
M = np.float32([[1,0,25],[0,1,50]])
shifted = cv2.warpAffine(image,M,(image.shape[1],image.shape[0]))
cv2.imshow("shifted down and right",shifted)
cv2.waitKey(0)
运行
python3 translation.py -i /路径/xxx.jpg
2. 旋转(rotation)
(h,w) = image.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center,45,1.0)
rotated = cv2.warpAffine(image,M,(w,h))
cv2.imshow("rotated by 45 degree",rotated)
首先确定中心位置center
定义矩阵M:利用cv2.getRotationMatrix2D而不是利用numpy,
cv2.getRotationMatrix2()中的参数分别为旋转的固定点、旋转角度、1.0代表大小不变(当然你也可以改变)
接下来继续利用cv2.warpAffine()进行变换
完整代码
import numpy as np
import argparse
import cv2
ap = argparse.ArgumentParser()
ap.add_argument("-i","--image",required = True,
help = "Path to the image")
args = vars(ap.parse_args())
image = cv2.imread(args["image"])
cv2.imshow("original",image)
(h,w) = image.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center,45,1.0)
rotated = cv2.warpAffine(image,M,(w,h))
cv2.imshow("rotated by 45 degree",rotated)
cv2.waitKey(0)
3. 改变大小(resizing)
r = 150.0 / image.shape[1]
dim = (150,int(image.shape[0]*r))
resized = cv2.resize(image,dim,interpolation = cv2.INTER_AREA)
cv2.imshow("REsized(Witdth)",resized)
假设定义新的图像的宽度(width)150像素,利用比例关系确定高度(height)
利用cv2.resize()变换,最后一个参数为变换的算法(当然还有其他的可以利用,比如cv2.INTER_LINEAR,cv2.INTER_CUBIC, cv2.INTER_NEAREST.)
r = 150.0 / image.shape[0]
dim = (int(image.shape[1]*r),50)
resized = cv2.resize(image,dim,interpolation = cv2.INTER_AREA)
cv2.imshow("REsized(Witdth)",resized)
定义高度进行变换
完整代码
import numpy as np
import cv2
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("-i","--image",required = True,
help = "Path to the image")
args = vars(ap.parse_args())
image = cv2.imread(args["image"])
cv2.imshow("Original",image)
r = 150.0 / image.shape[1]
dim = (150,int(image.shape[0]*r))
resized = cv2.resize(image,dim,interpolation = cv2.INTER_AREA)
cv2.imshow("REsized(Witdth)",resized)
r = 150.0 / image.shape[0]
dim = (int(image.shape[1]*r),50)
resized = cv2.resize(image,dim,interpolation = cv2.INTER_AREA)
cv2.imshow("REsized(Witdth)",resized)
cv2.waitKey(0)
4.翻转(flipping)
image = cv2.imread(args["image"])
cv2.imshow("Original",image)
flipped = cv2.flip(image,1)
cv2.imshow("flipped horizontally",flipped)
flipped = cv2.flip(image,0)
cv2.imshow("flipped vertiacally",flipped)
flipped = cv2.flip(image,-1)
cv2.imshow("flipped horizontally & vertiacally",flipped)
利用cv2.flip()即可
5. 剪裁(cropping)
cropped = image[30:120 ,240:335]
cv2.imshow("crop",cropped)
前一个参数为高度,后一个为宽度
完整代码
import numpy as np
import argparse
import cv2
ap = argparse.ArgumentParser()
ap.add_argument("-i","--image",required = True,
help = "Path to the image")
args = vars(ap.parse_args())
image = cv2.imread(args["image"])
cv2.imshow("Original",image)
cropped = image[30:120 ,240:335]
cv2.imshow("crop",cropped)
cv2.waitKey(0)