MATLAB Answers

# Can anybody plz explain this code,specifically the 5th line..

조회 수: 1(최근 30일)
Sumeet Badgujar 26 Oct 2018
댓글: Walter Roberson 27 Oct 2018
abc=input('Enter image name or path');
a=imread(abc);
imshow(a);
b=size(a);
c=b(1)*b(2)/1024;
disp(fprintf('Name of image: %s',abc))
disp(fprintf('Size of the image is %f kB',c))

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

### 답변(3개)

OCDER 26 Oct 2018
An image is often represented as a MxNx3 matrix, where each element of a matrix is a uint8 value ranging from 0 to 255 (this is 1 byte of data. 1 byte = 8 bits = value from 0 to 255).
Thus, each pixel of an image is of size 1x1x3 matrix for the Red, Green, and Blue values. With that said, that code line 5 is incorrect if dealing with color images, as each pixel actually requires 3 bytes of data (Ex. White = [255, 255, 255] = 3 bytes)
image size should be:
c = prod(b) /1024 ; %for kB of data
If you have the liberty of editing the code, change the variable names to be more descriptive.
ImageSizeKB = prod(b) / 1024; %prod(b) will do b(1)*b(2)*b(3) = MxNx3 = total bytes in image
Another way to get image size is by using iminfo FileSize
ImageInfo = iminfo(abc);
ImageSizeKB = ImageInfo.FileSize / 1024;

#### 댓글 수: 0

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

madhan ravi 26 Oct 2018
a small example to illustrate
a = rand(4,3)
b = size(a)
b(1)
b(2)

#### 댓글 수: 3

madhan ravi 26 Oct 2018
size() returns dimensions of a matrix as row number and column number, the output is b here so b(1) is the a’s row number and b(2) is the columns number. So the fifth lines simple explanation is a’s Row number and it’s column number is multiplied and divided by 1024.
madhan ravi 26 Oct 2018
imread() reads an image and outputs a matrix form of an image.
madhan ravi 26 Oct 2018
imshow() Displays an image by reading the matrix

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

Image Analyst 26 Oct 2018
This is BAD code and has several errors. What line 5 does is to try to compute the number of pixels and then kilobytes. b(1) is the number of rows. b(2) is the number of columns multiplied by the number of color channels. Then if you multiply them together you get the number of pixels, and if you assume they're 1 byte pixels and divide by 1024 you get kilobytes. Here is some less bad code:
fileName = input('Enter image name or path : ', 's')
theImage = imread(fileName);
imshow(theImage);
axis('on', 'image');
sizeDimensions = size(theImage);
sizeInBytes = sizeDimensions(1)*sizeDimensions(2)/1024
fileInfo = dir(fileName)
fprintf('Name of image: %s\n', fileName)
fprintf('Size of the image is %d bytes, or %.1f kB\n', fileInfo.bytes, fileInfo.bytes / 1024)
but it's still not that robust or user friendly. Please review these links:
I would not look to that programmer as a paragon of MATLAB code writing ability.

#### 댓글 수: 3

Sumeet Badgujar 27 Oct 2018
But it does not give the perfect size... the image used is 11.2 kb,
the imread output gives [264,191,3]..calculated according this gives wrong file size
Walter Roberson 27 Oct 2018
b(2) is the number of columns multiplied by the number of color channels.
Not in this case. Single output for size was used, so b will be as long as needed to represent the dimensions individually.
Walter Roberson 27 Oct 2018
Image file sizes on disk represent sizes including any headers and comments (such as EXIF information), and take into account any compression (which might be lossy) that has been used. The size of the disk file can end up being larger than the amount of memory required to store the image in memory, but the file size could also end up being much less than is required to store the image in main memory if the compression is strong.

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

### Community Treasure Hunt

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

Start Hunting!

Translated by