opencv与PIL处理图像视频

小记opencv与PIL图像处理

  • opencv
    • 读取图片
    • 读取视频并保存
  • PIL

opencv

读取图片

调用cv2.imread()函数:

import cv2
path = r"1.jpg"
img = cv2.imread(path)
# cv2.namedWindow("image")  
# cv2.resizeWindow("image",480,600)
cv2.imshow("image",img) 
cv2.waitKey(0)

读出图片颜色通道为"BGR",同时cv2读出的数据格式为numpy数组的形式,即有 img.shape = (w,h,c),img.size = whc ,在神经网络处理图像时要注意

读取视频并保存

import cv2
path = r"1.mp4"
cap = cv2.VideoCapture(path)  #path可为视频路径,也可为摄像头的rstp流
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
# = cv.VideoWriter( filename, fourcc, fps, frameSize[, isColor] )
while cap.isOpened():
	_, frame = cap.read()
	if ret==True:
		frame = cv2.flip(frame,0)   #沿x轴翻转
        out.write(frame)			#保存帧
	cv2.imshow("capture",frame)
	if cv2.waitKey(1) & 0xFF == ord("q")
		break
cap.release()
cv2.destroyAllWindows()

PIL

读取图片

import PIL.Image as pimg
path = r"24.jpg"
img = pimg.open(path)
img.show()

与opencv读取图片不同,读出的img颜色通道为RGB,并不能img.shape;但有img.size = (w,h),可以通过numpy.array(img)达到与opencv相同的效果

你可能感兴趣的:(opencv与PIL处理图像视频)