JPEG image memory byte size from OpenCV imread doesn't seem right

一世执手 提交于 2021-02-11 12:28:43

问题


Using OpenCV, Whenever I save an image (from a camera stream) as JPG to the disk. The file size in bytes on the disk differs depending on the JPEG quality as expected.

However, whenever I read the images regardless of the file size on the disk, the memory size remains constant. Is this normal?

Also, there seems to be a massive difference in the disk and memory size of the image. I was expecting a much smaller memory size, somewhat relative to the disk size.

For e.g.

import sys
import cv2

cv2.imwrite('temp.jpg', frame, [int(cv2.IMWRITE_JPEG_QUALITY), 10])
# disk size of temp.jpg is 24kb

image = cv2.imread('temp.jpg')
# memory size is 2.7 mb

cv2.imwrite('temp.jpg', frame, [int(cv2.IMWRITE_JPEG_QUALITY), 90])
# disk size of temp.jpg is 150kb

image = cv2.imread('temp.jpg')
# memory size is still constant at 2.7 mb

This is how I compute the memory size:

print("Image byte size :", round(sys.getsizeof(image) / (1024 * 1024), 2), "mb")

回答1:


Yes, it is completely normal. All the image formats are actually different ways of compressing a m x n RGB matrix data. JPG is relatively better than PNG in terms of space. Also note that this relative behaviour comes at a cost, which is: JPG is lossy compression technique but PNG is a lossless compression technique.

When we read a JPG or PNG image from disk, it is first uncompressed and then the matrix data is populated. The in memory size would always be: m x n x 3 bytes, but the disk size would be different depending upon the compression format used, compression level used, etc. It can also vary for different image gradients. An image with a single color would take much less space in JPG format than an image with lot of colors (gradient images). But the memory size of both the images would be same: m x n x 3 bytes.



来源:https://stackoverflow.com/questions/53607289/jpeg-image-memory-byte-size-from-opencv-imread-doesnt-seem-right

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!