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 comentarios

Geoff Hayes
Geoff Hayes el 5 de Abr. de 2015
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.
Balakrishnan Rajan
Balakrishnan Rajan el 5 de Abr. de 2015
Editada: dpb el 5 de Abr. de 2015
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.
dpb
dpb el 5 de Abr. de 2015
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.
Balakrishnan Rajan
Balakrishnan Rajan el 5 de Abr. de 2015
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.
Balakrishnan Rajan
Balakrishnan Rajan el 5 de Abr. de 2015
I would like to accept your answer but I cant seem to do it. I really appreciate the help. thank you.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 5 de Abr. de 2015

Comentada:

el 5 de Abr. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by