For calculating compressed image file size before & after compression, do I have to save images?Or calculating matrix bytes is enough and correct?

조회 수: 20 (최근 30일)
Hi friends! I want to calculate and compare image file size before and after the operation. As far as I know, image file size info can be get from imfinfo but I have image matrix only.
  • Do I have to save the image to get file size?
  • Does saving with different extensions make affect image file size (.jpg, .bmp, .png ...etc.) ?
  • How can I learn image file size without saving? ( Can I achieve this matrix calculations row*size*dimension or etc.)
Thank you in advance!
  댓글 수: 4

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2020년 6월 28일
I'm not sure the matrix size in MATLAB will be useful, though it might be interesting. You could use it to figure out how big the headers are for the uncompressed formats like BMP and TIF. What you want to do it use dir() or imfinfo() to get the size on disk, which is usually what people mean when they talk about compression. So just use create different filenames with different extensions representing the format you want to write the image out in, and then imwrite() to write it to disk. Then use dir() to get back the number of bytes on disk. For example
rgbImage = imread('peppers.png');
whos rgbImage
filename = 'image jpg.jpg';
imwrite(rgbImage, filename);
filename = 'image bmp.bmp';
imwrite(rgbImage, filename);
filename = 'image tif.tif';
imwrite(rgbImage, filename);
d = dir('image *.*')
for k = 1 : length(d)
fprintf('%s is %d bytes on disk.\n', d(k).name, d(k).bytes);
end
You'll see:
Name Size Bytes Class Attributes
rgbImage 384x512x3 589824 uint8
d =
3×1 struct array with fields:
name
folder
date
bytes
isdir
datenum
image bmp.bmp is 589878 bytes on disk.
image jpg.jpg is 23509 bytes on disk.
image tif.tif is 593776 bytes on disk.
You can see the header size for BMP is 589878 - 589824 = 54 bytes. Of course that doesn't work for compressed formats like PNG and JPG. By the way, don't save images in a lossy format like JPG if you plan on using them for image analysis.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by