Calculating the Bytes Per Pixel and BitRate of a JPEG Image

32 次查看(过去 30 天)
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 个评论
KALYAN ACHARJYA
KALYAN ACHARJYA 2018-9-26
编辑:KALYAN ACHARJYA 2018-9-26
*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!

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2018-9-26
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 个评论
whatchamacallit
whatchamacallit 2018-9-26
I totally agree. The reason why the formula they have provided doesn't work is because the original uncompressed filesize is 45577 bytes and the number of pixels is 65536, which gives us 5.56 bits per pixel, and not the 8. Hence I have no idea what they are doing, so I'm going with the second option which is using the compression ratio to figure out bit rate. I have no idea if this correct or completely wrong, but since I'm doing this remotely I have no one to really ask on campus.
And you correct btw, this is only grayscale, which is for the first part of this course. The image in question is here: https://imgur.com/WsEAGHf
Thanks.
Image Analyst
Image Analyst 2018-9-26
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.

请先登录,再进行评论。

更多回答(2 个)

KALYAN ACHARJYA
KALYAN ACHARJYA 2018-9-26
编辑:KALYAN ACHARJYA 2018-9-26
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

Masoumeh Farhadi Nia
编辑:Masoumeh Farhadi Nia 2020-12-29
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?

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by