dkin
dkin

Reputation: 76

image size using imread and size do not match - matlab

Why when I read an image im = imread('pears.png') I see the in workspace 486x732x3 unit 8 but when I run [height, width] = size(im) I get height 486 and width 2196?. When I read m83.tif all is ok. is it related to color? which type of file will be multiply by 3 than?

Upvotes: 0

Views: 238

Answers (1)

Nemesis
Nemesis

Reputation: 2334

Following the documentation of imread:

A = imread(FILENAME,FMT) ...

If the file contains a grayscale image, A is an M-by-N array
If the file contains a truecolor image, A is an M-by-N-by-3 array.

Thus, what you want to have are only the first dimensions. However, since im is three dimensional and size will return the size of a reshaped array when only querying two dimensions. You will see that your second dimeions is the product size(im,2)*size(im,3).

Just use

[height, width, ~] = size(im)

This will query three dimensions but drop the third output parameter. Also using:

height = size(im,1);
width = size(im,2);

will work and is a save solution.

See following code for more clarifictaion:

>> im = ones([40,20,3]);
>> [h,w] = size(im)

h =

    40


w =

    60

>> [h,w,~] = size(im)

h =

    40


w =

    20

>> h = size(im,1)

h =

    40

>> w = size(im,2)

w =

    20

Upvotes: 1

Related Questions