Skip to content

Commit

Permalink
Added average center calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Grijalva committed May 23, 2014
1 parent 9ee1f85 commit 80e8ae4
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions AR_Simple/Main.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ ARParam cparam;
ObjectData_T* object;
int objectNum = -1; // Number of patterns in file pointed to by patt_name

int** centers; // Contains the center for each image
int img_num = 0; // The currect image the program is on

/*
Initialize ARToolkit
*/
Expand Down Expand Up @@ -98,6 +101,29 @@ void get_midpoint(double* midpoint, double** pair){
midpoint[1] = (pair[0][1] + pair[1][1]) / 2.0;
}

/*
Calculate the average sphere location using x,y data from 'centers' array
Writes the result to the output.csv file
num: number of images in centers array
*/
void calculateAverage(int num)
{
int rx = 0;
int ry = 0;
for (int i = 0; i < num; i++) // Calculate average center for all images
{
if (centers[i][0] != 0 && centers[i][1] != 0) // Ignores images that pattern detection failed on
{
rx += centers[i][0];
ry += centers[i][1];
}
else num--;
}
rx /= num; ry /= num;
printf("Average location: %i, %i\n", rx, ry);
writeLine("Average", rx, ry);
}

/*
Main loop for ARToolkit. Runs until program exits
img_name: The name of the image file. This is used when writing results to csv file
Expand Down Expand Up @@ -145,11 +171,15 @@ static void mainLoop(char* img_name)
get_midpoint(&midpoint, &pair);
printf("Center is: %5i,%5i\n", (int)midpoint[0], (int)midpoint[1]);
writeLine(img_name, (int)midpoint[0], (int)midpoint[1]);
centers[img_num][0] = (int)midpoint[0]; // Save the x position
centers[img_num++][1] = (int)midpoint[1]; // Save the y position
}
else
{
printf("%i patterns isnt enough to find a center point!\n", marker_num);
writeLine(img_name, 0, 0);
centers[img_num][0] = 0; // Save the x position
centers[img_num++][1] = 0; // Save the y position
}
}

Expand All @@ -170,6 +200,9 @@ int main(int argc, char **argv)
}
}

centers = calloc((argc - 1), sizeof(int)); // Allocate space to hold sphere location for each image
for (int i = 0; i < argc - 1; i++) centers[i] = calloc(2, sizeof(int));

createOutputFile(); // Delete any old output.csv file and create a fresh one
for (int i = 1; i < argc; i++) // For each input image
{
Expand All @@ -185,6 +218,9 @@ int main(int argc, char **argv)
free(dataPtr);
printf("=================== Done! ===================\n\n");
}

calculateAverage(argc - 1);

printf("============================\n");
printf("See output.csv for results\n");
printf("============================\n");
Expand Down

0 comments on commit 80e8ae4

Please sign in to comment.