12 views (last 30 days)

Hello!

I am getting this error for this code to filter images. I am getting a matrix dimensions must agree error and I'm unsure where/what the exact issue is.

Here is my code:

function img_out = edge_detect(img_in)

x = [-1 0 1];

y = [-1 0 1];

Gxmat = [-1 0 1;-2 0 2; -1 0 1];

Gymat = [1 2 1; 0 0 0; -1 -2 -1];

Gx = zeros(size(img_in));

Gy = zeros(size(img_in));

fix = img_filter(img_in);

for k = 1:3

for n = 1:3

Gx = Gx + Gxmat.*img_shift(fix,x(n),y(k));

Gy = Gy + Gymat.*img_shift(fix,x(n),y(k));

end

end

img_out = rms(Gx,Gy);

end

function img_out = img_shift(img_in, horiz, vert)

x = horiz;

y = vert;

img_out = zeros(size(img_in));

if x == 0 && y == 0

img_out = img_in;

elseif x == 1 && y == 0

img_out(:,2:end) = img_in(:,1:end-1);

elseif x == 1 && y == 1

img_out(1:end-1,2:end) = img_in(2:end,1:end-1);

elseif x == 1 && y == -1

img_out(2:end,2:end) = img_in(1:end-1,1:end-1);

elseif x == -1 && y == 0

img_out(:,1:end-1) = img_in(:,2:end);

elseif x == -1 && y == 1

img_out(1:end-1,1:end-1) = img_in(2:end,2:end);

elseif x == -1 && y == -1

img_out(2:end,1:end-1) = img_in(1:end-1,2:end);

elseif x == 0 && y == 1

img_out(1:end-1,:) = img_in(2:end,:);

elseif x == 0 && y == -1

img_out(2:end,:) = img_in(1:end-1,:);

end

img_in = img_out;

img_out = img_in;

end

function img_out = img_filter(img_in)

x = [-1 0 1];

y = [-1 0 1];

img_out = zeros(size(img_in));

for k = 1:3

for n = 1:3

img_out = img_out + img_shift(img_in,x(k),y(n)); %indexing so you essentailly are taking the image out

%and running it with the shift but accounting with x and y.

end

end

img_out = img_out./9;

end

And here is the code to call to put into the command window:

dmat = zeros(10);

dmat(5:6,5:6) = 1;

dmat(:,1)=1;

dmat(:,end)=1;

dmat(1,:)=1;

dmat(end,:)=1;

img_out = edge_detect(dmat);

imagesc(img_out);

Thank you very much for your help!

Raunak Gupta
on 20 Mar 2020 at 12:50

Hi,

By running the example, I see that in edge_detect function, img_shift returns same size image that is inputted in it. So the size of img_shift(fix,x(n),y(k)) will be 10x10 as per example but the size of Gxmat and Gymat is 3x3. That is why you are not able to do elementwise multiplication. From what you are trying to implement I can see that the elementwise multiplication should be a 2-D convolution which can be done using conv2. So, I think replacing the Gx and Gy lines to below ones can clear the matrix dimension error.

Gx = Gx + conv2(img_shift(fix,x(n),y(k)),Gxmat,'same');

Gy = Gy + conv2(img_shift(fix,x(n),y(k)),Gymat,'same');

For using rms, I think only one input matrix is required the dimension input. I am not sure what is the logic behind using rms with two matrices of same size.

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 6 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/505861-matrix-dimensions-must-agree-error#comment_798255

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/505861-matrix-dimensions-must-agree-error#comment_798255

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/505861-matrix-dimensions-must-agree-error#comment_798257

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/505861-matrix-dimensions-must-agree-error#comment_798257

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/505861-matrix-dimensions-must-agree-error#comment_798259

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/505861-matrix-dimensions-must-agree-error#comment_798259

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/505861-matrix-dimensions-must-agree-error#comment_798260

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/505861-matrix-dimensions-must-agree-error#comment_798260

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/505861-matrix-dimensions-must-agree-error#comment_798263

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/505861-matrix-dimensions-must-agree-error#comment_798263

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/505861-matrix-dimensions-must-agree-error#comment_798266

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/505861-matrix-dimensions-must-agree-error#comment_798266

Sign in to comment.