Python OpenCV 图片的读取、缩放,裁剪与保存

Python OpenCV 图片的读取、缩放,裁剪与保存

import numpy as np
import cv2 as cv

**

读取图片

**
original = cv.imread('p4.jpg')
print(original.shape)

cv.imshow('1',original)
blue = np.zeros_like(original)
blue[...,0] = original[...,0]
显示
cv.imshow('blue',blue)

green = np.zeros_like(original)
green[...,1] = original[...,1]
cv.imshow('green',green)

red = np.zeros_like(original)
red[...,2] = original[...,2]
cv.imshow('red',red)
#裁剪
h,w =original.shape[:2]
l,t= int(w/4),int (h/4)
r,b = int(w*3/4), int(h*3/4)
cropped = original[t:b,l:r]
cv.imshow('cropped',cropped)

#缩放,线性插值
scaled =cv.resize(original,(w*2,int(h/2)),interpolation=cv.INTER_LINEAR)
cv.imshow('scaled',scaled)

cv.waitKey()
#保存图片
cv.imwrite('blue.jpg',blue)
cv.imwrite('green.jpg',green)
cv.imwrite('red.jpg',red)
cv.imwrite('croped.jpg',cropped)
cv.imwrite('scaled.jpg',scaled)

你可能感兴趣的:(Python OpenCV 图片的读取、缩放,裁剪与保存)