MATLAB: Calculating the Bytes Per Pixel and BitRate of a JPEG Image

bitratecompressionImage Processing ToolboxMATLAB

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 (256×256 = 65536) I have no idea how to get 8 bits-per-pixel.
Any help is appreciated.
Thanks, Steve.

Best Answer

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