Matrix dimensions must agree

9 visualizaciones (últimos 30 días)
Victoria Gruen
Victoria Gruen el 9 de Ag. de 2021
Comentada: Victoria Gruen el 16 de Ag. de 2021
Hello, trying to create a filter size dependent on pixel intensities. The error is in line [Temp2 = I2(i:i+M2,j:j+N2).*Kernel2;] and it says the matrix dimensions must agree. I am unsure which parts must agree. I2 is an image converted to double and M2 and N2 are
M2 = size(x,1)-1;
N2 = size(y,1)-1;
% Convolution
% sz2= pixel intensity+5/5
%if pixel intensity is 1, sigma is 1, if 255, sigma is 50
for i = 1:size(I2,1)-M2
for j =1:size(I2,2)-N2
sz2 = (((I2(i,j)+5)/10));
sigma2 = sz2/2.2;
[x,y]=meshgrid(-sz2:sz2,-sz2:sz2);
Exp_comp2 = -(x.^2+y.^2)/(2*sigma2*sigma2);
Kernel2= exp(Exp_comp2)/(2*pi*sigma2*sigma2);
Temp2 = I2(i:i+M2,j:j+N2).*Kernel2;
Output2(i,j)=sum(Temp2(:));
end
end

Respuestas (1)

Walter Roberson
Walter Roberson el 9 de Ag. de 2021
M2 and N2 are fixed, so the size selected from I2 is fixed.
The size of Kernel2 depends upon the data values in I2.
Question: when you vary the size of the kernel, do you want the kernel anchored in the upper left corner of the array from i, j? Or do you want the kernel centered around i, j?
  3 comentarios
Walter Roberson
Walter Roberson el 10 de Ag. de 2021
for i = 1:size(I2,1) %do not subtract M2
for j =1:size(I2,2) %do not subtract N2
sz2 = (((I2(i,j)+5)/10));
"Ideally" you now want a kernel that covers Temp2 = I2(i-sz2:i+sz2, j-sz2:j+sz2) . But what do you want to do in the case that that would exceed the boundaries of I2? Your current code with the subtraction of M2 and N2 would tend to imply that you do not want any partial windows, but that gets weird when the window size is dynamic. Consider for example,
15 15 15 15
15 5 15 15
15 15 5 15
15 15 15 15
(15+5)/10 --> 2 and none of the 15s have 2 locations before and after them, so if you do not want any partial windows you would have to... NaN? the output locations ? Well the outer ring of 15s you might just trim off instead of NaN. But (5+5)/10 --> 1 and the two 5's do have 1 location before and after them, so you would be calculating for them, so valid outputs for them but not valid outputs for the two 15s that are in the same 2 x 2 block on the other diagonal.
This tends to suggest that you should be using partial windows when necessary.
sigma2 = sz2/2.2;
Your sigma2 is related to the size of the kernel, possibly sz2/sqrt(5) or close to that, so that over the whole (2*(sz2+1))^2 kernel that you add up to a particular value related to the 5 or 10 used in determining the kernel size.
So... what do you want to do if you use only a partial window? Is it still appropriate to use that particular sigma2, or does the factor depend upon the size of the window being used?
For example, if you were doing a 3 x 3 mean, then you might have coded the kernel as ones(3,3)/9 but the extension to partial windows should be dividing the sum of the used values by the number of used values rather than by the constant 9.
A thought for you:
if you calculate unique( floor((I(i,j)+5)/10) ) and you were to build kernels in each of those sizes and apply them to the entire image using conv2(), then you could take those results and index into them according to sz2 for each location instead of having to build the kernel for each location.
Victoria Gruen
Victoria Gruen el 16 de Ag. de 2021
The entire code consists of two filters being subtracted from each other, so there are two sizes, two kernels, and two sigmas. The first size is chosen by the user, and the second is intended to be dependent on the pixel intensities. I am confused on the matrix dimensions, and I am not sure how to make them agree. The error pops up when I try to subtract them, because the outputs are not the same. The first output size is 576 x 720, and the second output is 678 x 822. Is there a way to make sure they are the same size or is it not possible because of the difference in filter size? Thank you so much.

Iniciar sesión para comentar.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by