Why is the output of corr2 of two matrixes not same dimension as the input matrix?

8 Ansichten (letzte 30 Tage)
Hi, I am currently implementing an algorithm of segmentation based on flow orientation, and one of the equations in the paper uses autocorrelation and crosscorrelation of image gradient.
The code runs fine, but when I try to reshape the final values into a matrix with same dimension as the input image, I receive error. The following is my code:
close all
x = imread(imfile);
x = im2double(x);
%%Gradient - orientation estimate
[Gx,Gy] = imgradientxy(x);
gxx = xcorr2(Gx(:,:));
gyy = xcorr2(Gy(:,:));
gxy = xcorr2(Gx(:,:), Gy(:,:));
angle = 0.5.*atan((2.*gxy)./(gxx - gyy)); % Equation 4
temp = (angle< 0);
angle(temp) = angle(temp) + 2.*pi; % angle within 0 to 2pi
angle = angle / pi *180;
Equation 4:
I spent some time looking at the length of each entry, and it turned out that all the values after processing by corr2 are double of the initial size; i.e.
input_image = 450 x 369
gx (after imgradientxy) = 450 x 450
gxx = 899 x 750
angle = 899 x 737
My questions:
1) How to make the matrix after autocorrelation & cross correlation same as the initial image dimension?
2) Why is there a difference in the matrix dimension after processing by imgradientxy ?
Any help would be greatly appreciated, thanks!

Akzeptierte Antwort

Rahul Goel
Rahul Goel am 20 Jul. 2015
Hi Alexander, I tried your code with a few images which ship with MATLAB, for example “pout.tif” image file. But I could not reproduce this issue. However, I observed the following:
1. Output of imgradientxy should not have different dimensions from that of input matrix.
As the documentation states,
[Gx,Gy] = imgradientxy(I)
returns the directional gradients, Gx and Gy, the same size as the input image I.
For image “pout.tif”,
close all
x = imread('pout.tif');
x = im2double(x);
[Gx,Gy] = imgradientxy(x);
>> whos x
Name Size Bytes Class Attributes
x 291x240 558720 double
>> whos Gx
Name Size Bytes Class Attributes
Gx 291x240 558720 double
2. Output of xcorr2(A) has different dimensions from that of the input matrix. If A is a matrix of size P X Q, "xcorr2(A)" will produce a matrix of size (P+P-1) X (Q+Q-1).
>> gxx = xcorr2(Gx(:,:));
>> gyy = xcorr2(Gy(:,:));
>> gxy = xcorr2(Gx(:,:), Gy(:,:));
>> whos gxx
Name Size Bytes Class Attributes
gxx 581x479 2226392 double
>> whos gxy
Name Size Bytes Class Attributes
gxy 581x479 2226392 double
>> whos gyy
Name Size Bytes Class Attributes
gyy 581x479 2226392 double
So, the change in dimensions after “xcorr2” is an expected behavior. You mentioned that you wanted the dimensions of output of “xcorr2” to be similar as that of the input matrix. However, as we have seen, it is not possible with “xcorr2” function. It would be more insightful if you could state the motivation behind that requirement. The user community will be able to assist you better with that information.
  1 Kommentar
Alexander Hum
Alexander Hum am 22 Jul. 2015
Thanks for your reply! The explanation is really helpful (since I don't have too much knowledge regarding autocorrelation).
I am trying to feed in the orientation values to trace boundary by using an average of flow direction, essentially the algorithm is here: http://home.cc.umanitoba.ca/~thomasg/MP/Research/DIP/samiee.htm
I tried downsampling, but it doesn't give me the same dimension either (with x-axis still significantly longer).
Currently I skipped the equation, and replaced by the Gdir values obtained by imgredient; but it would be great if I get some pointers (or can this equation by replaced by Gdir of imgredient?)
Thanks for your help!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by