COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Why do Image Size Dimensions Change

• Follow

Hi, all,

Am a newbie to Matlab and image processing. Have been reading the book, Digital Image Processing by Gonzales et al. But I have a question....if someone could explain in more detail to me...

Why do the dimensions of the image change? Is it due to the conversion? e.g.,

I = imread('new-001.tif');    % dimensions for I: width: 1246, height: 912, uint8
I = im2double(rgb2gray(I)); % Now width = 1249, height = 914, double

I am inquiring, because I would like to map the grayscale image, I, to another color image, J, of the same dimensions of I -- when 'I' was originally read in.  But I would like to know why the dimensions of I changed in the first place. I am assuming it has something to do with the conversion.


 0

On 7/8/2010 11:42 AM, Rise wrote:
> Hi, all,
> Am a newbie to Matlab and image processing. Have been reading the book,
> Digital Image Processing by Gonzales et al. But I have a question....if
> someone could explain in more detail to me...
>
> Why do the dimensions of the image change? Is it due to the conversion?
> e.g.,
> I = imread('new-001.tif'); % dimensions for I: width: 1246, height: 912,
> uint8
> I = im2double(rgb2gray(I)); % Now width = 1249, height = 914, double
>
> I am inquiring, because I would like to map the grayscale image, I, to
> another color image, J, of the same dimensions of I -- when 'I' was
> originally read in. But I would like to know why the dimensions of I
> changed in the first place. I am assuming it has something to do with
> the conversion.
>
> Thanks in advance. Sincerely, Rise

Neither rgb2gray nor im2double should change the image size in this way.

Possible explanations:

* The image in the TIFF file is really 1249x914 instead of 1246x912.
What is the output for:

imfinfo('new-001.tif')

* You have modified versions of the functions im2double or rgb2gray on
your path.  What is the output for:

which -all im2double
which -all rgb2gray

---
Steve Eddins
http://blogs.mathworks.com/steve/


 0
Reply steve.eddins (783) 7/8/2010 5:50:26 PM

Hi, Steve,

Here is the information that I get. Thnx for your assistance. - Rise

>> imfinfo('new-002.tif')
ans =
Filename: 'new-002.tif'
FileModDate: '03-May-2010 14:05:46'
FileSize: 2898754
Format: 'tif'
FormatVersion: []
Width: 1247
Height: 912
BitDepth: 24
ColorType: 'truecolor'
FormatSignature: [73 73 42 0]
ByteOrder: 'little-endian'
NewSubFileType: 0
BitsPerSample: [8 8 8]
Compression: 'PackBits'
PhotometricInterpretation: 'RGB'
StripOffsets: [456x1 double]
SamplesPerPixel: 3
RowsPerStrip: 2
StripByteCounts: [456x1 double]
XResolution: 72
YResolution: 72
ResolutionUnit: 'Inch'
Colormap: []
PlanarConfiguration: 'Chunky'
TileWidth: []
TileLength: []
TileOffsets: []
TileByteCounts: []
Orientation: 1
FillOrder: 1
GrayResponseUnit: 0.0100
MaxSampleValue: [255 255 255]
MinSampleValue: 0
Thresholding: 1
Offset: 2894910
ans =
912        1247           3
>> which -all im2double
C:\Program Files\MATLAB\R2010a\toolbox\images\images\im2double.m
>> which -all rgb2gray
C:\Program Files\MATLAB\R2010a\toolbox\images\images\rgb2gray.m
>>

 0

Hi, Steve,

I am looking at my code, and see where the dimensions are changing. I have the following:

I = im2double(rgb2gray(I));
gx = conv2(I,h);                   % extract horizontal edges of I
gy = conv2(I,v);                   % extract vertical edges of I
gm = sqrt(gx.*gx + gy.*gy);  % magnitude

It is here that my image apparently changes from
ans =
912        1247           3

to this:
>> size(gm)
ans =
914        1249
>>

Hm. But I still don't understand why the dimensions are changing if I am using the gradient. Thnx in advance for your explanation. Sincerely, Rise

 0

Hi, Steve,

>> which -all sqrt
built-in (C:\Program Files\MATLAB\R2010a\toolbox\matlab\elfun\@double\sqrt)     % double method
built-in (C:\Program Files\MATLAB\R2010a\toolbox\matlab\elfun\@single\sqrt)     % single method
C:\Program Files\MATLAB\R2010a\toolbox\distcomp\parallel\@codistributed\sqrt.m  % codistributed method
>> which -all conv2
built-in (C:\Program Files\MATLAB\R2010a\toolbox\matlab\datafun\@single\conv2)  % single method
built-in (C:\Program Files\MATLAB\R2010a\toolbox\matlab\datafun\@double\conv2)  % double method
C:\Program Files\MATLAB\R2010a\toolbox\matlab\datafun\@uint8\conv2.m            % uint8 method
C:\Program Files\MATLAB\R2010a\toolbox\matlab\datafun\@uint16\conv2.m           % uint16 method
>>

 0

