Calculating the Bytes Per Pixel and BitRate of a JPEG Image
44 views (last 30 days)
Show older comments
whatchamacallit
on 26 Sep 2018
Edited: Masoumeh Farhadi Nia
on 29 Dec 2020
Hello.
Graduated with an Electrical Engineering degree in 1999. Decided to purse a part-time MBA this year and was given an option to do an Engineering Elective with a partner University and completely getting my behind kicked as a lot has changed in the two decades since I graduated. Never had Matlab when I graduated and my focus back then was Electrical Motors, so this is all very very new to me. Everything I've learnt is from Google and the MatLab documentation (I travel a lot for work and therefore don't have much on-campus resources), so my apologies if I've even gotten the basics wrong.
For the first part of the assignment (four parts total), I have to "read" and image into Matlab, determine the size of the matrix, calculate the uncompressed image size, save it as a JPEG with varying quality factors, calculate compression ratios, etc. etc. for a total of 15 sub-tasks. I'm using the image file posted here: https://imgur.com/WsEAGHf
Does the code I have so far appear correct? I've tried to comment on what each function is attempting to accomplish.
%Reading image into a matrix called LENA in Matlab
LENA = imread('lena-256x256.jpg');
%Determining number of rows and columns of the matrix
[LENA_X, LENA_Y] = size(LENA);
%Calculating uncompressed image size assuming each pixel is stored in one byte
LENA_uncompressed_image_size = LENA_X * LENA_Y;
%Saving the image with different compression ratios of 95, 60, 30 and 5
imwrite(LENA, 'LENA_95.jpg', 'jpg', 'Quality', 95);
imwrite(LENA, 'LENA_60.jpg', 'jpg', 'Quality', 60);
imwrite(LENA, 'LENA_30.jpg', 'jpg', 'Quality', 30);
imwrite(LENA, 'LENA_5.jpg', 'jpg', 'Quality', 5);
Here is what is stumping me. I am supposed to calculate the bit-rate of each compressed file using the formula BitRate = (FileSize / PixelCount). I'm unable to figure out how to read the file size of each image into Matlab. Additionally, they have stated that the original uncompressed file has a bit-rate of 8. Using the formula they have provided, and just using the "Size" in Windows as the file size (45577 bytes) and the pixel count (256x256 = 65536) I have no idea how to get 8 bits-per-pixel.
Any help is appreciated.
Thanks, Steve.
1 Comment
KALYAN ACHARJYA
on 26 Sep 2018
Edited: KALYAN ACHARJYA
on 26 Sep 2018
*Graduated with an Electrical Engineerin degree in 1999. Decided to purse a part MatLab documentation (I travel a lot for work and ..............................gotten the basics wrong.
Please note, nowadays you can learn easily,. all resources are available in the fingertip, use it and explore the learning.
Good Wishes!
Accepted Answer
Image Analyst
on 26 Sep 2018
I wouldn't get hung up on the term "rate" - there is no time(s) involved here. I would just compute the compression ratio from the size in memory divided by the size on disk. Here's a start:
fileInfo = dir('*.jpg')
bytesOnDisk = [fileInfo.bytes]
% Let's list them all and also compute compression ratio.
for k = 1 : length(fileInfo)
thisName = fullfile(fileInfo(k).folder, fileInfo(k).name);
imageInfo = imfinfo(thisName);
imageSize = imageInfo.Width * imageInfo.Height * imageInfo.BitDepth/8;
fprintf('The size of %s in memory is %d bytes, the disk size is %d\n',...
fileInfo(k).name, imageSize, imageInfo.FileSize);
% Now compute compression ratio...
end
4 Comments
Image Analyst
on 26 Sep 2018
Yes, good to see that you're now using the file size from the directory into and not from the way you used size() originally like Guillaume said. It's a common beginner mistake, so much so that Steve Eddins wrote a blog on it: https://blogs.mathworks.com/steve/2011/03/22/too-much-information-about-the-size-function/
I also agree that bit rate is a bad term. Unless you know the time over which the image was transmitted, how can you know a rate? Maybe they mean bpp, bits per pixel, like if the original RGB image was 24 bits per pixel (8 bits for Red, 8 bits for green, and 8 bits for blue), and now it's 14.3 or whatever.
More Answers (2)
KALYAN ACHARJYA
on 26 Sep 2018
Edited: KALYAN ACHARJYA
on 26 Sep 2018
Use dir() and check the file size in return
And find rows and column of the respective write image, row*com gives the total pixel counts
0 Comments
Masoumeh Farhadi Nia
on 29 Dec 2020
Edited: Masoumeh Farhadi Nia
on 29 Dec 2020
Hello. I need to gain the bitrate for a bulk of video sequences comprssed utilizing different codecs and crfs. I just need to know the correct form of bitrate calculation utilizing videoinfo files in matlab. Is there an body can help?
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!