来自OpenCV读取的JPEG图像内存字节大小似乎不正确 [英] JPEG image memory byte size from OpenCV imread doesn't seem right

查看:490
本文介绍了来自OpenCV读取的JPEG图像内存字节大小似乎不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用OpenCV,每当我将图像(来自相机流)保存为JPG到磁盘时.磁盘上的文件大小(以字节为单位)因预期的JPEG质量而异.

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.

例如

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")

推荐答案

是的,这是完全正常的.实际上,所有图像格式都是压缩m x n RGB矩阵数据的不同方法.就空间而言,JPG相对优于PNG.还要注意,这种相对行为是有代价的,即:JPG是有损压缩技术,而PNG是无损压缩技术.

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.

当我们从磁盘读取JPGPNG映像时,首先将其解压缩,然后填充矩阵数据.内存中的大小将始终为:m x n x 3字节,但是磁盘大小将根据所使用的压缩格式,所使用的压缩级别等而有所不同.对于不同的图像渐变,它也可能会有所不同.与具有多种颜色的图像(渐变图像)相比,具有单色的图像以JPG格式占用的空间要少得多.但是,两个图像的内存大小都相同:m x n x 3字节.

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.

这篇关于来自OpenCV读取的JPEG图像内存字节大小似乎不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