# Creating a function that has an input from another algorithm

2 views (last 30 days)
arun Dhillon on 1 Feb 2019
Answered: arun Dhillon on 2 Feb 2019
HEllo Everone
I am having an algorithm which is giving me an output in the form of a variable (on my workspace). The Variable(centers) is an n*2 array and is giving me center locatations or x,y coordinates of bunch of different circles on an image. What i wanna do is to use that centers variable and create bounding boxes on each of those coordinates. For creating the bounding box on any x,y cordinate, i am using the following algorithm.
figure, imshow(im1)
hold on
x=425.7450;
y= 211.1882;
plot(centers)
boxHalfWidth = 40; %
boxXStart = x-boxHalfWidth;
boxYStart = y-boxHalfWidth;
boxWidth = 2 * boxHalfWidth;
boxHeight = 2 * boxHalfWidth;
rectangle('Position',[boxXStart boxYStart boxWidth boxHeight])
Can anyone please help me, about how do i create a function in which i give the input as centers for each x,y pair and in turn execute the above written algorithm (boundbox) sequentially on each x,y values in that centers array, so that i get bounding boxes on all those x,y pairs.
Please find the attached screenshot of the centers array as well. Any suggestion or help will be greatly appreciated .

arun Dhillon on 2 Feb 2019
Actually i figured out the way to do this from some resources, but thanks a lot for every1's contribution.
Here is how i am able to get it
n = size(centers,1);
figure, imshow(mush);
hold on
for k=1:n
% run your algorithm here with x = xy(k,1) and y = xy(k,2)
x = centers(k,1);
y = centers(k,2);
plot(centers)
boxHalfWidth = 40; %
boxXStart = x-boxHalfWidth;
boxYStart = y-boxHalfWidth;
boxWidth = 2 * boxHalfWidth;
boxHeight = 2 * boxHalfWidth;
rectangle('Position',[boxXStart boxYStart boxWidth boxHeight])
end

Geoff Hayes on 1 Feb 2019
arun - why not make your Centredetect.m into a function that can then call your other function. So something like
function Centredetect(imageFilename)
% resize to fit on screen
% etc. the remainder of your code from the script
% now once you have centers, you can call the other function as
showImageAndBoundingBoxes(img, centers);
end
Your other function would then look like (in a fie named showImageAndBoundingBoxes.m or just nest it in the above one)
function showImageAndBoundingBoxes(imageToShow, centers)
figure, imshow(imageToShow)
hold on
for k = 1:size(centers,1)
x = centers(k,1);
y = centers(k,2);
boxHalfWidth = 40; %
boxXStart = x-boxHalfWidth;
boxYStart = y-boxHalfWidth;
boxWidth = 2 * boxHalfWidth;
boxHeight = 2 * boxHalfWidth;
rectangle('Position',[boxXStart boxYStart boxWidth boxHeight])
end
end
That might do what you want...the above is untested but hopefully it is clear enough about what you can do.
Geoff Hayes on 1 Feb 2019
Hi Geoff, thanks a lot for the reply, but i am still not able to get the bounding boxes on the right x,y coordinates. I have attached the image files for the way i am using my functions . I am getting the bounding boxes,, but not at the locations that i want them to be. I tried to change some of the things in the function code in couple of different ways, but i am not getting them on the precise x,y corirdinates. Please see the attached images. Also the bounding boxes that i am getting are on top left corner of the image as you may see that in the result image file. Please tell me if you think, i am doing something wrong. Coz i am also getting some resize error as well, but i am already resizing the image in the centerdetect func. ALso i have attached the original image as well.

### Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by