Info
This question is closed. Reopen it to edit or answer.
??? Subscript indices must either be real positive integers or logicals.
1 view (last 30 days)
Show older comments
i am new and doing 2d convolution but i am stuck at this error. here is the code:
function output_args = convo(img,k )
%user giant malik
%UNTITLED4 Summary of this function goes here
% Detailed explanation goes here
%I = [1 2 3;
%4 5 6;
%7 8 9;];
%k = [0 0 0;
%0 1 0;
%0 0 0];
[r1 c1]= size(img);
[r2 c2]=size(k);
length = r1+(r2-1); % for creating new dimensiont
row = length; %
col = length;
a= (r2-1)/2; b= (c2-1)/2; % start value for calculating convultion
s= a*(-1)+1;
t=(b)*(-1)+1;
%disp(s);
output_args=zeros([row, col]);
z=s;w=t;
for x=0:length
for y=0:length
for s=s:a+1
% disp(s);
for t= t:b+1
output_args(x,y)=output_args(x,y) + (output_args(s, t)* f(z-s,w-t)); % error pointing here
end
end
w=w+1;
end
z=z+1;
end
end
0 Comments
Answers (2)
Azzi Abdelmalek
on 17 May 2014
In your code there are indices equal to 0.
output_args(x,y) % with x starting at 0 will give you an error, you should start at 1
1 Comment
Image Analyst
on 17 May 2014
Lots of problems with that code. No checking for number of dimensions of img. No initialization of output_args. No try/catch. Virtually no comments. No definition of the "f" function, Etc.
What happens if someone passes in a color image for img? Kaboom! Why? See this http://blogs.mathworks.com/steve/2011/03/22/too-much-information-about-the-size-function/
Put a try/catch into your function:
try
% Some code that might generate an error.
catch ME
errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
fprintf(1, '%s\n', errorMessage);
uiwait(warndlg(errorMessage));
end
Move this to be first line, right after, or even right before, the try line:
output_args = [];
Now, .... the main reason for your error is that you used f, which is undefined . You're not really programming up a convolution correctly. Think about what indexes you should use. Perhaps it would be easier for you to extract a little sub-image from the main image, which is right under your sliding kernel window. Adding some comment before each line might help you figure out where you're going wrong. Give it a try and if you can't figure it out, let me know.
1 Comment
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!