Skip to content

Commit

Permalink
DOC: Add comments and Docstrings to Tracker.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
NicerNewerCar committed Oct 19, 2023
1 parent b7fb7a2 commit 0cbbad2
Showing 1 changed file with 110 additions and 3 deletions.
113 changes: 110 additions & 3 deletions libautoscoper/src/Tracker.cpp
Original file line number Diff line number Diff line change
@@ -128,7 +128,7 @@ namespace xromm {
#ifdef __APPLE__
sprintf(filename,"/Users/bardiya/autoscoper-v2/debug/image_cam%02d.pgm",count++);
#elif _WIN32
sprintf(filename,"C:/Autoscoper-v2.7/build/install/bin/Release/debug/image_cam%02d.pgm",count++);
sprintf(filename,"C:/Users/anthony.lombardi/Desktop/DebugImgs/image_cam%02d.pgm",count++);
#endif

std::cout << filename << std::endl;
@@ -307,6 +307,18 @@ void Tracker::load(const Trial& trial)

void Tracker::optimize(int frame, int dFrame, int repeats, int opt_method, unsigned int max_iter, double min_limit, double max_limit, int cf_model, unsigned int max_stall_iter)
{
/**
* Upon a call to the Tracker::optimize method, the following steps are performed:
* 1) Set the heuristic to determine the initial guess for the volume pose
* 2) Start the particle swarm optimization (PSO) based on the initial guess
* 3) For each iteration of the PSO:
* 3a) Generate 100 random particles around the current best guess
* 3b) Evaluate the fitness of each particle using normalized cross correlation (NCC)
* 3c) Update the best guess based on the best particle
* 3d) Repeat until the maximum number of iterations without change (MAX_STALL) is reached and return the best guess
* 4) Run Downhill Simplex (Nelder-Mead) on the best guess from the PSO
* 5) Update the volume pose based on the best guess
*/
intializeRandom();

optimization_method = opt_method;
@@ -585,7 +597,20 @@ void Tracker::optimize(int frame, int dFrame, int repeats, int opt_method, unsig

// Calculate Correlation for Bone Matching
std::vector <double> Tracker::trackFrame(unsigned int volumeID, double* xyzypr) const
{
{
/**
* Upon a call to the Tracker::trackFrame method, the following steps are performed:
* 1) Given a new pose, for each camera view:
* 1a) Compute the modelView and inverseModelView matrices
* 1b) Project the volume onto the film plane and calculate the 2D bounding box
* 1c) Compute the size of the bounding box in pixels
* 1d) Pass the bounding box to the drr, radiograph, mask, and background renderers
* 1e) Render the drr, radiograph, mask, and background images
* 1f) Use the drr mask to "blank out" the areas of the radiograph not covered by the drr
* 1g) Compute the normalized cross correlation (NCC) between the radiograph and drr
* 1h) Compute 1.0 - NCC value, this turns the function from a maximization to a minimization problem
* 2) Return an NCC value for each camera view
*/
std::vector<double> correlations;
CoordFrame xcframe = CoordFrame::from_xyzypr(xyzypr);

@@ -618,6 +643,76 @@ std::vector <double> Tracker::trackFrame(unsigned int volumeID, double* xyzypr)
views_[i]->radRenderer()->set_viewport(viewport[0], viewport[1],
viewport[2], viewport[3]);


/**
* drr projection
* @ = drr pixel with some intensity value
* -------------------------------
* | |
* | ^ |
* | /@\ |
* | /@@@\ |
* | /@@@@@\ |
* | \@@@@@/ |
* | \@@@/ |
* | \@/ |
* | v |
* | |
* -------------------------------
*
* crop the radiograph to the bounds of the drr
* ! = some pixel with some intensity value, inside the region of interest
* $ = some pixel with some intensity value, outside the region of interest
* -------------------------------
* |$$$$$$$$$$$$$$$$$$$$$$$$$$$$$|
* |$$$$$$$$$$$$$^$$$$$$$$$$$$$$$|
* |$$$$$$$$$$$$/!\$$$$$$$$$$$$$$|
* |$$$$$$$$$$$/!!!\$$$$$$$$$$$$$|
* |$$$$$$$$$$/!!!!!\$$$$$$$$$$$$|
* |$$$$$$$$$$\!!!!!/$$$$$$$$$$$$|
* |$$$$$$$$$$$\!!!/$$$$$$$$$$$$$|
* |$$$$$$$$$$$$\!/$$$$$$$$$$$$$$|
* |$$$$$$$$$$$$$v$$$$$$$$$$$$$$$|
* |$$$$$$$$$$$$$$$$$$$$$$$$$$$$$|
* -------------------------------
*
* Create the background mask
* This just contains the background pixels
* 255 / white
*
* Create the binary drr mask
* -------------------------------
* |11111111111111111111111111111|
* |11111111111110111111111111111|
* |11111111111100011111111111111|
* |11111111111000001111111111111|
* |11111111110000000111111111111|
* |11111111110000000111111111111|
* |11111111111000001111111111111|
* |11111111111100011111111111111|
* |11111111111110111111111111111|
* |11111111111111111111111111111|
* -------------------------------
*
* Mask the radiograph with the drr mask
* We are assigning 1 to be 255 and 0 to
* be the value of the radiograph pixel
* -------------------------------
* | |
* | ^ |
* | /!\ |
* | /!!!\ |
* | /!!!!!\ |
* | \!!!!!/ |
* | \!!!/ |
* | \!/ |
* | v |
* | |
* -------------------------------
*
* Calculate the NCC between the drr and the masked radiograph
*/

// Render the DRR and Radiograph
views_[i]->renderDrrSingle(volumeID, rendered_drr_, render_width, render_height);
views_[i]->renderRad(rendered_rad_, render_width, render_height);
@@ -784,6 +879,14 @@ std::vector<unsigned char> Tracker::getImageData(unsigned volumeID, unsigned cam

void Tracker::calculate_viewport(const CoordFrame& modelview,double* viewport) const
{
/**
* Despite being called a viewport, this is actually the 2D bounding box of the volume
* after it has been projected onto the film/view plane. The term viewport refers to the
* coordinate system the bounding box is returned in.
*
* The film plane sits at z = 2.
* The bounding box is returned as {x_min, y_min, x_max, y_max}
*/
// Calculate the minimum and maximum values of the bounding box
// corners after they have been projected onto the view plane
double min_max[4] = {1.0,1.0,-1.0,-1.0};
@@ -802,7 +905,11 @@ void Tracker::calculate_viewport(const CoordFrame& modelview,double* viewport) c
double corner[3];
modelview.point_to_world_space(&corners[3*j],corner);

// Calculate its projection onto the film plane, where z = -2
// Calculate its projection onto the film plane, where z = 2
// xfp = - f * xc / zc
// yfp = - f * yc / zc
// zfp = - f * zc / zc = -f
// Where, (f)ocal length = 2, *c refers to camera coordinates, and *fp refers to film plane coordinates
double film_plane[3];
film_plane[0] = -2*corner[0]/corner[2];
film_plane[1] = -2*corner[1]/corner[2];

0 comments on commit 0cbbad2

Please sign in to comment.