From: Rise on
Hi, Steve,

Here is more information re. conv2 and sqrt:

>> 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
>>
From: Jan Simon on
Dear Rise,

> I am looking at my code, and see where the dimensions are changing. I have the following:
> [I, map] = imread('new-002.tif');
> 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
> >> size(imread('new-002.tif'))
> 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
From: Rise on
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}, map{k}] = imread(fileNames{k});
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
From: Jan Simon on
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
From: Rise on
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 ;-)~