【python】opencv教程CV2模块——图片处理,裁剪缩放加边框
opencv教程CV2模块——图片处理,裁剪缩放加边框
import cv2
# 读取图片
img = cv2.imread('./imgg/1.jpeg')
# 缩放成200x200的方形图像
img_200x200 = cv2.resize(img, (200, 200))
# 不直接指定缩放后大小,通过fx和fy指定缩放比例,0.5则长宽都为原来一半
# 等效于img_200x300 = cv2.resize(img, (300, 200)),注意指定大小的格式是(宽度,高度)
# 插值方法默认是cv2.INTER_LINEAR,这里指定为最近邻插值
img_200x300 = cv2.resize(img, (0, 0), fx=0.5, fy=0.5,
interpolation=cv2.INTER_NEAREST)
# 在上张图片的基础上,上下各贴50像素的黑边,生成300x300的图像
img_300x300 = cv2.copyMakeBorder(img, 50, 50, 0, 0,
cv2.BORDER_CONSTANT,
value=(0, 0, 0))
# 对照片中树的部分进行剪裁
patch_tree = img[20:150, -180:-50]
cv2.imwrite('./out/cropped_tree.jpg', patch_tree)
cv2.imwrite('./out/resized_200x200.jpg', img_200x200)
cv2.imwrite('./out/resized_200x300.jpg', img_200x300)
cv2.imwrite('./out/bordered_300x300.jpg', img_300x300)