Having issues with loops inside function.

I want to create a function for finding the bounding box of images(rgb). The code works fine when I have it as a part of the main script. But when I implement it as a function it returns the value which I initialized with and not the values computed after the loops are executed. Which is why I have dummy variables and then assigning the return variables with their values. But this doesnt help me either.

5 个评论

Balakrishnan - please provide some code so that we can get an idea of what you have attempted and how best to go about solving the problem. Or just provide the function signature and how you are calling this function.
function [area,toprow,botrow,leftcol,rightcol] = ...
impro(image,redval,blueval,greenval)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
count=0;
l=0;
r=0;
b=0;
t=0;
for i=1:32
for j=1:32
if((image(i,j,1)==redval) & ...
(image(i,j,2)==blueval) & ...
(image(i,j,3)==greenval))
count=count+1;
if(count==1)
t=i;
b=i;
l=j;
r=j;
end
if(j<l)
l=j;
end
if(i>b)
b=i;
end
if(j>r)
r=j;
end
end
end
end
area=count;
leftcol=l;
rightcol=r;
toprow=t;
botrow=b;
end
Sorry about that. I thought I had added that as an attachment. It just returns zero for all my outputs. Its like the loops doesnt execute at all.
Have you stepped through with the debugger to check on the logic/values inside the function?
Looks like the loops could be replaced entirely with a suitable use of find and the 'first','last' optional inputs, btw.
Sorry, the problem in this case was just that the color isnt exactly a particular value due to a diffuse component of the shade. I understood this only after I saw the image itself with imshow command. Thanks for the help anyways.
PS: I am using the find now. Thanks a lot.
I would like to accept your answer but I cant seem to do it. I really appreciate the help. thank you.

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by