// Regularized version of the Heaviside step function, // parameterized by a small positive number 'e' void heaviside(MAT *H, MAT *z, double v, double e) { int m = z->m, n = z->n, i, j; // Precompute constants to avoid division in the for loops below double one_over_pi = 1.0 / PI; double one_over_e = 1.0 / e; // Compute H = (1 / pi) * atan((z * v) / e) + 0.5 for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { double z_val = m_get_val(z, i, j) * v; double H_val = one_over_pi * atan(z_val * one_over_e) + 0.5; m_set_val(H, i, j, H_val); } } // A simpler, faster approximation of the Heaviside function /* for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { double z_val = m_get_val(z, i, j) * v; double H_val = 0.5; if (z_val < -0.0001) H_val = 0.0; else if (z_val > 0.0001) H_val = 1.0; m_set_val(H, i, j, H_val); } } */ }
void print(MAT *m) { unsigned int i,j; for (i=0; i<m->n; i++){ for (j=0; j<m->m; j++) printf("%f ",m_get_val(m,i,j)); printf("\n"); } }
// Returns the sum of all of the elements in the specified matrix double sum_m(MAT *matrix) { if (matrix == NULL) return 0.0; int i, j; double sum = 0.0; for (i = 0; i < matrix->m; i++) for (j = 0; j < matrix->n; j++) sum += m_get_val(matrix, i, j); return sum; }
//Given a matrix, return the matrix containing an approximation of the gradient matrix dM/dy MAT * gradient_y(MAT * input) { int i, j; MAT * result = m_get(input->m, input->n); for(i = 0; i < result->n; i++) { for(j = 0; j < result->m; j++) { if(j==0) m_set_val(result, j, i, m_get_val(input, j+1, i) - m_get_val(input, j, i)); else if(j==input->m-1) m_set_val(result, j, i, m_get_val(input, j, i) - m_get_val(input, j-1, i)); else m_set_val(result, j, i, (m_get_val(input, j+1, i) - m_get_val(input, j-1, i)) / 2.0); } } return result; }
// Initializes data on the GPU for the MGVF kernel void IMGVF_OpenCL_init(MAT **IE, int num_cells) { cl_int error; // Allocate array of offsets to each cell's image size_t mem_size = sizeof(int) * num_cells; host_I_offsets = (int *) malloc(mem_size); device_I_offsets = clCreateBuffer(context, CL_MEM_READ_ONLY, mem_size, NULL, &error); check_error(error, __FILE__, __LINE__); // Allocate arrays to hold the dimensions of each cell's image host_m_array = (int *) malloc(mem_size); host_n_array = (int *) malloc(mem_size); device_m_array = clCreateBuffer(context, CL_MEM_READ_ONLY, mem_size, NULL, &error); check_error(error, __FILE__, __LINE__); device_n_array = clCreateBuffer(context, CL_MEM_READ_ONLY, mem_size, NULL, &error); check_error(error, __FILE__, __LINE__); // Figure out the size of all of the matrices combined int i, j, cell_num; size_t total_size = 0; for (cell_num = 0; cell_num < num_cells; cell_num++) { MAT *I = IE[cell_num]; size_t size = I->m * I->n; total_size += size; } total_mem_size = total_size * sizeof(float); // Allocate host memory just once for all cells host_I_all = (float *) malloc(total_mem_size); // Allocate device memory just once for all cells device_I_all = clCreateBuffer(context, CL_MEM_READ_ONLY, total_mem_size, NULL, &error); check_error(error, __FILE__, __LINE__); device_IMGVF_all = clCreateBuffer(context, CL_MEM_READ_WRITE, total_mem_size, NULL, &error); check_error(error, __FILE__, __LINE__); // Copy each initial matrix into the allocated host memory int offset = 0; for (cell_num = 0; cell_num < num_cells; cell_num++) { MAT *I = IE[cell_num]; // Determine the size of the matrix int m = I->m, n = I->n; int size = m * n; // Store memory dimensions host_m_array[cell_num] = m; host_n_array[cell_num] = n; // Store offsets to this cell's image host_I_offsets[cell_num] = offset; // Copy matrix I (which is also the initial IMGVF matrix) into the overall array for (i = 0; i < m; i++) for (j = 0; j < n; j++) host_I_all[offset + (i * n) + j] = (float) m_get_val(I, i, j); offset += size; } // Copy I matrices (which are also the initial IMGVF matrices) to device error = clEnqueueWriteBuffer(command_queue, device_I_all, CL_TRUE, 0, total_mem_size, (void *) host_I_all, 0, NULL, NULL); check_error(error, __FILE__, __LINE__); error = clEnqueueWriteBuffer(command_queue, device_IMGVF_all, CL_TRUE, 0, total_mem_size, (void *) host_I_all, 0, NULL, NULL); check_error(error, __FILE__, __LINE__); // Copy offsets array to device error = clEnqueueWriteBuffer(command_queue, device_I_offsets, CL_TRUE, 0, mem_size, (void *) host_I_offsets, 0, NULL, NULL); check_error(error, __FILE__, __LINE__); // Copy memory dimension arrays to device error = clEnqueueWriteBuffer(command_queue, device_m_array, CL_TRUE, 0, mem_size, (void *) host_m_array, 0, NULL, NULL); check_error(error, __FILE__, __LINE__); error = clEnqueueWriteBuffer(command_queue, device_n_array, CL_TRUE, 0, mem_size, (void *) host_n_array, 0, NULL, NULL); check_error(error, __FILE__, __LINE__); }
// Main int main(int argc, char ** argv) { // Choose the best GPU in case there are multiple available choose_GPU(); // Keep track of the start time of the program long long program_start_time = get_time(); if (argc !=3){ fprintf(stderr, "usage: %s <input file> <number of frames to process>", argv[0]); exit(1); } // Let the user specify the number of frames to process int num_frames = atoi(argv[2]); // Open video file char *video_file_name = argv[1]; avi_t *cell_file = AVI_open_input_file(video_file_name, 1); if (cell_file == NULL) { AVI_print_error("Error with AVI_open_input_file"); return -1; } int i, j, *crow, *ccol, pair_counter = 0, x_result_len = 0, Iter = 20, ns = 4, k_count = 0, n; MAT *cellx, *celly, *A; double *GICOV_spots, *t, *G, *x_result, *y_result, *V, *QAX_CENTERS, *QAY_CENTERS; double threshold = 1.8, radius = 10.0, delta = 3.0, dt = 0.01, b = 5.0; // Extract a cropped version of the first frame from the video file MAT *image_chopped = get_frame(cell_file, 0, 1, 0); printf("Detecting cells in frame 0\n"); // Get gradient matrices in x and y directions MAT *grad_x = gradient_x(image_chopped); MAT *grad_y = gradient_y(image_chopped); // Allocate for gicov_mem and strel gicov_mem = (float*) malloc(sizeof(float) * grad_x->m * grad_y->n); strel = (float*) malloc(sizeof(float) * strel_m * strel_n); m_free(image_chopped); int grad_m = grad_x->m; int grad_n = grad_y->n; #pragma acc data create(sin_angle,cos_angle,theta,tX,tY) \ create(gicov_mem[0:grad_x->m*grad_y->n]) { // Precomputed constants on GPU compute_constants(); // Get GICOV matrices corresponding to image gradients long long GICOV_start_time = get_time(); MAT *gicov = GICOV(grad_x, grad_y); long long GICOV_end_time = get_time(); // Dilate the GICOV matrices long long dilate_start_time = get_time(); MAT *img_dilated = dilate(gicov); long long dilate_end_time = get_time(); } /* end acc data */ // Find possible matches for cell centers based on GICOV and record the rows/columns in which they are found pair_counter = 0; crow = (int *) malloc(gicov->m * gicov->n * sizeof(int)); ccol = (int *) malloc(gicov->m * gicov->n * sizeof(int)); for(i = 0; i < gicov->m; i++) { for(j = 0; j < gicov->n; j++) { if(!double_eq(m_get_val(gicov,i,j), 0.0) && double_eq(m_get_val(img_dilated,i,j), m_get_val(gicov,i,j))) { crow[pair_counter]=i; ccol[pair_counter]=j; pair_counter++; } } } GICOV_spots = (double *) malloc(sizeof(double) * pair_counter); for(i = 0; i < pair_counter; i++) GICOV_spots[i] = sqrt(m_get_val(gicov, crow[i], ccol[i])); G = (double *) calloc(pair_counter, sizeof(double)); x_result = (double *) calloc(pair_counter, sizeof(double)); y_result = (double *) calloc(pair_counter, sizeof(double)); x_result_len = 0; for (i = 0; i < pair_counter; i++) { if ((crow[i] > 29) && (crow[i] < BOTTOM - TOP + 39)) { x_result[x_result_len] = ccol[i]; y_result[x_result_len] = crow[i] - 40; G[x_result_len] = GICOV_spots[i]; x_result_len++; } } // Make an array t which holds each "time step" for the possible cells t = (double *) malloc(sizeof(double) * 36); for (i = 0; i < 36; i++) { t[i] = (double)i * 2.0 * PI / 36.0; } // Store cell boundaries (as simple circles) for all cells cellx = m_get(x_result_len, 36); celly = m_get(x_result_len, 36); for(i = 0; i < x_result_len; i++) { for(j = 0; j < 36; j++) { m_set_val(cellx, i, j, x_result[i] + radius * cos(t[j])); m_set_val(celly, i, j, y_result[i] + radius * sin(t[j])); } } A = TMatrix(9,4); V = (double *) malloc(sizeof(double) * pair_counter); QAX_CENTERS = (double * )malloc(sizeof(double) * pair_counter); QAY_CENTERS = (double *) malloc(sizeof(double) * pair_counter); memset(V, 0, sizeof(double) * pair_counter); memset(QAX_CENTERS, 0, sizeof(double) * pair_counter); memset(QAY_CENTERS, 0, sizeof(double) * pair_counter); // For all possible results, find the ones that are feasibly leukocytes and store their centers k_count = 0; for (n = 0; n < x_result_len; n++) { if ((G[n] < -1 * threshold) || G[n] > threshold) { MAT * x, *y; VEC * x_row, * y_row; x = m_get(1, 36); y = m_get(1, 36); x_row = v_get(36); y_row = v_get(36); // Get current values of possible cells from cellx/celly matrices x_row = get_row(cellx, n, x_row); y_row = get_row(celly, n, y_row); uniformseg(x_row, y_row, x, y); // Make sure that the possible leukocytes are not too close to the edge of the frame if ((m_min(x) > b) && (m_min(y) > b) && (m_max(x) < cell_file->width - b) && (m_max(y) < cell_file->height - b)) { MAT * Cx, * Cy, *Cy_temp, * Ix1, * Iy1; VEC *Xs, *Ys, *W, *Nx, *Ny, *X, *Y; Cx = m_get(1, 36); Cy = m_get(1, 36); Cx = mmtr_mlt(A, x, Cx); Cy = mmtr_mlt(A, y, Cy); Cy_temp = m_get(Cy->m, Cy->n); for (i = 0; i < 9; i++) m_set_val(Cy, i, 0, m_get_val(Cy, i, 0) + 40.0); // Iteratively refine the snake/spline for (i = 0; i < Iter; i++) { int typeofcell; if(G[n] > 0.0) typeofcell = 0; else typeofcell = 1; splineenergyform01(Cx, Cy, grad_x, grad_y, ns, delta, 2.0 * dt, typeofcell); } X = getsampling(Cx, ns); for (i = 0; i < Cy->m; i++) m_set_val(Cy_temp, i, 0, m_get_val(Cy, i, 0) - 40.0); Y = getsampling(Cy_temp, ns); Ix1 = linear_interp2(grad_x, X, Y); Iy1 = linear_interp2(grad_x, X, Y); Xs = getfdriv(Cx, ns); Ys = getfdriv(Cy, ns); Nx = v_get(Ys->dim); for (i = 0; i < Ys->dim; i++) v_set_val(Nx, i, v_get_val(Ys, i) / sqrt(v_get_val(Xs, i)*v_get_val(Xs, i) + v_get_val(Ys, i)*v_get_val(Ys, i))); Ny = v_get(Xs->dim); for (i = 0; i < Xs->dim; i++) v_set_val(Ny, i, -1.0 * v_get_val(Xs, i) / sqrt(v_get_val(Xs, i)*v_get_val(Xs, i) + v_get_val(Ys, i)*v_get_val(Ys, i))); W = v_get(Nx->dim); for (i = 0; i < Nx->dim; i++) v_set_val(W, i, m_get_val(Ix1, 0, i) * v_get_val(Nx, i) + m_get_val(Iy1, 0, i) * v_get_val(Ny, i)); V[n] = mean(W) / std_dev(W); // Find the cell centers by computing the means of X and Y values for all snaxels of the spline contour QAX_CENTERS[k_count] = mean(X); QAY_CENTERS[k_count] = mean(Y) + TOP; k_count++; // Free memory v_free(W); v_free(Ny); v_free(Nx); v_free(Ys); v_free(Xs); m_free(Iy1); m_free(Ix1); v_free(Y); v_free(X); m_free(Cy_temp); m_free(Cy); m_free(Cx); } // Free memory v_free(y_row); v_free(x_row); m_free(y); m_free(x); } } // Free memory free(gicov_mem); free(strel); free(V); free(ccol); free(crow); free(GICOV_spots); free(t); free(G); free(x_result); free(y_result); m_free(A); m_free(celly); m_free(cellx); m_free(img_dilated); m_free(gicov); m_free(grad_y); m_free(grad_x); // Report the total number of cells detected printf("Cells detected: %d\n\n", k_count); // Report the breakdown of the detection runtime printf("Detection runtime\n"); printf("-----------------\n"); printf("GICOV computation: %.5f seconds\n", ((float) (GICOV_end_time - GICOV_start_time)) / (1000*1000)); printf(" GICOV dilation: %.5f seconds\n", ((float) (dilate_end_time - dilate_start_time)) / (1000*1000)); printf(" Total: %.5f seconds\n", ((float) (get_time() - program_start_time)) / (1000*1000)); // Now that the cells have been detected in the first frame, // track the ellipses through subsequent frames if (num_frames > 1) printf("\nTracking cells across %d frames\n", num_frames); else printf("\nTracking cells across 1 frame\n"); long long tracking_start_time = get_time(); int num_snaxels = 20; ellipsetrack(cell_file, QAX_CENTERS, QAY_CENTERS, k_count, radius, num_snaxels, num_frames); printf(" Total: %.5f seconds\n", ((float) (get_time() - tracking_start_time)) / (float) (1000*1000*num_frames)); // Report total program execution time printf("\nTotal application run time: %.5f seconds\n", ((float) (get_time() - program_start_time)) / (1000*1000)); return 0; }
void ellipseevolve(MAT *f, double *xc0, double *yc0, double *r0, double *t, int Np, double Er, double Ey) { /* % ELLIPSEEVOLVE evolves a parametric snake according % to some energy constraints. % % INPUTS: % f............potential surface % xc0,yc0......initial center position % r0,t.........initial radii & angle vectors (with Np elements each) % Np...........number of snaxel points per snake % Er...........expected radius % Ey...........expected y position % % OUTPUTS % xc0,yc0.......final center position % r0...........final radii % % Matlab code written by: DREW GILLIAM (based on work by GANG DONG / % NILANJAN RAY) % Ported to C by: MICHAEL BOYER */ // Constants double deltax = 0.2; double deltay = 0.2; double deltar = 0.2; double converge = 0.1; double lambdaedge = 1; double lambdasize = 0.2; double lambdapath = 0.05; int iterations = 1000; // maximum number of iterations int i, j; // Initialize variables double xc = *xc0; double yc = *yc0; double *r = (double *) malloc(sizeof(double) * Np); for (i = 0; i < Np; i++) r[i] = r0[i]; // Compute the x- and y-gradients of the MGVF matrix MAT *fx = gradient_x(f); MAT *fy = gradient_y(f); // Normalize the gradients int fh = f->m, fw = f->n; for (i = 0; i < fh; i++) { for (j = 0; j < fw; j++) { double temp_x = m_get_val(fx, i, j); double temp_y = m_get_val(fy, i, j); double fmag = sqrt((temp_x * temp_x) + (temp_y * temp_y)); m_set_val(fx, i, j, temp_x / fmag); m_set_val(fy, i, j, temp_y / fmag); } } double *r_old = (double *) malloc(sizeof(double) * Np); VEC *x = v_get(Np); VEC *y = v_get(Np); // Evolve the snake int iter = 0; double snakediff = 1.0; while (iter < iterations && snakediff > converge) { // Save the values from the previous iteration double xc_old = xc, yc_old = yc; for (i = 0; i < Np; i++) { r_old[i] = r[i]; } // Compute the locations of the snaxels for (i = 0; i < Np; i++) { v_set_val(x, i, xc + r[i] * cos(t[i])); v_set_val(y, i, yc + r[i] * sin(t[i])); } // See if any of the points in the snake are off the edge of the image double min_x = v_get_val(x, 0), max_x = v_get_val(x, 0); double min_y = v_get_val(y, 0), max_y = v_get_val(y, 0); for (i = 1; i < Np; i++) { double x_i = v_get_val(x, i); if (x_i < min_x) min_x = x_i; else if (x_i > max_x) max_x = x_i; double y_i = v_get_val(y, i); if (y_i < min_y) min_y = y_i; else if (y_i > max_y) max_y = y_i; } if (min_x < 0.0 || max_x > (double) fw - 1.0 || min_y < 0 || max_y > (double) fh - 1.0) break; // Compute the length of the snake double L = 0.0; for (i = 0; i < Np - 1; i++) { double diff_x = v_get_val(x, i + 1) - v_get_val(x, i); double diff_y = v_get_val(y, i + 1) - v_get_val(y, i); L += sqrt((diff_x * diff_x) + (diff_y * diff_y)); } double diff_x = v_get_val(x, 0) - v_get_val(x, Np - 1); double diff_y = v_get_val(y, 0) - v_get_val(y, Np - 1); L += sqrt((diff_x * diff_x) + (diff_y * diff_y)); // Compute the potential surface at each snaxel MAT *vf = linear_interp2(f, x, y); MAT *vfx = linear_interp2(fx, x, y); MAT *vfy = linear_interp2(fy, x, y); // Compute the average potential surface around the snake double vfmean = sum_m(vf ) / L; double vfxmean = sum_m(vfx) / L; double vfymean = sum_m(vfy) / L; // Compute the radial potential surface int m = vf->m, n = vf->n; MAT *vfr = m_get(m, n); for (i = 0; i < n; i++) { double vf_val = m_get_val(vf, 0, i); double vfx_val = m_get_val(vfx, 0, i); double vfy_val = m_get_val(vfy, 0, i); double x_val = v_get_val(x, i); double y_val = v_get_val(y, i); double new_val = (vf_val + vfx_val * (x_val - xc) + vfy_val * (y_val - yc) - vfmean) / L; m_set_val(vfr, 0, i, new_val); } // Update the snake center and snaxels xc = xc + (deltax * lambdaedge * vfxmean); yc = (yc + (deltay * lambdaedge * vfymean) + (deltay * lambdapath * Ey)) / (1.0 + deltay * lambdapath); double r_diff = 0.0; for (i = 0; i < Np; i++) { r[i] = (r[i] + (deltar * lambdaedge * m_get_val(vfr, 0, i)) + (deltar * lambdasize * Er)) / (1.0 + deltar * lambdasize); r_diff += fabs(r[i] - r_old[i]); } // Test for convergence snakediff = fabs(xc - xc_old) + fabs(yc - yc_old) + r_diff; // Free temporary matrices m_free(vf); m_free(vfx); m_free(vfy); m_free(vfr); iter++; } // Set the return values *xc0 = xc; *yc0 = yc; for (i = 0; i < Np; i++) r0[i] = r[i]; // Free memory free(r); free(r_old); v_free( x); v_free( y); m_free(fx); m_free(fy); }
void ellipsetrack(avi_t *video, double *xc0, double *yc0, int Nc, int R, int Np, int Nf) { /* % ELLIPSETRACK tracks cells in the movie specified by 'video', at % locations 'xc0'/'yc0' with radii R using an ellipse with Np discrete % points, starting at frame number one and stopping at frame number 'Nf'. % % INPUTS: % video.......pointer to avi video object % xc0,yc0.....initial center location (Nc entries) % Nc..........number of cells % R...........initial radius % Np..........nbr of snaxels points per snake % Nf..........nbr of frames in which to track % % Matlab code written by: DREW GILLIAM (based on code by GANG DONG / % NILANJAN RAY) % Ported to C by: MICHAEL BOYER */ int i, j; // Compute angle parameter double *t = (double *) malloc(sizeof(double) * Np); double increment = (2.0 * PI) / (double) Np; for (i = 0; i < Np; i++) { t[i] = increment * (double) i ; } // Allocate space for a snake for each cell in each frame double **xc = alloc_2d_double(Nc, Nf + 1); double **yc = alloc_2d_double(Nc, Nf + 1); double ***r = alloc_3d_double(Nc, Np, Nf + 1); double ***x = alloc_3d_double(Nc, Np, Nf + 1); double ***y = alloc_3d_double(Nc, Np, Nf + 1); // Save the first snake for each cell for (i = 0; i < Nc; i++) { xc[i][0] = xc0[i]; yc[i][0] = yc0[i]; for (j = 0; j < Np; j++) { r[i][j][0] = (double) R; } } // Generate ellipse points for each cell for (i = 0; i < Nc; i++) { for (j = 0; j < Np; j++) { x[i][j][0] = xc[i][0] + (r[i][j][0] * cos(t[j])); y[i][j][0] = yc[i][0] + (r[i][j][0] * sin(t[j])); } } // Keep track of the total time spent on computing // the MGVF matrix and evolving the snakes long long MGVF_time = 0; long long snake_time = 0; // Process each frame int frame_num, cell_num; for (frame_num = 1; frame_num <= Nf; frame_num++) { printf("\rProcessing frame %d / %d", frame_num, Nf); fflush(stdout); // Get the current video frame and its dimensions MAT *I = get_frame(video, frame_num, 0, 1); int Ih = I->m; int Iw = I->n; // Set the current positions equal to the previous positions for (i = 0; i < Nc; i++) { xc[i][frame_num] = xc[i][frame_num - 1]; yc[i][frame_num] = yc[i][frame_num - 1]; for (j = 0; j < Np; j++) { r[i][j][frame_num] = r[i][j][frame_num - 1]; } } // Split the work among multiple threads, if OPEN is defined #ifdef OPEN #pragma omp parallel for num_threads(omp_num_threads) private(i, j) #endif // Track each cell for (cell_num = 0; cell_num < Nc; cell_num++) { // Make copies of the current cell's location double xci = xc[cell_num][frame_num]; double yci = yc[cell_num][frame_num]; double *ri = (double *) malloc(sizeof(double) * Np); for (j = 0; j < Np; j++) { ri[j] = r[cell_num][j][frame_num]; } // Add up the last ten y-values for this cell // (or fewer if there are not yet ten previous frames) double ycavg = 0.0; for (i = (frame_num > 10 ? frame_num - 10 : 0); i < frame_num; i++) { ycavg += yc[cell_num][i]; } // Compute the average of the last ten y-values // (this represents the expected y-location of the cell) ycavg = ycavg / (double) (frame_num > 10 ? 10 : frame_num); // Determine the range of the subimage surrounding the current position int u1 = max(xci - 4.0 * R + 0.5, 0 ); int u2 = min(xci + 4.0 * R + 0.5, Iw - 1); int v1 = max(yci - 2.0 * R + 1.5, 0 ); int v2 = min(yci + 2.0 * R + 1.5, Ih - 1); // Extract the subimage MAT *Isub = m_get(v2 - v1 + 1, u2 - u1 + 1); for (i = v1; i <= v2; i++) { for (j = u1; j <= u2; j++) { m_set_val(Isub, i - v1, j - u1, m_get_val(I, i, j)); } } // Compute the subimage gradient magnitude MAT *Ix = gradient_x(Isub); MAT *Iy = gradient_y(Isub); MAT *IE = m_get(Isub->m, Isub->n); for (i = 0; i < Isub->m; i++) { for (j = 0; j < Isub->n; j++) { double temp_x = m_get_val(Ix, i, j); double temp_y = m_get_val(Iy, i, j); m_set_val(IE, i, j, sqrt((temp_x * temp_x) + (temp_y * temp_y))); } } // Compute the motion gradient vector flow (MGVF) edgemaps long long MGVF_start_time = get_time(); MAT *IMGVF = MGVF(IE, 1, 1); MGVF_time += get_time() - MGVF_start_time; // Determine the position of the cell in the subimage xci = xci - (double) u1; yci = yci - (double) (v1 - 1); ycavg = ycavg - (double) (v1 - 1); // Evolve the snake long long snake_start_time = get_time(); ellipseevolve(IMGVF, &xci, &yci, ri, t, Np, (double) R, ycavg); snake_time += get_time() - snake_start_time; // Compute the cell's new position in the full image xci = xci + u1; yci = yci + (v1 - 1); // Store the new location of the cell and the snake xc[cell_num][frame_num] = xci; yc[cell_num][frame_num] = yci; for (j = 0; j < Np; j++) { r[cell_num][j][frame_num] = ri[j]; x[cell_num][j][frame_num] = xc[cell_num][frame_num] + (ri[j] * cos(t[j])); y[cell_num][j][frame_num] = yc[cell_num][frame_num] + (ri[j] * sin(t[j])); } // Output the updated center of each cell //printf("%d,%f,%f\n", cell_num, xci[cell_num], yci[cell_num]); // Free temporary memory m_free(IMGVF); free(ri); } // Output a new line to visually distinguish the output from different frames //printf("\n"); } // Free temporary memory free(t); free_2d_double(xc); free_2d_double(yc); free_3d_double(r); free_3d_double(x); free_3d_double(y); // Report average processing time per frame printf("\n\nTracking runtime (average per frame):\n"); printf("------------------------------------\n"); printf("MGVF computation: %.5f seconds\n", ((float) (MGVF_time)) / (float) (1000*1000*Nf)); printf(" Snake evolution: %.5f seconds\n", ((float) (snake_time)) / (float) (1000*1000*Nf)); }
MAT *MGVF(MAT *I, double vx, double vy) { /* % MGVF calculate the motion gradient vector flow (MGVF) % for the image 'I' % % Based on the algorithm in: % Motion gradient vector flow: an external force for tracking rolling % leukocytes with shape and size constrained active contours % Ray, N. and Acton, S.T. % IEEE Transactions on Medical Imaging % Volume: 23, Issue: 12, December 2004 % Pages: 1466 - 1478 % % INPUTS % I...........image % vx,vy.......velocity vector % % OUTPUT % IMGVF.......MGVF vector field as image % % Matlab code written by: DREW GILLIAM (based on work by GANG DONG / % NILANJAN RAY) % Ported to C by: MICHAEL BOYER */ // Constants double converge = 0.00001; double mu = 0.5; double epsilon = 0.0000000001; double lambda = 8.0 * mu + 1.0; // Smallest positive value expressable in double-precision double eps = pow(2.0, -52.0); // Maximum number of iterations to compute the MGVF matrix int iterations = 500; // Find the maximum and minimum values in I int m = I->m, n = I->n, i, j; double Imax = m_get_val(I, 0, 0); double Imin = m_get_val(I, 0, 0); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { double temp = m_get_val(I, i, j); if (temp > Imax) Imax = temp; else if (temp < Imin) Imin = temp; } } // Normalize the image I double scale = 1.0 / (Imax - Imin + eps); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { double old_val = m_get_val(I, i, j); m_set_val(I, i, j, (old_val - Imin) * scale); } } // Initialize the output matrix IMGVF with values from I MAT *IMGVF = m_get(m, n); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { m_set_val(IMGVF, i, j, m_get_val(I, i, j)); } } // Precompute row and column indices for the // neighbor difference computation below int *rowU = (int *) malloc(sizeof(int) * m); int *rowD = (int *) malloc(sizeof(int) * m); int *colL = (int *) malloc(sizeof(int) * n); int *colR = (int *) malloc(sizeof(int) * n); rowU[0] = 0; rowD[m - 1] = m - 1; for (i = 1; i < m; i++) { rowU[i] = i - 1; rowD[i - 1] = i; } colL[0] = 0; colR[n - 1] = n - 1; for (j = 1; j < n; j++) { colL[j] = j - 1; colR[j - 1] = j; } // Allocate matrices used in the while loop below MAT *U = m_get(m, n), *D = m_get(m, n), *L = m_get(m, n), *R = m_get(m, n); MAT *UR = m_get(m, n), *DR = m_get(m, n), *UL = m_get(m, n), *DL = m_get(m, n); MAT *UHe = m_get(m, n), *DHe = m_get(m, n), *LHe = m_get(m, n), *RHe = m_get(m, n); MAT *URHe = m_get(m, n), *DRHe = m_get(m, n), *ULHe = m_get(m, n), *DLHe = m_get(m, n); // Precompute constants to avoid division in the for loops below double mu_over_lambda = mu / lambda; double one_over_lambda = 1.0 / lambda; // Compute the MGVF int iter = 0; double mean_diff = 1.0; while ((iter < iterations) && (mean_diff > converge)) { // Compute the difference between each pixel and its eight neighbors for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { double subtrahend = m_get_val(IMGVF, i, j); m_set_val(U, i, j, m_get_val(IMGVF, rowU[i], j) - subtrahend); m_set_val(D, i, j, m_get_val(IMGVF, rowD[i], j) - subtrahend); m_set_val(L, i, j, m_get_val(IMGVF, i, colL[j]) - subtrahend); m_set_val(R, i, j, m_get_val(IMGVF, i, colR[j]) - subtrahend); m_set_val(UR, i, j, m_get_val(IMGVF, rowU[i], colR[j]) - subtrahend); m_set_val(DR, i, j, m_get_val(IMGVF, rowD[i], colR[j]) - subtrahend); m_set_val(UL, i, j, m_get_val(IMGVF, rowU[i], colL[j]) - subtrahend); m_set_val(DL, i, j, m_get_val(IMGVF, rowD[i], colL[j]) - subtrahend); } } // Compute the regularized heaviside version of the matrices above heaviside( UHe, U, -vy, epsilon); heaviside( DHe, D, vy, epsilon); heaviside( LHe, L, -vx, epsilon); heaviside( RHe, R, vx, epsilon); heaviside(URHe, UR, vx - vy, epsilon); heaviside(DRHe, DR, vx + vy, epsilon); heaviside(ULHe, UL, -vx - vy, epsilon); heaviside(DLHe, DL, vy - vx, epsilon); // Update the IMGVF matrix double total_diff = 0.0; for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { // Store the old value so we can compute the difference later double old_val = m_get_val(IMGVF, i, j); // Compute IMGVF += (mu / lambda)(UHe .*U + DHe .*D + LHe .*L + RHe .*R + // URHe.*UR + DRHe.*DR + ULHe.*UL + DLHe.*DL); double vU = m_get_val(UHe, i, j) * m_get_val(U, i, j); double vD = m_get_val(DHe, i, j) * m_get_val(D, i, j); double vL = m_get_val(LHe, i, j) * m_get_val(L, i, j); double vR = m_get_val(RHe, i, j) * m_get_val(R, i, j); double vUR = m_get_val(URHe, i, j) * m_get_val(UR, i, j); double vDR = m_get_val(DRHe, i, j) * m_get_val(DR, i, j); double vUL = m_get_val(ULHe, i, j) * m_get_val(UL, i, j); double vDL = m_get_val(DLHe, i, j) * m_get_val(DL, i, j); double vHe = old_val + mu_over_lambda * (vU + vD + vL + vR + vUR + vDR + vUL + vDL); // Compute IMGVF -= (1 / lambda)(I .* (IMGVF - I)) double vI = m_get_val(I, i, j); double new_val = vHe - (one_over_lambda * vI * (vHe - vI)); m_set_val(IMGVF, i, j, new_val); // Keep track of the absolute value of the differences // between this iteration and the previous one total_diff += fabs(new_val - old_val); } } // Compute the mean absolute difference between this iteration // and the previous one to check for convergence mean_diff = total_diff / (double) (m * n); iter++; } // Free memory free(rowU); free(rowD); free(colL); free(colR); m_free(U); m_free(D); m_free(L); m_free(R); m_free(UR); m_free(DR); m_free(UL); m_free(DL); m_free(UHe); m_free(DHe); m_free(LHe); m_free(RHe); m_free(URHe); m_free(DRHe); m_free(ULHe); m_free(DLHe); return IMGVF; }
void ellipsetrack(avi_t *video, double *xc0, double *yc0, int Nc, int R, int Np, int Nf) { /* % ELLIPSETRACK tracks cells in the movie specified by 'video', at % locations 'xc0'/'yc0' with radii R using an ellipse with Np discrete % points, starting at frame number one and stopping at frame number 'Nf'. % % INPUTS: % video.......pointer to avi video object % xc0,yc0.....initial center location (Nc entries) % Nc..........number of cells % R...........initial radius % Np..........number of snaxels points per snake % Nf..........number of frames in which to track % % Matlab code written by: DREW GILLIAM (based on code by GANG DONG / % NILANJAN RAY) % Ported to C by: MICHAEL BOYER */ // Compute angle parameter double *t = (double *) malloc(sizeof(double) * Np); double increment = (2.0 * PI) / (double) Np; int i, j; for (i = 0; i < Np; i++) { t[i] = increment * (double) i ; } // Allocate space for a snake for each cell in each frame double **xc = alloc_2d_double(Nc, Nf + 1); double **yc = alloc_2d_double(Nc, Nf + 1); double ***r = alloc_3d_double(Nc, Np, Nf + 1); double ***x = alloc_3d_double(Nc, Np, Nf + 1); double ***y = alloc_3d_double(Nc, Np, Nf + 1); // Save the first snake for each cell for (i = 0; i < Nc; i++) { xc[i][0] = xc0[i]; yc[i][0] = yc0[i]; for (j = 0; j < Np; j++) { r[i][j][0] = (double) R; } } // Generate ellipse points for each cell for (i = 0; i < Nc; i++) { for (j = 0; j < Np; j++) { x[i][j][0] = xc[i][0] + (r[i][j][0] * cos(t[j])); y[i][j][0] = yc[i][0] + (r[i][j][0] * sin(t[j])); } } // Allocate arrays so we can break up the per-cell for loop below double *xci = (double *) malloc(sizeof(double) * Nc); double *yci = (double *) malloc(sizeof(double) * Nc); double **ri = alloc_2d_double(Nc, Np); double *ycavg = (double *) malloc(sizeof(double) * Nc); int *u1 = (int *) malloc(sizeof(int) * Nc); int *u2 = (int *) malloc(sizeof(int) * Nc); int *v1 = (int *) malloc(sizeof(int) * Nc); int *v2 = (int *) malloc(sizeof(int) * Nc); MAT **Isub = (MAT **) malloc(sizeof(MAT *) * Nc); MAT **Ix = (MAT **) malloc(sizeof(MAT *) * Nc); MAT **Iy = (MAT **) malloc(sizeof(MAT *) * Nc); MAT **IE = (MAT **) malloc(sizeof(MAT *) * Nc); // Keep track of the total time spent on computing // the MGVF matrix and evolving the snakes long long MGVF_time = 0; long long snake_time = 0; // Process each frame sequentially int frame_num; for (frame_num = 1; frame_num <= Nf; frame_num++) { printf("\rProcessing frame %d / %d", frame_num, Nf); fflush(stdout); // Get the current video frame and its dimensions MAT *I = get_frame(video, frame_num, 0, 1); int Ih = I->m; int Iw = I->n; // Initialize the current positions to be equal to the previous positions for (i = 0; i < Nc; i++) { xc[i][frame_num] = xc[i][frame_num - 1]; yc[i][frame_num] = yc[i][frame_num - 1]; for (j = 0; j < Np; j++) { r[i][j][frame_num] = r[i][j][frame_num - 1]; } } // Sequentially extract the subimage near each cell int cell_num; for (cell_num = 0; cell_num < Nc; cell_num++) { // Make copies of the current cell's location xci[cell_num] = xc[cell_num][frame_num]; yci[cell_num] = yc[cell_num][frame_num]; for (j = 0; j < Np; j++) { ri[cell_num][j] = r[cell_num][j][frame_num]; } // Add up the last ten y values for this cell // (or fewer if there are not yet ten previous frames) ycavg[cell_num] = 0.0; for (i = (frame_num > 10 ? frame_num - 10 : 0); i < frame_num; i++) { ycavg[cell_num] += yc[cell_num][i]; } // Compute the average of the last ten values // (this represents the expected location of the cell) ycavg[cell_num] = ycavg[cell_num] / (double) (frame_num > 10 ? 10 : frame_num); // Determine the range of the subimage surrounding the current position u1[cell_num] = max(xci[cell_num] - 4.0 * R + 0.5, 0 ); u2[cell_num] = min(xci[cell_num] + 4.0 * R + 0.5, Iw - 1); v1[cell_num] = max(yci[cell_num] - 2.0 * R + 1.5, 0 ); v2[cell_num] = min(yci[cell_num] + 2.0 * R + 1.5, Ih - 1); // Extract the subimage Isub[cell_num] = m_get(v2[cell_num] - v1[cell_num] + 1, u2[cell_num] - u1[cell_num] + 1); for (i = v1[cell_num]; i <= v2[cell_num]; i++) { for (j = u1[cell_num]; j <= u2[cell_num]; j++) { m_set_val(Isub[cell_num], i - v1[cell_num], j - u1[cell_num], m_get_val(I, i, j)); } } // Compute the subimage gradient magnitude Ix[cell_num] = gradient_x(Isub[cell_num]); Iy[cell_num] = gradient_y(Isub[cell_num]); IE[cell_num] = m_get(Isub[cell_num]->m, Isub[cell_num]->n); for (i = 0; i < Isub[cell_num]->m; i++) { for (j = 0; j < Isub[cell_num]->n; j++) { double temp_x = m_get_val(Ix[cell_num], i, j); double temp_y = m_get_val(Iy[cell_num], i, j); m_set_val(IE[cell_num], i, j, sqrt((temp_x * temp_x) + (temp_y * temp_y))); } } } // Compute the motion gradient vector flow (MGVF) edgemaps for all cells concurrently long long MGVF_start_time = get_time(); MAT **IMGVF = MGVF(IE, 1, 1, Nc); MGVF_time += get_time() - MGVF_start_time; // Sequentially determine the new location of each cell for (cell_num = 0; cell_num < Nc; cell_num++) { // Determine the position of the cell in the subimage xci[cell_num] = xci[cell_num] - (double) u1[cell_num]; yci[cell_num] = yci[cell_num] - (double) (v1[cell_num] - 1); ycavg[cell_num] = ycavg[cell_num] - (double) (v1[cell_num] - 1); // Evolve the snake long long snake_start_time = get_time(); ellipseevolve(IMGVF[cell_num], &(xci[cell_num]), &(yci[cell_num]), ri[cell_num], t, Np, (double) R, ycavg[cell_num]); snake_time += get_time() - snake_start_time; // Compute the cell's new position in the full image xci[cell_num] = xci[cell_num] + u1[cell_num]; yci[cell_num] = yci[cell_num] + (v1[cell_num] - 1); // Store the new location of the cell and the snake xc[cell_num][frame_num] = xci[cell_num]; yc[cell_num][frame_num] = yci[cell_num]; for (j = 0; j < Np; j++) { r[cell_num][j][frame_num] = 0; r[cell_num][j][frame_num] = ri[cell_num][j]; x[cell_num][j][frame_num] = xc[cell_num][frame_num] + (ri[cell_num][j] * cos(t[j])); y[cell_num][j][frame_num] = yc[cell_num][frame_num] + (ri[cell_num][j] * sin(t[j])); } // Output the updated center of each cell // printf("\n%d,%f,%f", cell_num, xci[cell_num], yci[cell_num]); // Free temporary memory m_free(Isub[cell_num]); m_free(Ix[cell_num]); m_free(Iy[cell_num]); m_free(IE[cell_num]); m_free(IMGVF[cell_num]); } #ifdef OUTPUT if (frame_num == Nf) { FILE * pFile; pFile = fopen ("result.txt","w+"); for (cell_num = 0; cell_num < Nc; cell_num++) fprintf(pFile,"\n%d,%f,%f", cell_num, xci[cell_num], yci[cell_num]); fclose (pFile); } #endif free(IMGVF); // Output a new line to visually distinguish the output from different frames //printf("\n"); } // Free temporary memory free_2d_double(xc); free_2d_double(yc); free_3d_double(r); free_3d_double(x); free_3d_double(y); free(t); free(xci); free(yci); free_2d_double(ri); free(ycavg); free(u1); free(u2); free(v1); free(v2); free(Isub); free(Ix); free(Iy); free(IE); // Report average processing time per frame printf("\n\nTracking runtime (average per frame):\n"); printf("------------------------------------\n"); printf("MGVF computation: %.5f seconds\n", ((float) (MGVF_time)) / (float) (1000*1000*Nf)); printf(" Snake evolution: %.5f seconds\n", ((float) (snake_time)) / (float) (1000*1000*Nf)); printf("CAUTION: cpu_offset: %d time: %lf mseconds\n", cpu_offset, ((float) (MGVF_time)) / (float) (1000*1000*Nf)*1000); }