Matrix dimensions must agree error

1 view (last 30 days)
Arshia Nazerani Houshmand
Arshia Nazerani Houshmand on 17 Feb 2020
Answered: Raunak Gupta on 20 Mar 2020
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!
  6 Comments
Walter Roberson
Walter Roberson on 17 Feb 2020
Your code assumes that img_in is a 2d array. Not, for example, an rgb image. In the code before this have you tested to be sure you will be passing 2d?
Arshia Nazerani Houshmand
Arshia Nazerani Houshmand on 17 Feb 2020
Here is the question prompt:
Write a function which will take, as input, a 2D image in double format, apply the averaging filter, and generate an edge-detected image using the Sobel method. There are two kernels used in the edge detection filter process, Gx and Gy as shown in the Edge Detection section. function img_out = edge_detect(img_in) Your function should use the img_shift() and img_filter() functions you wrote in the previous problems.
The code before this which is the 3rd function: img_out = img_filter(img_in) , runs perfectly and provides the correct answer in MatLab grader. This whole problem is a precurser to being able to take an BW image and running a filter on it. Unfortunetly, I cannot run a BW image filter unless I get this current 2D array code to work.

Sign in to comment.

Answers (1)

Raunak Gupta
Raunak Gupta on 20 Mar 2020
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.

Categories

Find more on Display Image in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!