void Segment_Channel(Array *input, Segmentation *seg) { double mean, sdev; int threshc, threshe, sizemin; Array *labels; Histogram *hist = Histogram_Array(input,0x100,VALU(1),VALU(0)); mean = Histogram_Mean(hist); sdev = Histogram_Sigma(hist); threshc = mean + Get_Double_Arg("-c")*sdev; threshe = mean + Get_Double_Arg("-e")*sdev; sizemin = Get_Int_Arg("-s"); #ifdef PROGRESS printf("\nChannel Segmentation:\n"); printf(" Mean = %.2f Std.Dev = %.2f\n",mean,sdev); printf(" Thresh-c = %d Thresh-e = %d Size-s = %d\n",threshc,threshe,sizemin); #ifdef DEBUG Print_Histogram(hist,stdout,4,BIN_COUNT|CUMULATIVE_COUNT|CLIP_HGRAM,0); #endif fflush(stdout); #endif Free_Histogram(hist); labels = Make_Array(PLAIN_KIND,UINT8_TYPE,3,input->dims); Array_Op_Scalar(labels,SET_OP,UVAL,VALU(0)); SEG_threshc = threshc; SEG_threshe = threshe; SEG_sizemin = sizemin; SEG_values = AUINT16(input); SEG_labels = AUINT8(labels); SEG_count = 0; SEG_coretouch = 0; SEG_id = 0; // Mark connected-components of pixels >= threshc that have not less than sizemin pixels Flood_All(input,0,ISCON2N,NULL,InCore,NULL,CountCore,NULL,GoodCore,NULL,MarkAsIn); // Mark all connected components of pixels >= threshe that contain a good core as above Flood_All(input,0,ISCON2N,NULL,InExtend,NULL,TouchCore,NULL,GoodExtend,NULL,SetLabel); // Capture each labeled region in "labels" with a Region { int i, nsegs; Indx_Type p; uint8 *val; Region **segs; seg->label = labels; seg->nsegs = nsegs = SEG_id; seg->segs = segs = (Region **) Guarded_Malloc(sizeof(Region *)*nsegs,Program_Name()); seg->mean = mean; seg->ethresh = threshe; seg->cthresh = threshc; for (i = 0; i < nsegs; i++) segs[i] = NULL; val = AUINT8(labels); for (p = 0; p < labels->size; p++) { i = val[p]; if (i > 0 && segs[i-1] == NULL) segs[i-1] = Record_Basic(labels,0,ISCON2N,p,1,EQ_COMP,VALU(i)); } } }
double ZArgumentProcessor::getDoubleArg(const char *arg, int index) { return Get_Double_Arg(const_cast<char*>(arg), index); }
int main(int argc, char *argv[]) { FILE *output; Process_Arguments(argc,argv,Spec,0); #ifdef PROGRESS printf("\nParameters: c=%g e=%g s=%d\n", Get_Double_Arg("-c"),Get_Double_Arg("-e"),Get_Int_Arg("-s")); printf("SubFolder: %s\n",Get_String_Arg("folder")); printf("CoreName: %s\n",Get_String_Arg("core")); fflush(stdout); #endif RezFolder = strdup(Get_String_Arg("folder")); if (RezFolder[strlen(RezFolder)-1] == '/') RezFolder[strlen(RezFolder)-1] = '\0'; if (mkdir(RezFolder,S_IRWXU|S_IRWXG|S_IRWXO)) { if (errno != EEXIST) { fprintf(stderr,"Error trying to create directory %s: %s\n",RezFolder,strerror(errno)); exit (1); } } CoreName = strdup(Get_String_Arg("core")); sprintf(NameBuf,"%s.neu",CoreName); output = fopen(NameBuf,"w"); fprintf(output,"NEUSEP: Version 0.9\n"); { Histogram *hist; int curchan; int maxchans; int i, n; n = Get_Repeat_Count("inputs"); fwrite(&n,sizeof(int),1,output); hist = Make_Histogram(UVAL,0x10000,VALU(1),VALU(0)); maxchans = 0; for (i = 0; i < n; i++) { curchan = NumChans; maxchans = Read_All_Channels(Get_String_Arg("inputs",i),maxchans); int channelsInCurrentFile=NumChans-curchan; { Size_Type sum, max; Indx_Type p; int j, wch; uint16 *val; max = -1; for (j = curchan; j < NumChans; j++) { val = AUINT16(Images[j]); sum = 0; for (p = 0; p < Images[j]->size; p++) sum += val[p]; if (sum > max) { max = sum; wch = j; } } fprintf(output,"%s\n",Get_String_Arg("inputs",i)); j = wch-curchan; fwrite(&j,sizeof(int),1,output); #ifdef PROGRESS printf("\n Eliminating channel %d from %s\n",j+1,Get_String_Arg("inputs",i)); fflush(stdout); #endif { // Section to write out the reference channel printf("\n Considering reference channel output, channelsInCurrentFile=%d\n", channelsInCurrentFile); fflush(stdout); if (channelsInCurrentFile>2) { // should work with both lsm pair with channels=3, or raw file with channels=4 sprintf(NameBuf,"%s/Reference.tif",RezFolder,CoreName,i); Write_Image(NameBuf,Images[wch],LZW_PRESS); } } Free_Array(Images[wch]); NumChans -= 1; for (j = wch; j < NumChans; j++) Images[j] = Images[j+1]; } { int j, ceil; Indx_Type p; uint16 *val; for (j = curchan; j < NumChans; j++) { Histagain_Array(hist,Images[j],0); ceil = Percentile2Bin(hist,1e-5); if (ceil==0) { fprintf(stderr, "Channel must have non-zero values for this program to function\n"); exit(1); } #ifdef PROGRESS printf(" Clipping channel %d at ceil = %d\n",j,ceil); fflush(stdout); fflush(stdout); #endif val = AUINT16(Images[j]); for (p = 0; p < Images[j]->size; p++) { if (val[p] > ceil) val[p] = ceil; val[p] = (val[p]*4095)/ceil; } // Convert_Array_Inplace(Images[j],PLAIN_KIND,UINT8_TYPE,8,0); } } } Free_Histogram(hist); printf("Starting ConsolidatedSignal.tif section\n"); fflush(stdout); // NA addition: write tif with re-scaled intensities to serve as basis for mask file { Array *signalStack; signalStack = Make_Array(RGB_KIND,UINT8_TYPE,3,Images[0]->dims); uint8 *sp=AUINT8(signalStack); int m; Indx_Type signalIndex; signalIndex=0; for (m=0;m<NumChans;m++) { sprintf(NameBuf, "%s/Signal_%d.tif", RezFolder, m); printf("Writing 16-bit channel file %s...", NameBuf); Write_Image(NameBuf, Images[m], LZW_PRESS); printf("done\n"); uint16 *ip=AUINT16(Images[m]); Indx_Type channelIndex; for (channelIndex=0;channelIndex<Images[m]->size;channelIndex++) { int value=ip[channelIndex]/16; if (value>255) { value=255; } sp[signalIndex++]=value; // convert 12-bit to 8-bit } } sprintf(NameBuf,"%s/ConsolidatedSignal.tif", RezFolder); printf("Writing 8-bit consolidated signal file %s...", NameBuf); Write_Image(NameBuf,signalStack,LZW_PRESS); printf("done"); //Free_Array(signalStack); - this is causing a bug } printf("Finished ConsolidatedSignal.tif section\n"); fflush(stdout); } { int i; Segmentation *segs; Overlaps *ovl; Clusters *clust; int numneur; Region **neurons; segs = (Segmentation *) Guarded_Malloc(sizeof(Segmentation)*NumChans,Program_Name()); for (i = 0; i < NumChans; i++) { Segment_Channel(Images[i],segs+i); if (i == 0) segs[i].base = 0; else segs[i].base = segs[i-1].base + segs[i-1].nsegs; printf("channel=%d segmentBase=%d\n", i, segs[i].base); } ovl = Find_Overlaps(segs); clust = Merge_Segments(segs,ovl); neurons = Segment_Clusters(segs,ovl,clust,&numneur); if (Is_Arg_Matched("-gp")) Output_Clusters(segs,ovl,clust); if (Is_Arg_Matched("-nr")) Output_Neurons(numneur,neurons,1); // Added for NA Output_Consolidated_Mask(numneur,neurons,1); fwrite(&numneur,sizeof(int),1,output); for (i = 0; i < numneur; i++) Write_Region(neurons[i],output); #ifdef PROGRESS printf("\nProduced %d neurons/fragments in %s.neu\n",numneur,CoreName); fflush(stdout); #endif printf("DEBUG: starting cleanup\n"); fflush(stdout); for (i = 0; i < numneur; i++) { printf("DEBUG: calling Kill_Region on neuron=%d\n", i); fflush(stdout); Kill_Region(neurons[i]); } printf("DEBUG: calling Kill_Clusters\n"); fflush(stdout); Kill_Clusters(clust); printf("DEBUG: calling Kill_Overlaps\n"); fflush(stdout); //Kill_Overlaps(ovl); - causing a bug printf("DEBUG: starting Kill_Segmentation loop\n"); fflush(stdout); for (i = 0; i < NumChans; i++) { printf("DEBUG: Kill_Segmentation on index=%d\n", i); fflush(stdout); Kill_Segmentation(segs+i); } printf("DEBUG: calling free() on segs\n"); fflush(stdout); free(segs); } printf("DEBUG: starting filestream cleanup\n"); fflush(stdout); { int i; fclose(output); free(CoreName); free(RezFolder); for (i = 0; i < NumChans; i++) Kill_Array(Images[i]); free(Images); } #ifdef VERBOSE printf("\nDid I free all arrays?:\n"); Print_Inuse_List(stdout,4); #endif exit (0); }
int main(int argc, char *argv[]) { if (Show_Version(argc, argv, "1.0") == 1) { return 0; } static char *Spec[] = { "[-R<string> -T<string> -M<string>] -D<string> [-minlen <double>]", "[-root <double> <double> <double>] [-trans <double> <double> <double>]", "[-rtlist <string>] [-sup_root] [-dist <double>]", "[-C<string>] [-I<string>] [-z <double>] -o <string> [-b] [-res <string>]", "[-screen] [-sp] [-intp] [-sl] [-rb] [-rz] [-rs] [-ct] [-al <double>]", "[-screenz <double>] [-force_merge <double>] [-ct_break <double>]", "[-jumpz <double>] [-single_break]", NULL}; Print_Arguments(argc, argv); Process_Arguments(argc, argv, Spec, 1); char *dir = Get_String_Arg("-D"); Stack_Document *stack_doc = NULL; if (Is_Arg_Matched("-I")) { if (!fexist(Get_String_Arg("-I"))) { PRINT_EXCEPTION("File does not exist", ""); fprintf(stderr, "%s cannot be found.\n", Get_String_Arg("-I")); return 1; } if (fhasext(Get_String_Arg("-I"), "xml")) { stack_doc = Xml_Read_Stack_Document(Get_String_Arg("-I")); } } /* Get number of chains */ int chain_number2 = dir_fnum_p(dir, "^chain.*\\.tb"); if (chain_number2 == 0) { printf("No tube found.\n"); printf("Quit reconstruction.\n"); return 1; } int i; int *chain_map = iarray_malloc(chain_number2); int chain_number; Locseg_Chain **chain_array = Dir_Locseg_Chain_Nd(dir, "chain.*\\.tb", &chain_number, chain_map); if (Is_Arg_Matched("-screenz")) { Locseg_Chain_Array_Screen_Z(chain_array, chain_number, Get_Double_Arg("-screenz")); } if (Is_Arg_Matched("-single_break")) { int i; for (i = 0; i < chain_number; i++) { if (Locseg_Chain_Length(chain_array[i]) == 1) { /* break the segment into two parts */ Locseg_Chain_Break_Node(chain_array[i], 0, 0.5); } } } if (Is_Arg_Matched("-ct_break")) { int tmp_chain_number; Locseg_Chain **tmp_chain_array = Locseg_Chain_Array_Break_Jump(chain_array, chain_number, Get_Double_Arg("-ct_break"), &tmp_chain_number); kill_locseg_chain_array(chain_array, chain_number); chain_array = tmp_chain_array; chain_number = tmp_chain_number; } Connection_Test_Workspace *ctw = New_Connection_Test_Workspace(); if (Is_Arg_Matched("-res")) { FILE *fp = fopen(Get_String_Arg("-res"), "r"); if (fp != NULL) { if (darray_fscanf(fp, ctw->resolution, 3) != 3) { fprintf(stderr, "Failed to load %s\n", Get_String_Arg("-res")); ctw->resolution[0] = 1.0; ctw->resolution[1] = 1.0; ctw->resolution[2] = 1.0; } else { ctw->unit = 'u'; } fclose(fp); } else { fprintf(stderr, "Failed to load %s. The file may not exist.\n", Get_String_Arg("-res")); } } else if (stack_doc != NULL) { ctw->resolution[0] = stack_doc->resolution[0]; ctw->resolution[1] = stack_doc->resolution[1]; ctw->resolution[2] = stack_doc->resolution[2]; } if (Is_Arg_Matched("-force_merge")) { Connection_Test_Workspace *ws = New_Connection_Test_Workspace(); ws->dist_thre = Get_Double_Arg("-force_merge"); ws->interpolate = FALSE; ws->resolution[2] = ctw->resolution[2] / ctw->resolution[0]; for (i = 0; i < chain_number; i++) { //Locseg_Chain_Correct_Ends(chain_array[i]); } Locseg_Chain_Array_Force_Merge(chain_array, chain_number, ws); Kill_Connection_Test_Workspace(ws); } chain_number2 = 0; Neuron_Component *chain_array2; GUARDED_MALLOC_ARRAY(chain_array2, chain_number, Neuron_Component); for (i = 0; i < chain_number; i++) { if (Locseg_Chain_Is_Empty(chain_array[i]) == FALSE) { chain_map[chain_number2] = chain_map[i]; Set_Neuron_Component(chain_array2+(chain_number2++), NEUROCOMP_TYPE_LOCSEG_CHAIN, chain_array[i]); } else { printf("chain_%d is empty.\n", chain_map[i]); } } /* Dir_Locseg_Chain_Nc(dir, "^chain.*\\.tb", &chain_number2, chain_map); */ Stack *signal = NULL; //Stack *canvas = NULL; if (Is_Arg_Matched("-I")) { signal = Read_Stack_U(Get_String_Arg("-I")); //canvas = Translate_Stack(signal, COLOR, 0); } else { if (Is_Arg_Matched("-screen")) { perror("The -screen option requires -I option to be supplied.\n"); return 1; } } /* Minimal tube length. */ double minlen = 25.0; if (Is_Arg_Matched("-minlen")) { minlen = Get_Double_Arg("-minlen"); } chain_number = 0; //int i; if (signal != NULL) { ctw->mask = Make_Stack(GREY, signal->width, signal->height, signal->depth); One_Stack(ctw->mask); } FILE *result_file = fopen(full_path(dir, Get_String_Arg("-o")), "w"); double z_scale = 1.0; if (Is_Arg_Matched("-z")) { z_scale = Get_Double_Arg("-z"); } /* Array to store corrected chains */ Neuron_Component *chain_array_c = Make_Neuron_Component_Array(chain_number2); int screen = 0; double average_intensity = 0.0; if (Is_Arg_Matched("-screen")) { int good_chain_number = 0; int bad_chain_number = 0; for (i = 0; i < chain_number2; i++) { Locseg_Chain *chain = NEUROCOMP_LOCSEG_CHAIN(chain_array2 + i); average_intensity += Locseg_Chain_Average_Score(chain, signal, z_scale, STACK_FIT_MEAN_SIGNAL); if ((Locseg_Chain_Geolen(chain) > 55) || (Locseg_Chain_Average_Score(chain, signal, z_scale, STACK_FIT_CORRCOEF) > 0.6)) { good_chain_number++; } else { bad_chain_number++; } } printf("good %d bad %d\n", good_chain_number, bad_chain_number); if (good_chain_number + bad_chain_number > 50) { if (bad_chain_number > good_chain_number) { screen = 1; } } else { screen = 3; /* if (bad_chain_number > good_chain_number * 2) { screen = 2; } */ } } average_intensity /= chain_number2; /* build chain map */ for (i = 0; i < chain_number2; i++) { Locseg_Chain *chain = NEUROCOMP_LOCSEG_CHAIN(chain_array2 + i); BOOL good = FALSE; switch (screen) { case 1: case 2: if ((Locseg_Chain_Geolen(chain) > 100) || (Locseg_Chain_Average_Score(chain, signal, z_scale, STACK_FIT_CORRCOEF) > 0.6)) { good = TRUE; } else { if (Locseg_Chain_Geolen(chain) < 100) { if ((Locseg_Chain_Average_Score(chain, signal, z_scale, STACK_FIT_CORRCOEF) > 0.5) || (Locseg_Chain_Average_Score(chain, signal, z_scale, STACK_FIT_MEAN_SIGNAL) > average_intensity)) { good = TRUE; } } } break; case 3: if ((Locseg_Chain_Average_Score(chain, signal, z_scale, STACK_FIT_CORRCOEF) > 0.50) || (Locseg_Chain_Average_Score(chain, signal, z_scale, STACK_FIT_MEAN_SIGNAL) > average_intensity)) { good = TRUE; } break; default: good = TRUE; } if (good == TRUE) { if (Locseg_Chain_Geolen(chain) < minlen) { good = FALSE; } } if (good == TRUE) { Locseg_Chain *tmpchain = chain; if (signal != NULL) { //Locseg_Chain_Trace_Np(signal, 1.0, tmpchain, tw); Locseg_Chain_Erase(chain, ctw->mask, 1.0); } fprintf(result_file, "%d %d\n", chain_number, chain_map[i]); chain_map[chain_number] = chain_map[i]; if (z_scale != 1.0) { Locseg_Chain_Scale_Z(chain, z_scale); } Set_Neuron_Component(chain_array_c + chain_number, NEUROCOMP_TYPE_LOCSEG_CHAIN, tmpchain); chain_number++; } else { #ifdef _DEBUG_ printf("chain%d is excluded.\n", i); /* char tmpfile[500]; sprintf(tmpfile, "../data/diadem_c1/bad_chain/chain%d.tb", i); Write_Locseg_Chain(tmpfile, chain); */ #endif } } z_scale = 1.0; fprintf(result_file, "#\n"); //Int_Arraylist *hit_spots = Int_Arraylist_New(0, chain_number); /* reconstruct neuron */ if (Is_Arg_Matched("-res")) { FILE *fp = fopen(Get_String_Arg("-res"), "r"); if (fp != NULL) { if (darray_fscanf(fp, ctw->resolution, 3) != 3) { fprintf(stderr, "Failed to load %s\n", Get_String_Arg("-res")); ctw->resolution[0] = 1.0; ctw->resolution[1] = 1.0; ctw->resolution[2] = 1.0; } else { ctw->unit = 'u'; } fclose(fp); } else { fprintf(stderr, "Failed to load %s. The file may not exist.\n", Get_String_Arg("-res")); } } else if (stack_doc != NULL) { ctw->resolution[0] = stack_doc->resolution[0]; ctw->resolution[1] = stack_doc->resolution[1]; ctw->resolution[2] = stack_doc->resolution[2]; } if (!Is_Arg_Matched("-sp")) { ctw->sp_test = FALSE; if (ctw->sp_test == FALSE) { ctw->dist_thre = NEUROSEG_DEFAULT_H / 2.0; } } else { ctw->dist_thre = NEUROSEG_DEFAULT_H * 1.5; } if (Is_Arg_Matched("-dist")) { ctw->dist_thre = Get_Double_Arg("-dist"); } if (!Is_Arg_Matched("-intp")) { ctw->interpolate = FALSE; } //ctw->dist_thre = 100.0; double *tube_offset = NULL; if (Is_Arg_Matched("-trans")) { tube_offset = darray_malloc(3); tube_offset[0] = Get_Double_Arg("-trans", 1); tube_offset[1] = Get_Double_Arg("-trans", 2); tube_offset[2] = Get_Double_Arg("-trans", 3); } else { if (stack_doc != NULL) { tube_offset = darray_malloc(3); tube_offset[0] = stack_doc->offset[0]; tube_offset[1] = stack_doc->offset[1]; tube_offset[2] = stack_doc->offset[2]; } } Neuron_Structure *ns = New_Neuron_Structure(); ns->comp = chain_array_c; ns->graph = New_Graph(); ns->graph->nvertex = chain_number; if (Is_Arg_Matched("-rtlist")) { int m, n; double *d = darray_load_matrix(Get_String_Arg("-rtlist"), NULL, &m, &n); if (n > 0) { coordinate_3d_t *roots = GUARDED_MALLOC_ARRAY(roots, n, coordinate_3d_t); int i; for (i = 0; i < n; i++) { if (Is_Arg_Matched("-trans")) { roots[i][0] = d[i*3] - tube_offset[0]; roots[i][1] = d[i*3 + 1] - tube_offset[1]; roots[i][2] = d[i*3 + 2] - tube_offset[2]; } else { roots[i][0] = d[i*3]; roots[i][1] = d[i*3 + 1]; roots[i][2] = d[i*3 + 2]; } } Neuron_Structure_Break_Root(ns, roots, n); Neuron_Structure_Load_Root(ns, roots, n); } } Locseg_Chain_Comp_Neurostruct_W(ns, signal, z_scale, ctw); if (tube_offset != NULL) { for (i = 0; i < chain_number; i++) { Locseg_Chain_Translate(NEUROCOMP_LOCSEG_CHAIN(chain_array_c + i), tube_offset); } } /* Neuron_Structure *ns = Locseg_Chain_Comp_Neurostruct(chain_array, chain_number, signal, z_scale, ctw); */ FILE *tube_fp = fopen(full_path(dir, "tube.swc"), "w"); int start_id = 1; for (i = 0; i < chain_number; i++) { int node_type = i % 10; int n = Locseg_Chain_Swc_Fprint_T(tube_fp, NEUROCOMP_LOCSEG_CHAIN(chain_array_c + i), node_type, start_id, -1, DL_FORWARD, 1.0, NULL); start_id += n; } fclose(tube_fp); //Neuron_Structure_To_Swc_File(ns, full_path(dir, "tube.swc")); /* Graph *testgraph = New_Graph(0, 0, FALSE); Int_Arraylist *cidx = Make_Int_Arraylist(0, 2); Int_Arraylist *sidx = Make_Int_Arraylist(0, 2); Locseg_Chain_Network_Simlify(&net, testgraph, cidx, sidx); */ /* Find branch points */ //Locseg_Chain *branches = Locseg_Chain_Network_Find_Branch(ns); //Graph *graph = Locseg_Chain_Graph(chain_array, chain_number, hit_spots); //Graph *graph = ns->graph; if (Is_Arg_Matched("-sup_root")) { if (Is_Arg_Matched("-rtlist")) { int m, n; double *d = darray_load_matrix(Get_String_Arg("-rtlist"), NULL, &m, &n); if (n > 0) { coordinate_3d_t *roots = GUARDED_MALLOC_ARRAY(roots, n, coordinate_3d_t); int i; for (i = 0; i < n; i++) { roots[i][0] = d[i*3]; roots[i][1] = d[i*3 + 1]; roots[i][2] = d[i*3 + 2]; /* if (tube_offset != NULL) { roots[i][0] += tube_offset[0]; roots[i][1] += tube_offset[1]; roots[i][2] += tube_offset[2]; } */ } neuron_structure_suppress(ns, roots, n); free(roots); } } } Process_Neuron_Structure(ns); Print_Neuron_Structure(ns); #ifdef _DEBUG_ for (i = 0; i < NEURON_STRUCTURE_LINK_NUMBER(ns); i++) { printf("chain_%d (%d) -- chain_%d (%d) ", chain_map[ns->graph->edges[i][0]], ns->graph->edges[i][0], chain_map[ns->graph->edges[i][1]], ns->graph->edges[i][1]); Print_Neurocomp_Conn(ns->conn + i); } #endif if (Is_Arg_Matched("-ct")) { Neuron_Structure_Crossover_Test(ns, ctw->resolution[0] / ctw->resolution[2]); } if (Is_Arg_Matched("-al")) { Neuron_Structure_Adjust_Link(ns, Get_Double_Arg("-al")); } Neuron_Structure_To_Tree(ns); Neuron_Structure_Remove_Negative_Conn(ns); #ifdef _DEBUG_ printf("\nTree:\n"); for (i = 0; i < NEURON_STRUCTURE_LINK_NUMBER(ns); i++) { printf("chain_%d (%d) -- chain_%d (%d) ", chain_map[ns->graph->edges[i][0]], ns->graph->edges[i][0], chain_map[ns->graph->edges[i][1]], ns->graph->edges[i][1]); Print_Neurocomp_Conn(ns->conn + i); } #endif /* printf("\ncross over changed: \n"); Print_Neuron_Structure(ns); */ #ifdef _DEBUG_2 ns->graph->nedge = 0; Neuron_Structure_To_Swc_File(ns, "../data/test.swc"); return 1; #endif //Print_Neuron_Structure(ns); Neuron_Structure* ns2= NULL; if (Is_Arg_Matched("-intp")) { ns2 = Neuron_Structure_Locseg_Chain_To_Circle_S(ns, 1.0, 1.0); } else { ns2 = Neuron_Structure_Locseg_Chain_To_Circle(ns); } /* Neuron_Structure* ns2= Neuron_Structure_Locseg_Chain_To_Circle_S(ns, 1.0, 1.0); */ Graph_To_Dot_File(ns2->graph, full_path(dir, "graph_d.dot")); //Neuron_Structure_Main_Graph(ns2); Neuron_Structure_To_Tree(ns2); double root[3]; if (Is_Arg_Matched("-root")) { root[0] = Get_Double_Arg("-root", 1); root[1] = Get_Double_Arg("-root", 2); root[2] = Get_Double_Arg("-root", 3); } Swc_Tree *tree = NULL; if (Is_Arg_Matched("-root")) { /* int root_index = Neuron_Structure_Find_Root_Circle(ns2, root); Graph_Workspace *gw2 = New_Graph_Workspace(); Graph_Clean_Root(ns2->graph, root_index, gw2); Neuron_Structure_To_Swc_File_Circle_Z(ns2, full_path(dir, "graph_d.swc"), z_scale, root); */ tree = Neuron_Structure_To_Swc_Tree_Circle_Z(ns2, z_scale, root); if (Swc_Tree_Node_Is_Virtual(tree->root) == TRUE) { tree->root->first_child->next_sibling = NULL; } Swc_Tree_Clean_Root(tree); } else { /* Neuron_Structure_To_Swc_File_Circle_Z(ns2, full_path(dir, "graph_d.swc"), z_scale, NULL); */ tree = Neuron_Structure_To_Swc_Tree_Circle_Z(ns2, z_scale, NULL); } ns->graph->nedge = 0; //Neuron_Structure_To_Swc_File(ns, full_path(dir, "tube.swc")); if (Is_Arg_Matched("-rb")) { //Swc_Tree_Tune_Branch(tree); Swc_Tree_Tune_Fork(tree); } if (Is_Arg_Matched("-sl")) { Swc_Tree_Leaf_Shrink(tree); } if (Is_Arg_Matched("-rz")) { Swc_Tree_Remove_Zigzag(tree); } if (Is_Arg_Matched("-rs")) { Swc_Tree_Remove_Spur(tree); } Swc_Tree_Resort_Id(tree); Write_Swc_Tree(full_path(dir, "graph_d.swc"), tree); if (Is_Arg_Matched("-rtlist")) { int m, n; double *d = darray_load_matrix(Get_String_Arg("-rtlist"), NULL, &m, &n); if (n > 0) { coordinate_3d_t *roots = GUARDED_MALLOC_ARRAY(roots, n, coordinate_3d_t); int i; for (i = 0; i < n; i++) { roots[i][0] = d[i*3]; roots[i][1] = d[i*3 + 1]; roots[i][2] = d[i*3 + 2]; /* if (tube_offset != NULL) { roots[i][0] += tube_offset[0]; roots[i][1] += tube_offset[1]; roots[i][2] += tube_offset[2]; } */ Swc_Tree *subtree = Swc_Tree_Pull_R(tree, roots[i]); char filename[MAX_PATH_LENGTH]; if (subtree->root != NULL) { //Swc_Tree_Clean_Root(subtree); Swc_Tree_Clean_Root(subtree); Swc_Tree_Node_Set_Pos(subtree->root, roots[i]); if (Is_Arg_Matched("-jumpz")) { //swc_tree_remove_zjump(subtree, Get_Double_Arg("-jumpz")); } Swc_Tree_Resort_Id(subtree); sprintf(filename, "graph%d.swc", i + 1); Write_Swc_Tree(full_path(dir, filename), subtree); } } } } printf("%d chains\n", chain_number); return 0; }
int main(int argc, char *argv[]) { static char *Spec[] = {"[-z <double>]", NULL}; Process_Arguments(argc, argv, Spec, 1); #if 0 Locseg_Chain *chain1 = Read_Locseg_Chain("../data/ct017/test5/chain2.bn"); Locseg_Chain *chain2 = Read_Locseg_Chain("../data/ct017/test5/chain1.bn"); if (Is_Arg_Matched("-z")) { double z_scale = Get_Double_Arg("-z"); Locseg_Chain_Scale_Z(chain1, z_scale); Locseg_Chain_Scale_Z(chain2, z_scale); } Neurocomp_Conn conn; Locseg_Chain_Connection_Test_P(chain1, chain2, &conn); Print_Neurocomp_Conn(&conn); double scale = 2.0; double offset = 5.0; Local_Neuroseg *locseg1; if (conn.info[0] == 0) { locseg1 = Locseg_Chain_Head_Seg(chain1); Local_Neuroseg_Stretch(locseg1, scale, offset, -1); } else { locseg1 = Locseg_Chain_Tail_Seg(chain1); Local_Neuroseg_Stretch(locseg1, scale, offset, 1); } Local_Neuroseg *locseg2 = Locseg_Chain_Peek_Seg_At(chain2, conn.info[1]); Local_Neuroseg_Stretch(locseg2, scale, offset, 0); printf("%g\n", Local_Neuroseg_Planar_Dist_L(locseg1, locseg2)); FILE *fp = fopen("../data/ct017/test5/test.swc", "w"); /* to avoid v3d bug */ Local_Neuroseg *tmp_locseg = Copy_Local_Neuroseg(locseg1); tmp_locseg->seg.r1 = 0.1; Local_Neuroseg_Swc_Fprint(fp, tmp_locseg, 0, -1); Local_Neuroseg_Swc_Fprint(fp, locseg1, 2, -1); Local_Neuroseg_Swc_Fprint(fp, locseg2, 4, -1); fclose(fp); Locseg_Chain_Connection_Test(chain1, chain2, NULL, 1.0, &conn, NULL); Print_Neurocomp_Conn(&conn); #endif #if 0 double z_scale = 0.5375; Locseg_Chain *chain[11]; char file_path[100]; int i, j; for (i = 0; i < 11; i++) { sprintf(file_path, "../data/mouse_single_org/chain%d.bn", i); chain[i] = Read_Locseg_Chain(file_path); Locseg_Chain_Scale_Z(chain[i], z_scale); } Neurocomp_Conn conn; for (i = 0; i < 11; i++) { for (j = 0; j < 11; j++) { if (i != j) { if (Locseg_Chain_Connection_Test_P(chain[i], chain[j], &conn) < 5.0) { printf("%d -> %d \n", i, j); Print_Neurocomp_Conn(&conn); } } } } #endif #if 0 double z_scale = 0.5375; int n = 278; Locseg_Chain *chain[n]; char file_path[100]; int i, j; for (i = 0; i < n; i++) { sprintf(file_path, "../data/mouse_single_org/chain%d.bn", i); chain[i] = Read_Locseg_Chain(file_path); Locseg_Chain_Scale_Z(chain[i], z_scale); } Locseg_Chain *hook = chain[19]; Neurocomp_Conn conn; for (i = 0; i < n; i++) { if (i != 19) { if (Locseg_Chain_Connection_Test_P(hook, chain[i], &conn) < 5.0) { printf("19 -> %d \n", i); Print_Neurocomp_Conn(&conn); } } } #endif #if 0 double z_scale = 0.32; Locseg_Chain *chain[17]; char file_path[100]; int i, j; for (i = 0; i < 17; i++) { sprintf(file_path, "../data/fly_neuron_n1/chain%d.bn", i); chain[i] = Read_Locseg_Chain(file_path); Locseg_Chain_Scale_Z(chain[i], z_scale); } Neurocomp_Conn conn; for (i = 0; i < 17; i++) { for (j = 0; j < 17; j++) { if (i != j) { if (Locseg_Chain_Connection_Test_P(chain[i], chain[j], &conn) < 5.0) { printf("%d -> %d \n", i, j); Print_Neurocomp_Conn(&conn); } } } } #endif #if 0 double z_scale = 0.1400; //double z_scale = 1.0; int n = 12; Locseg_Chain *chain[n]; char file_path[100]; int i, j; for (i = 0; i < n; i++) { sprintf(file_path, "../data/ct017/test6/chain%d.bn", i); chain[i] = Read_Locseg_Chain(file_path); Locseg_Chain_Scale_Z(chain[i], z_scale); } Neurocomp_Conn conn; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i != j) { if (Locseg_Chain_Connection_Test_P(chain[i], chain[j], &conn) < 10.0) { printf("%d -> %d \n", i, j); Print_Neurocomp_Conn(&conn); } } } } #endif #if 0 //double z_scale = 0.1400; double z_scale = 1.20; int n = 5; Locseg_Chain *chain[n]; char file_path[100]; int i, j; for (i = 0; i < n; i++) { sprintf(file_path, "../data/ct017/test7/chain%d.bn", i); chain[i] = Read_Locseg_Chain(file_path); Locseg_Chain_Scale_Z(chain[i], z_scale); } Neurocomp_Conn conn; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i != j) { if (Locseg_Chain_Connection_Test_P(chain[i], chain[j], &conn) < 10.0) { printf("%d -> %d \n", i, j); Print_Neurocomp_Conn(&conn); } } } } #endif #if 0 double z_scale = 1.20; //double z_scale = 1.0; Stack *stack = Read_Stack("../data/lobster_neuron_org.tif"); int n = 5; Locseg_Chain *chain[n]; char file_path[100]; int i, j; for (i = 0; i < n; i++) { sprintf(file_path, "../data/ct017/test7/chain%d.bn", i); chain[i] = Read_Locseg_Chain(file_path); //Locseg_Chain_Scale_Z(chain[i], z_scale); } Connection_Test_Workspace *ctw = New_Connection_Test_Workspace(); ctw->z_scale = z_scale; Neurocomp_Conn conn; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i != j) { if (Locseg_Chain_Connection_Test(chain[i], chain[j], stack, 1.0, &conn, ctw)) { printf("%d -> %d \n", i, j); Print_Neurocomp_Conn(&conn); } } } } #endif #if 0 Locseg_Chain *chain = Read_Locseg_Chain("../data/ct017/test6/chain7.bn"); Local_Neuroseg *locseg = Locseg_Chain_Tail_Seg(chain); Print_Local_Neuroseg(locseg); Stack *stack = Read_Stack("../data/mouse_neuron_sp2.tif"); double *profile = Local_Neuroseg_Height_Profile(locseg, stack, 1.0, 11, STACK_FIT_CORRCOEF, NULL); darray_write("../data/profile1.bn", profile, 11); #endif #if 0 Locseg_Chain *chain = Read_Locseg_Chain("../data/ct017/test6/chain7.bn"); Local_Neuroseg *locseg = Locseg_Chain_Tail_Seg(chain); Print_Local_Neuroseg(locseg); Stack *stack = Read_Stack("../data/mouse_neuron_sp2.tif"); int n; double record[11]; Locseg_Chain *chain2 = Local_Neuroseg_Push(locseg, stack, 1.0, STACK_FIT_OUTER_SIGNAL, record, &n); Print_Locseg_Chain(chain2); Write_Locseg_Chain("../data/test.tb", chain2); return 1; Locseg_Chain *chain3 = Read_Locseg_Chain("../data/ct017/test6/chain6.bn"); Locseg_Chain_Scale_Z(chain2, 0.14); Locseg_Chain_Scale_Z(chain3, 0.14); Neurocomp_Conn conn; Locseg_Chain_Connection_Test(chain2, chain3, NULL, 1.0, &conn, NULL); Print_Neurocomp_Conn(&conn); #endif #if 0 Locseg_Chain *chain = Read_Locseg_Chain("../data/ct017/test6/chain9.bn"); Local_Neuroseg *locseg = Locseg_Chain_Tail_Seg(chain); //Local_Neuroseg *locseg = Locseg_Chain_Head_Seg(chain); //Flip_Local_Neuroseg(locseg); Print_Local_Neuroseg(locseg); Stack *stack = Read_Stack("../data/mouse_neuron_sp2.tif"); int n; double record[11]; Locseg_Chain *chain2 = Local_Neuroseg_Push(locseg, stack, 1.0, STACK_FIT_OUTER_SIGNAL, record, &n); Print_Locseg_Chain(chain2); Write_Locseg_Chain("../data/test.bn", chain2); return 1; Locseg_Chain *chain3 = Read_Locseg_Chain("../data/ct017/test6/chain9.bn"); Locseg_Chain_Scale_Z(chain2, 0.14); Locseg_Chain_Scale_Z(chain3, 0.14); Neurocomp_Conn conn; Locseg_Chain_Connection_Test(chain3, chain2, stack, 1.0, &conn, NULL); Print_Neurocomp_Conn(&conn); #endif #if 0 double z_scale = 0.1400; //double z_scale = 1.0; Stack *stack = Read_Stack("../data/mouse_neuron_sp2.tif"); int n = 12; Locseg_Chain *chain[n]; char file_path[100]; int i, j; for (i = 0; i < n; i++) { sprintf(file_path, "../data/ct017/test6/chain%d.bn", i); chain[i] = Read_Locseg_Chain(file_path); //Locseg_Chain_Scale_Z(chain[i], z_scale); } Connection_Test_Workspace *ctw = New_Connection_Test_Workspace(); ctw->z_scale = 0.14; Neurocomp_Conn conn; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i != j) { if (Locseg_Chain_Connection_Test(chain[i], chain[j], stack, 1.0, &conn, ctw)) { printf("%d -> %d \n", i, j); Print_Neurocomp_Conn(&conn); } } } } #endif #if 0 //double z_scale = 0.32; //double z_scale = 1.0; Stack *stack = Read_Stack("../data/fly_neuron_n1.tif"); int n = 17; Locseg_Chain *chain[n]; char file_path[100]; int i, j; for (i = 0; i < n; i++) { sprintf(file_path, "../data/fly_neuron_n1/chain%d.tb", i); chain[i] = Read_Locseg_Chain(file_path); //Locseg_Chain_Scale_Z(chain[i], z_scale); } Connection_Test_Workspace *ctw = New_Connection_Test_Workspace(); ctw->resolution[0] = 0.32; ctw->resolution[1] = 0.32; ctw->resolution[2] = 1.0; Neurocomp_Conn conn; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i != j) { if (Locseg_Chain_Connection_Test(chain[i], chain[j], stack, 1.0, &conn, ctw)) { printf("%d -> %d \n", i, j); Print_Neurocomp_Conn(&conn); } } } } #endif #if 0 Locseg_Chain *chain = Read_Locseg_Chain("../data/fly_neuron_n1/chain3.bn"); Local_Neuroseg *locseg = Locseg_Chain_Tail_Seg(chain); //Flip_Local_Neuroseg(locseg); Print_Local_Neuroseg(locseg); Stack *stack = Read_Stack("../data/fly_neuron_n1.tif"); int n; double record[11]; Locseg_Chain *chain2 = Local_Neuroseg_Push(locseg, stack, 1.0, STACK_FIT_CORRCOEF, record, &n); Print_Locseg_Chain(chain2); Write_Locseg_Chain("../data/test.bn", chain2); return 1; Locseg_Chain *chain3 = Read_Locseg_Chain("../data/fly_neuron_n1/chain0.bn"); Locseg_Chain_Scale_Z(chain2, 0.32); Locseg_Chain_Scale_Z(chain3, 0.32); Neurocomp_Conn conn; Locseg_Chain_Connection_Test(chain2, chain3, stack, 1.0, &conn, NULL); Print_Neurocomp_Conn(&conn); #endif return 0; }
int main(int argc, char* argv[]) { if (Show_Version(argc, argv, "1.00") == 1) { return 0; } static char *Spec[] = { " <image:string> -s <string> -o <string> [-e <string>] [-fo <int>] " "[-z <double> | -res <string>] [-field <int>] [-min_score <double>]", NULL}; Process_Arguments(argc, argv, Spec, 1); Geo3d_Scalar_Field *seed = Read_Geo3d_Scalar_Field(Get_String_Arg("-s")); size_t idx; double max_r = darray_max(seed->values, seed->size, &idx); max_r *= 1.5; //Set_Neuroseg_Max_Radius(max_r); Stack *signal = Read_Stack_U(Get_String_Arg("image")); dim_type dim[3]; dim[0] = signal->width; dim[1] = signal->height; dim[2] = signal->depth; Rgb_Color color; Set_Color(&color, 255, 0, 0); int seed_offset = -1; double z_scale = 1.0; if (Is_Arg_Matched("-res")) { if (fexist(Get_String_Arg("-res"))) { double res[3]; int length; darray_read2(Get_String_Arg("-res"), res, &length); if (res[0] != res[1]) { perror("Different X-Y resolutions."); TZ_ERROR(ERROR_DATA_VALUE); } z_scale = res[0] / res[2] * 2.0; } } if (Is_Arg_Matched("-z")) { z_scale = Get_Double_Arg("-z"); } printf("z scale: %g\n", z_scale); tic(); double *values = darray_malloc(seed->size); int i; Local_Neuroseg *locseg = (Local_Neuroseg *) malloc(seed->size * sizeof(Local_Neuroseg)); int index = 0; //int ncol = LOCAL_NEUROSEG_NPARAM + 1 + 23; //double *features = darray_malloc(seed->size * ncol); //double *tmpfeats = features; Stack *seed_mask = Make_Stack(GREY, signal->width, signal->height, signal->depth); Zero_Stack(seed_mask); Locseg_Fit_Workspace *fws = New_Locseg_Fit_Workspace(); if (Is_Arg_Matched("-field")) { fws->sws->field_func = Neuroseg_Slice_Field_Func(Get_Int_Arg("-field")); } fws->sws->fs.n = 2; fws->sws->fs.options[0] = STACK_FIT_DOT; fws->sws->fs.options[1] = STACK_FIT_CORRCOEF; if (Is_Arg_Matched("-fo")) { fws->sws->fs.options[1] = Get_Int_Arg("-fo"); } for (i = 0; i < seed->size; i++) { printf("-----------------------------> seed: %d / %d\n", i, seed->size); index = i; int x = (int) seed->points[index][0]; int y = (int) seed->points[index][1]; int z = (int) seed->points[index][2]; double width = seed->values[index]; seed_offset = Stack_Util_Offset(x, y, z, signal->width, signal->height, signal->depth); if (width < 3.0) { width += 0.5; } Set_Neuroseg(&(locseg[i].seg), width, 0.0, NEUROSEG_DEFAULT_H, 0.0, 0.0, 0.0, 0.0, 1.0); double cpos[3]; cpos[0] = x; cpos[1] = y; cpos[2] = z; cpos[2] /= z_scale; Set_Neuroseg_Position(&(locseg[i]), cpos, NEUROSEG_CENTER); if (seed_mask->array[seed_offset] > 0) { printf("labeled\n"); values[i] = 0.0; continue; } //Local_Neuroseg_Optimize(locseg + i, signal, z_scale, 0); Local_Neuroseg_Optimize_W(locseg + i, signal, z_scale, 0, fws); values[i] = fws->sws->fs.scores[1]; /* Stack_Fit_Score fs; fs.n = 1; fs.options[0] = 1; values[i] = Local_Neuroseg_Score(locseg + i, signal, z_scale, &fs); */ //values[i] = Local_Neuroseg_Score_W(locseg + i, signal, z_scale, sws); printf("%g\n", values[i]); double min_score = LOCAL_NEUROSEG_MIN_CORRCOEF; if (Is_Arg_Matched("-min_score")) { min_score = Get_Double_Arg("-min_score"); } if (values[i] > min_score) { Local_Neuroseg_Label_G(locseg + i, seed_mask, -1, 2, z_scale); } else { Local_Neuroseg_Label_G(locseg + i, seed_mask, -1, 1, z_scale); } /* tmpfeats += Local_Neuroseg_Param_Array(locseg + i, z_scale, tmpfeats); tmpfeats += Local_Neuroseg_Stack_Feature(locseg + i, signal, z_scale, tmpfeats); */ } if (Is_Arg_Matched("-e")) { Write_Stack(Get_String_Arg("-e"), seed_mask); } Write_Local_Neuroseg_Array(Get_String_Arg("-o"), locseg, seed->size); char file_path[MAX_PATH_LENGTH]; sprintf(file_path, "%s_score", Get_String_Arg("-o")); darray_write(file_path, values, seed->size); //sprintf(file_path, "%s_feat", Get_String_Arg("-o")); //darray_write(file_path, features, seed->size * ncol); Kill_Geo3d_Scalar_Field(seed); printf("Time passed: %lld\n", toc()); return 0; }
int main(int argc, char* argv[]) { int n_rows, count; Measurements *table,*cursor; double thresh, px2mm, low_px, high_px; int face_x, face_y; int follicle_thresh = 0, follicle_col = 4, follicle_high; int n_cursor; Process_Arguments( argc, argv, Spec, 0); if( Is_Arg_Matched("-h") | Is_Arg_Matched("--help") ) { Print_Argument_Usage(stdout,0); printf("-------------------------- \n" " Classify 4 (radius filter) \n" "--------------------------- \n" " \n" " Uses a length threshold to seperate hair/microvibrissae from main whiskers. \n" " Then, for frames where the expected number of whiskers are found, \n" " label the whiskers according to their order on the face. \n" "\n" " This version of classify filters out curves where the follicle side falls \n" " outside of a circle centered at the face position with the radius specified \n" " by the --follicle option." "\n" " <source> Filename with Measurements table.\n" " <dest> Filename to which labelled Measurements will be saved.\n" " This can be the same as <source>.\n" " <faceX> <faceY> <faceAxis>\n" " These are used for determining the order of whisker segments along \n" " the face. This requires an approximate position for the center of \n" " the face and can be specified in pixel coordinates with <x> and <y>.\n" " <axis> indicates the orientaiton of the face. Values for <axis> may\n" " be 'x' or 'h' for horizontal. 'y' or 'v' indicate a vertical face. \n" " If the face is located along the edge of the frame then specify \n" " that edge with 'left', 'right', 'top' or 'bottom'. \n" " --px2mm <double>\n" " The length of a pixel in millimeters. This is used to determine \n" " appropriate thresholds for discriminating hairs from whiskers. \n" " -n <int> (Optional) Optimize the threshold to find this number of whiskers. \n" " If this isn't specified, or if this is set to a number less than 1 \n" " then the number of whiskers is automatically determined. \n" " --follicle <int>\n" " Only count follicles that lie inside a circle with this radius in \n" " (in pixels) and centered at the face position as whiskers. \n" "-- \n"); return 0; } px2mm = Get_Double_Arg("--px2mm"); low_px = Get_Double_Arg("--limit",1) / px2mm; high_px = Get_Double_Arg("--limit",2) / px2mm; #ifdef DEBUG_CLASSIFY_4 debug("mm/px %f\n" " low %f\n" " high %f\n", px2mm, low_px, high_px ); #endif table = Measurements_Table_From_Filename ( Get_String_Arg("source"), NULL, &n_rows ); if(!table) error("Couldn't read %s\n",Get_String_Arg("source")); Sort_Measurements_Table_Time(table,n_rows); { int maxx,maxy; const char *axis = Get_String_Arg("faceAxis"); static const int x = 4, y = 5; Measurements_Table_Pixel_Support( table, n_rows, &maxx, &maxy ); face_x = Get_Int_Arg("faceX"); face_y = Get_Int_Arg("faceY"); follicle_thresh = 0; // set defaults if( Is_Arg_Matched("--follicle") && Get_Int_Arg("--follicle")>0 ) { follicle_thresh = Get_Int_Arg("--follicle"); switch( axis[0] ) // respond to <follicle> option { case 'x': // follicle must be between threshold and face case 'h': case 'y': case 'v': break; default: error("Could not recognize <axis>. Must be 'x','h','y', or 'v'. Got %s\n",axis); } } } // Follicle location threshold if( Is_Arg_Matched("--follicle") && Get_Int_Arg("--follicle")>0 ) follicle_thresh = Get_Int_Arg("--follicle"); //inline void Measurements_Table_Label_By_RadialThreshold( Measurements *table, int n_rows, double thresh, int ox, int oy, int colx, int coly) Measurements_Table_Label_By_RadialThreshold( table, n_rows, follicle_thresh, face_x, face_y, follicle_col, follicle_col+1); #ifdef DEBUG_CLASSIFY_4 debug(" Face Position: ( %3d, %3d )\n", face_x, face_y); #endif // Shuffle to select subset with good follicles Sort_Measurements_Table_State_Time( table, n_rows ); { cursor = table; while( (cursor->state == 0) && (cursor < table+n_rows ) ) cursor++; n_cursor = n_rows - (cursor-table); } Sort_Measurements_Table_Time(cursor,n_cursor); //resort selected by time #ifdef DEBUG_CLASSIFY_1 { Measurements *row = cursor + n_cursor; //Assert all state==1 while(row-- > cursor) assert(row->state == 1); } #endif // // Estimate best length threshold and apply // if( Is_Arg_Matched("-n") && ( (count = Get_Int_Arg("-n"))>=1 ) ) { thresh = Measurements_Table_Estimate_Best_Threshold_For_Known_Count( cursor, //table, n_cursor, //n_rows, 0 /*length column*/, low_px, high_px, 1, /*use > */ count ); } else { thresh = Measurements_Table_Estimate_Best_Threshold( cursor, //table, n_cursor, //n_rows, 0 /*length column*/, low_px, high_px, 1, /* use > */ &count ); } /* Measurements_Table_Label_By_Threshold ( cursor, n_cursor, follicle_col, follicle_thresh, is_gt); */ Measurements_Table_Label_By_RadialThreshold( table, n_rows, follicle_thresh, face_x, face_y, follicle_col, follicle_col+1); #ifdef DEBUG_CLASSIFY_4 { Measurements *row = cursor + n_cursor; //Assert all state==1 while(row-- > cursor) assert(row->state == 1); } #endif Measurements_Table_Label_By_Threshold_And ( cursor, n_cursor, 0 /*length column*/, thresh, 1 /*use gt*/); #ifdef DEBUG_CLASSIFY_4 debug(" Length threshold: %f\n" " Target count: %d\n", thresh,count); #endif Measurements_Table_Set_Constant_Face_Position ( table, n_rows, face_x, face_y); Measurements_Table_Set_Follicle_Position_Indices ( table, n_rows, 4, 5 ); Measurements_Table_Label_By_Order(table, n_rows, count ); //re-sorts Measurements_Table_To_Filename( Get_String_Arg("dest"), NULL, table, n_rows ); Free_Measurements_Table(table); return 0; }
/* * trace_neuron - trace neuron from given seeds * * trace_neuron [!wtr] seed_file -Dsave_dir * -r: write intermediate results * */ int main(int argc, char* argv[]) { static char *Spec[] = { "[!wtr] [-canvas <string>] [-mask <string>] [-res <string>] [-minr <int>]", "-minlen <double>", " <image:string> -S<string> -D<string>", NULL}; Process_Arguments(argc, argv, Spec, 1); char *dir = Get_String_Arg("-D"); char file_path[100]; sprintf(file_path, "%s/%s", dir, Get_String_Arg("-S")); printf("%s\n", file_path); Geo3d_Scalar_Field *seed = Read_Geo3d_Scalar_Field(file_path); int idx; sprintf(file_path, "%s/%s.bn", dir, "max_r"); double max_r; int tmp; if (fexist(file_path)) { darray_read2(file_path, &max_r, &tmp); } else { max_r = darray_max(seed->values, seed->size, &idx); } printf("%g\n", max_r); max_r *= 1.5; /* sprintf(file_path, "%s/%s", dir, "soma0.bn"); if (!fexist(file_path)) { max_r *= 2.0; } */ Set_Neuroseg_Max_Radius(max_r); Stack *signal = Read_Stack(Get_String_Arg("image")); dim_type dim[3]; dim[0] = signal->width; dim[1] = signal->height; dim[2] = signal->depth; /* IMatrix *chord = Make_IMatrix(dim, 3); Stack *code = Make_Stack(GREY16, signal->width, signal->height, signal->depth); */ Rgb_Color color; Set_Color(&color, 255, 0, 0); Stack *canvas = NULL; char trace_file_path[100]; sprintf(trace_file_path, "%s/%s", dir, Get_String_Arg("-canvas")); if (fexist(trace_file_path) == 1) { canvas = Read_Stack((char *) trace_file_path); } else { canvas = Copy_Stack(signal); Stretch_Stack_Value_Q(canvas, 0.999); Translate_Stack(canvas, COLOR, 1); } Stack *traced = NULL; char trace_mask_path[100]; sprintf(trace_mask_path, "%s/%s", dir, Get_String_Arg("-mask")); if (fexist(trace_mask_path) == 1) { traced = Read_Stack((char *) trace_mask_path); } else { traced = Make_Stack(GREY, signal->width, signal->height, signal->depth); One_Stack(traced); } //Object_3d *obj = NULL; int seed_offset = -1; Neurochain *chain = NULL; double z_scale = 1.0; if (Is_Arg_Matched("-res")) { sprintf(file_path, "%s", Get_String_Arg("-res")); if (fexist(file_path)) { double res[3]; int length; darray_read2(file_path, res, &length); if (res[0] != res[1]) { perror("Different X-Y resolutions."); TZ_ERROR(ERROR_DATA_VALUE); } z_scale = res[0] / res[2]; } } //sprintf(file_path, "%s/%s", dir, Get_String_Arg("-M")); //Stack *stack = Read_Stack(file_path); tic(); FILE *fp = NULL; char chain_file_path[100]; char vrml_file_path[100]; double min_chain_length = 25.0; if (Is_Arg_Matched("-minlen")) { min_chain_length = Get_Double_Arg("-minlen"); } int *indices = iarray_malloc(seed->size); double *values = darray_malloc(seed->size); int i; Local_Neuroseg *locseg = (Local_Neuroseg *) malloc(seed->size * sizeof(Local_Neuroseg)); int index = 0; for (i = 0; i < seed->size; i++) { printf("-----------------------------> seed: %d / %d\n", i, seed->size); indices[i] = i; index = i; int x = (int) seed->points[index][0]; int y = (int) seed->points[index][1]; int z = (int) seed->points[index][2]; double width = seed->values[index]; chain = New_Neurochain(); seed_offset = Stack_Util_Offset(x, y, z, signal->width, signal->height, signal->depth); if (width < 3.0) { width += 0.5; } Set_Neuroseg(&(locseg[i].seg), width, width, 12.0, 0.0, 0.0, 0.0); double cpos[3]; cpos[0] = x; cpos[1] = y; cpos[2] = z; cpos[2] *= z_scale; Set_Neuroseg_Position(&(locseg[i]), cpos, NEUROSEG_CENTER); Stack_Fit_Score fs; fs.n = 1; fs.options[0] = 1; values[i] = Local_Neuroseg_Orientation_Search_C(&(locseg[i]), signal, z_scale, &fs); } darray_qsort(values, indices, seed->size); /* for (i = 0; i < seed->size; i++) { indices[i] = i; } darraycpy(values, seed->values, 0, seed->size); darray_qsort(values, indices, seed->size); */ int counter = 0; // for (i = seed->size - 1; i >= seed->size - 231; i--) { for (i = seed->size - 1; i >= 0; i--) { index = indices[i]; printf("-----------------------------> seed: %d / %d\n", i, seed->size); sprintf(chain_file_path, "%s/chain%d.bn", dir, index); sprintf(vrml_file_path, "%s/chain%d.wrl", dir, index); if (fexist(chain_file_path) == 1) { chain = Read_Neurochain(chain_file_path); if (Neurochain_Geolen(chain) >= min_chain_length) { Write_Neurochain_Vrml(vrml_file_path, chain); Neurochain_Label(canvas, chain, z_scale); Neurochain_Erase_E(traced, chain, z_scale, 0, Neurochain_Length(chain, FORWARD), 1.5, 0.0); } Free_Neurochain(chain); printf("chain exists\n"); continue; } int x = (int) seed->points[index][0]; int y = (int) seed->points[index][1]; int z = (int) seed->points[index][2]; if (*STACK_PIXEL_8(traced, x, y, z, 0) == 0) { printf("traced \n"); continue; } double width = seed->values[index]; if (width > max_r) { printf("too thick\n"); continue; } if (Is_Arg_Matched("-minr")) { int max_level = (int) (width + 0.5); if (max_level <= Get_Int_Arg("-minr")) { printf("too thin\n"); continue; } } /* seed_offset = Stack_Util_Offset(x, y, z, signal->width, signal->height, signal->depth); */ chain = New_Neurochain(); /* Stack_Level_Code_Constraint(stack, code, chord->array, &seed_offset, 1, max_level + 1); Voxel_t v; v[0] = x; v[1] = y; v[2] = z; Stack *tmp_stack = Copy_Stack(stack); obj = Stack_Grow_Object_Constraint(tmp_stack, 1, v, chord, code, max_level); Free_Stack(tmp_stack); Print_Object_3d_Info(obj); double vec[3]; Object_3d_Orientation_Zscale(obj, vec, MAJOR_AXIS, z_scale); double theta, psi; Geo3d_Vector obj_vec; Set_Geo3d_Vector(&obj_vec, vec[0], vec[1], vec[2]); Geo3d_Vector_Orientation(&obj_vec, &theta, &psi); */ /* if (width < 3.0) { width += 0.5; } Set_Neuroseg(&(chain->locseg.seg), width, width, 12.0, 0.0, 0.0, 0.0); double cpos[3]; cpos[0] = x; cpos[1] = y; cpos[2] = z; cpos[2] *= z_scale; //Set_Neuroseg_Position(&(chain->locseg), cpos, NEUROSEG_BOTTOM); Set_Neuroseg_Position(&(chain->locseg), cpos, NEUROSEG_CENTER); Stack_Fit_Score fs; fs.n = 1; fs.options[0] = 1; Local_Neuroseg_Orientation_Search_C(&(chain->locseg), signal, z_scale, &fs); //fs.options[0] = 1; */ Copy_Local_Neuroseg(&(chain->locseg), &(locseg[index])); Neurochain *chain_head = chain; if (Initialize_Tracing(signal, chain, NULL, z_scale) >= MIN_SCORE) { if ((Neuroseg_Hit_Traced(&(chain->locseg), traced, z_scale) == FALSE) && (chain->locseg.seg.r1 < max_r) && (chain->locseg.seg.r2 < max_r)) { //Initialize_Tracing(signal, chain, NULL, z_scale); chain = Trace_Neuron2(signal, chain, BOTH, traced, z_scale, 500); //Neurochain *chain_head = Neurochain_Head(chain); chain_head = Neurochain_Remove_Overlap_Segs(chain); chain_head = Neurochain_Remove_Turn_Ends(chain_head, 0.5); /* if (i == seed->size - 231) { Print_Neurochain(chain_head); } */ fp = fopen(chain_file_path, "w"); Neurochain_Fwrite(chain_head, fp); fclose(fp); if (Neurochain_Geolen(chain_head) >= min_chain_length) { Write_Neurochain_Vrml(vrml_file_path, chain_head); Neurochain_Erase_E(traced, chain_head, z_scale, 0, Neurochain_Length(chain_head, FORWARD), 1.5, 0.0); Neurochain_Label(canvas, chain_head, z_scale); counter += Neurochain_Length(chain_head, FORWARD); if (counter > 500) { if (Is_Arg_Matched("-r")) { Write_Stack((char *) trace_mask_path, traced); } if (Is_Arg_Matched("-r")) { Write_Stack((char *) trace_file_path, canvas); } counter = 0; } } } } Free_Neurochain(chain_head); //Kill_Object_3d(obj); } Write_Stack((char *) trace_file_path, canvas); if (Is_Arg_Matched("-r")) { Write_Stack((char *) trace_mask_path, traced); } Kill_Geo3d_Scalar_Field(seed); printf("Time passed: %lld\n", toc()); return 0; }