Why is the output of corr2 of two matrixes not same dimension as the input matrix?
5 次查看(过去 30 天)
显示 更早的评论
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/152306/image.png)
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!
0 个评论
采纳的回答
Rahul Goel
2015-7-20
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:
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.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!