How to store individual return value of a function

조회 수: 10 (최근 30일)
Tipu Sultan
Tipu Sultan 2019년 4월 25일
댓글: Tipu Sultan 2019년 4월 26일
I want to store individeual return values to a variable
The code is as follows:
function tri=imageReadFromFolder()
something!
tri=[z,distance,angle];
end
As can be seen it basically reads set of images from a folder and for each image it will have individual 'z,distance,angle' .But after it reads it only shows the result for the last images.
Any help on how to solve this will be highly appreciated.
  댓글 수: 2
Jan
Jan 2019년 4월 25일
편집: Jan 2019년 4월 25일
The question is not clear. What is "something!"? Where does "z,distance,angle" come from? It cannot be seen (especially not "basically"), that a set of images is read here. There is no code to read images, there I cannot guess, why the data of the "last" image are replied only. Most likely you use a loop and write:
for k = 1:numel(images)
img = import_image()
z = fcn(Img)
end
instead of
z(k) = fcn(Img)
% ^^^
Please edit your question and post some working code, which reproduces the problem. If we guess, what the problem is, the answers will be more or less unrelated.
Tipu Sultan
Tipu Sultan 2019년 4월 26일
I am giving you the whole code:
function tri=imageReadFromFolder()
D = 'C:\Users\Tipu_Standard\Desktop\testIMAGES';
S = dir(fullfile(D,'Original*.jpg')); % pattern to match filenames.
for i = 1:numel(S)
F = fullfile(D,S(i).name);
I = imread(F);
B = rgb2gray(I);
A=adapthisteq(B);
% Display the original image
figure,imshow(A);
distance =0;
angle=0;
rmin=8;
rmax=30;
% Find all the circles with radius >= 8 and radius <= 30
[centersdark,radiidark] = imfindcircles(A,[rmin rmax],'Objectpolarity','dark','Sensitivity',0.913);
k=numel(radiidark);
viscircles(centersdark,radiidark ,'EdgeColor','b');
if(k==0)
z=0;
else
z=1;
p=2*radiidark;
f=2.1;
w=62;
h=144;
s=2.88;
distance=(f*w*h)/(p*s);
a=(192/2)-centersdark(1);
b=144-centersdark(2);
angle=atan2(a,b)*(180/pi);
end
tri=[z,distance,angle];
end
end

댓글을 달려면 로그인하십시오.

채택된 답변

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 4월 26일
편집: KALYAN ACHARJYA 2019년 4월 26일
Inputs in Main script help to change the input easily.
Main scripts:
D = 'C:\Users\Tipu_Standard\Desktop\testIMAGES';
S = dir(fullfile(D,'Original*.jpg')); % pattern to match filenames.
for i = 1:numel(S)
F = fullfile(D,S(i).name);
I = imread(F);
% Next line changes required, k{i}, I missed the account the size of function output please check the below @Stephen Comment
k(i)=fun1(I);
end
Here k gives the values of individual images, like k(1),k(2)...
Make the different function file
function tri=fun1(I)
B=rgb2gray(I);
A=adapthisteq(B);
% Display the original image
figure,imshow(A);
distance =0;
angle=0;
rmin=8;
rmax=30;
% Find all the circles with radius >= 8 and radius <= 30
[centersdark,radiidark] = imfindcircles(A,[rmin rmax],'Objectpolarity','dark','Sensitivity',0.913);
k=numel(radiidark);
viscircles(centersdark,radiidark ,'EdgeColor','b');
if(k==0)
z=0;
else
z=1;
p=2*radiidark;
f=2.1;
w=62;
h=144;
s=2.88;
distance=(f*w*h)/(p*s);
a=(192/2)-centersdark(1);
b=144-centersdark(2);
angle=atan2(a,b)*(180/pi);
end
tri=[z,distance,angle];
end
  댓글 수: 9
Tipu Sultan
Tipu Sultan 2019년 4월 26일
thank you all :)
Tipu Sultan
Tipu Sultan 2019년 4월 26일
yes it worked thanks :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

제품


릴리스

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by