Python+Opencv3 图像基本操作

对最近学习的openCV相关的图像操作进行整理,尤其是对图像的读取、视频的读写等,详细如下:

1. 图像的读取显示

import cv2
img = cv2.imread('/home/zxl/Downloads/timg.jpeg')
cv2.namedWindow('Image')
cv2.imshow('Image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

2. 摄像头视频实时显示并保存

import cv2

cap = cv2.VideoCapture(1)

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('/home/zxl/PycharmProjects/learn/output.avi',fourcc, 20.0, (1280,720))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
	#此处读出的图像是反的,需要将图像镜像
        frame = cv2.flip(frame,0)

        # write the flipped frame
        out.write(frame)

        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

猜你喜欢

转载自www.cnblogs.com/qq377801394/p/10339280.html