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

Geoff Hayes
Geoff Hayes 2015년 4월 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.
Balakrishnan Rajan
Balakrishnan Rajan 2015년 4월 5일
편집: dpb 2015년 4월 5일
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 2015년 4월 5일
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에 대해 자세히 알아보기

질문:

2015년 4월 5일

댓글:

2015년 4월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by