添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

print(cv2. version )

Open the webcam

cap = cv2.VideoCapture(0) # Change the index if you have multiple cameras

while True:
# Capture a frame from the webcam
ret, frame = cap.read()

# Encode the frame as jpeg, tiff, webp image
# Set the quality (optional)
encode_param_jpeg = [int(cv2.IMWRITE_JPEG_QUALITY), 50]
result_jpeg, encimg_jpeg = cv2.imencode('.jpg', frame, encode_param_jpeg)
#tiff
encode_param_tiff = [int(cv2.IMWRITE_TIFF_COMPRESSION), 50]
result_tiff, encimg_tiff = cv2.imencode('.tif', frame)
#webp
encode_param_webp = [int(cv2.IMWRITE_WEBP_QUALITY), 50]
result_webp, encimg_webp = cv2.imencode('.webp', frame, encode_param_webp)
print("frame size: ", frame.shape)
# #avif
# encode_param_avif = [int(cv2.IMWRITE_AVIF_QUALITY), 50]
# result_avif, encimg_avif = cv2.imencode('.avif', frame, encode_param_avif)
#comparethe size of the frame and encoded image
print("jpeg size: ", len(encimg_jpeg))
print("tiff size: ", len(encimg_tiff))
print("webp size: ", len(encimg_webp), "\n")
# decode the jpeg encoded image
decimg_jpeg = cv2.imdecode(encimg_jpeg, 1)
decimg_tiff = cv2.imdecode(encimg_tiff, 1)
decimg_webp = cv2.imdecode(encimg_webp, 1)
# merge the images   side by side
merged_image = np.hstack((decimg_jpeg, decimg_tiff, decimg_webp))
# Display the frame
cv2.imshow('frame', merged_image)
cv2.waitKey(1)  # Adjust as needed to display the frame

I am trying to reduce the amount of data in video transmission.

If you try to compress the image with the AVIF codec by activating lines 29-30 in the above code, the following error occurs.

result_avif, encimg_avif = cv2.imencode('.avif', frame, encode_param_avif)

cv2.error: OpenCV(4.8.0) /io/opencv/modules/imgcodecs/src/loadsave.cpp:1120: error: (-2:Unspecified error) could not find encoder for the specified extension in function ‘imencode’

I am using opencv-4.8.0 in a python3 environment.
I’m asking because I think there is a variable for the avif codec in the documentation.
https://docs.opencv.org/4.8.0/d8/d6a/group__imgcodecs__flags.html

  • Support AVIF for image reading and writing · Issue #19271 · opencv/opencv · GitHub
  • https://github.com/opencv/opencv/pull/23596
  • AVIF support is extremely new. you’ll probably need libavif. I don’t know if the cmake stuff fetches that or requires you to provide it.

    also looks like you’ll have to use VideoWriter, since that format is based on AV1, a video codec.