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

2 次查看(过去 30 天)
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
OCDER 2018-10-26
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;

madhan ravi
madhan ravi 2018-10-26
a small example to illustrate
a = rand(4,3)
b = size(a)
b(1)
b(2)
  3 个评论

请先登录,再进行评论。


Image Analyst
Image Analyst 2018-10-26
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 个评论
Walter Roberson
Walter Roberson 2018-10-27
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
Walter Roberson 2018-10-27
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Segmentation and Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by