003	for (j=0;j<NUM_NEIGHBORS;j++)/* for all neighbors of next node */

The loop variable j selects the row in the energy/direction table. The range of this variable corresponds to a 3x3 grid centered around the current position of the snaxel. This computes the minimum energy over all possiblepositions to move this snaxel; that energy is later stored in the energy matrix.


006	for (k=0;k<NUM_NEIGHBORS;k++) /*for all neighors of curr node*/
007	        {
008	            nextNode = snaxel[i+1] + neighbor[j] 
009	            currNode = snaxel[i]   + neighbor[k]
010	            energy = energyMtx[i-1][k] + E(currNode, nextNode)
011	            if (energy < min_energy)
012	            {
013	                min_energy = energy
014	                min_position = k
015	            }
016	        }

In this loop, we are finding the minimum over k in the following equation.

We are building an energy table for the snaxel, translated to its neighbor. Each entry in the table represents the minimum energy associated with each possibility for the choice of the snaxel. We search over all possibilities in our chosen neighborhood (indexed by k) for the snaxel and compute the corresponding energy. We then pick the minimum over these possibilities and store that energy in the table along with which neighbor was chosen for . When we have done this for each neighbor, we have built one column in the energy table.

This part of the code comes from the paper by Amini, pp. 861 - 862 , equations 52 & 53.

READ TUTORIAL ON THIS TOPIC


017	/* Store minimum energy into table */
018	energyMtx[i][j] = min_energy
019	posMtx[i][j] = min_position

These two lines store in the table the minimum energy and position yielding this energy corresponding to the ith snaxel and its jth neighbor.


023	/* search final column for minimum energy */ 

This loop searches the last column of the table to find the minimum energy and to determine a starting point for the backwards iteration that will give the updated optimal snaxel positions.


035	/* search backwards through table to find optimum postions */

This loop takes as its starting point, the position of the minimum of the last column of the energy matrix, and updates the last snaxel to reflect the best choice for that snaxel. It then uses the corresponding value in the position matrix as the next starting point. This is repeated until the whole snake is updated (number of iterations is NUM_SNAXELS-1). This completes one execution of the algorithm.