Matrix dimensions must agree error
이전 댓글 표시
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
Star Strider
2020년 2월 17일
‘I am getting a matrix dimensions must agree error...’
Where?
Arshia Nazerani Houshmand
2020년 2월 17일
편집: Arshia Nazerani Houshmand
2020년 2월 17일
Star Strider
2020년 2월 17일
I cannot run your code, and because there are no comments or any other description, I have no idea what it does.
Unless the output of ‘img_shift’ is a (3x3) matrix, you will continue to get that error.
Think carefully about what you want to be doing, and then write your code to do exactly that.
Arshia Nazerani Houshmand
2020년 2월 17일
Walter Roberson
2020년 2월 17일
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
2020년 2월 17일
답변 (1개)
Raunak Gupta
2020년 3월 20일
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.
카테고리
도움말 센터 및 File Exchange에서 Object Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!