Hi, Steve,

>> which -all sqrt
built-in (C:\Program Files\MATLAB\R2010a\toolbox\matlab\elfun\@double\sqrt)     % double method
built-in (C:\Program Files\MATLAB\R2010a\toolbox\matlab\elfun\@single\sqrt)     % single method
C:\Program Files\MATLAB\R2010a\toolbox\distcomp\parallel\@codistributed\sqrt.m  % codistributed method
>> which -all conv2
built-in (C:\Program Files\MATLAB\R2010a\toolbox\matlab\datafun\@single\conv2)  % single method
built-in (C:\Program Files\MATLAB\R2010a\toolbox\matlab\datafun\@double\conv2)  % double method
C:\Program Files\MATLAB\R2010a\toolbox\matlab\datafun\@uint8\conv2.m            % uint8 method
C:\Program Files\MATLAB\R2010a\toolbox\matlab\datafun\@uint16\conv2.m           % uint16 method
>>

 0

Dear Rise,

> I am looking at my code, and see where the dimensions are changing. I have the following:
>     I = im2double(rgb2gray(I));
>     gx = conv2(I,h);                   % extract horizontal edges of I
>     gy = conv2(I,v);                   % extract vertical edges of I
>     gm = sqrt(gx.*gx + gy.*gy);  % magnitude
>
> It is here that my image apparently changes from
> ans = 912        1247           3
> to this:
> >> size(gm)
> ans =  914        1249
> Hm. But I still don't understand why the dimensions are changing if I am using the gradient.

Wow, Rise, this differs in some very important details from your first posting. It is not helpful for the people, who want to help, to omit these information.

You do not post the definition of "h" and "v". Anyhow, what do you expect CONV2 to do? Read the doc of CONV2 to get the answer, why this changes the size.

Kind regards, Jan

 0

Hi, Jan,

So sorry for the omittance and confusion. I didn't know if I should have posted my entire code as is and wanted to keep it simple in presenting my question, but for future posts on this forum, I will indeed post it as is.

My code is using a for loop to read in a sequence of images and then computes the gradient of each image:

thepath = '\\files.med.harvard.edu\home\MATLAB\002\results-0.45-gamma-monkey\';
filelist = dir(fullfile(thepath,'new-*.tif'));
fileNames = {filelist.name}'
nImages = length(fileNames);

% Sobel horizontal and vertical masks
h = fspecial('sobel');
v = h';

% Read in each image, convert to grayscale and double, and find the
% magnitude, magnitude direction and edge direction. We get a vector
% per pixel, with its direction perpendicular to an edge and its
% magnitude proportional to the contrast
for (k = 1:nImages)
I{k} = im2double(rgb2gray(I{k})); %range is [0 1]
gx = conv2(I{k},h); % extract horizontal edges of I
gy = conv2(I{k},v); % extract vertical edges of I
gm = sqrt(gx.*gx + gy.*gy); % combine both horizontal and vertical edges
theta = atan2(gy,gx); %magnitude direction, angles in [-PI, PI] and quadrant accurate
thetaedge = atan2(-gx,gy); %angle/direction of the magnitude

% more code...
end

Yes, I believe that the issue may be when I use conv2 because when read in the size of each image goes from <912x1247> to <914x1249> after I use conv2.

And yes, from what I've read in the Help section on conv2(A,B), "The size of C in each dimension is equal to the sum of the corresponding dimensions of the input matrices, minus one. That is, if the size of A is [ma,na] and the size of B is [mb,nb], then the size of C is [ma+mb-1,na+nb-1]."

Ah, ok, so now I know where the numbers come from so that my image goes from <912x1247> to <914x1249>. I don't completely understand why it is doing this on a deeper level.

Hm. I do see that I can use the following so that my grayscale is the same size as my original read in image. For example, instead of using:

conv2(I{k}, h);

I think I need to use the one below as "same - Returns the central part of the convolution of the same size as A."

conv2(I{k}, h, same);

Yes? Thank you very much in advance. Sincerely, Rise

 0

Dear Rise,

> I think I need to use the one below as "same - Returns the central part of the convolution of the same size as A."
> conv2(I{k}, h, same);

Include the quotes...
conv2(I{k}, h, 'same')

Good luck, Jan

 0

Hi, Jan,

Yes, sigh, I forgot the single quotes. Thanks for being patient with my learning curve and thanks for the luck, as I definitely need it!

Cheers, - Rise ;-)~

 0

9 Replies
175 Views

Similiar Articles:

7/24/2012 9:13:36 PM