예제 #1
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
  int option;

  check_argin(nrhs, prhs, &option);
  
  Array_Link *a = MxArray_To_Stack(prhs[0]);
  mwSize dims[3];
  dims[0] = Get_Stack_At(a)->width;
  dims[1] = Get_Stack_At(a)->height;
  dims[2] = Get_Stack_At(a)->depth;

  plhs[0] = mxCreateNumericArray(3, dims, mxUINT8_CLASS, mxREAL);
  Array_Link *aout = MxArray_To_Stack(plhs[0]);

  Stack_Local_Max(Get_Stack_At(a), Get_Stack_At(aout), option);

#if 0
  Print_Stack_Value(Get_Stack_At(a));
  Print_Stack_Value(Get_Stack_At(aout));
#endif

  Delete_Array_Link(a);
  Delete_Array_Link(aout);
}
예제 #2
0
int main(int argc, char *argv[])
{
  static char *Spec[] = {"[-t]", NULL};
  Process_Arguments(argc, argv, Spec, 1);

  if (Is_Arg_Matched("-t")) {
    /* Example test */
    Stack *stack = Make_Stack(GREY, 7, 7, 1);
    One_Stack(stack);
    Stack_Watershed_Workspace *ws = Make_Stack_Watershed_Workspace(stack);
    ws->conn = 26;
    ws->mask = Make_Stack(GREY, Stack_Width(stack), Stack_Height(stack),
			  Stack_Depth(stack));
    Zero_Stack(ws->mask);

    /* set seeds */
    Set_Stack_Pixel(ws->mask, 1, 1, 0, 0, 1.0);
    Set_Stack_Pixel(ws->mask, 1, 5, 0, 0, 2.0);
    Set_Stack_Pixel(ws->mask, 3, 3, 0, 0, 3.0);
    Set_Stack_Pixel(ws->mask, 5, 1, 0, 0, 4.0);
    Set_Stack_Pixel(ws->mask, 5, 5, 0, 0, 5.0);

    /* set stack values */
    Set_Stack_Pixel(stack, 1, 1, 0, 0, 3.0);
    Set_Stack_Pixel(stack, 1, 5, 0, 0, 3.0);
    Set_Stack_Pixel(stack, 3, 3, 0, 0, 3.0);
    Set_Stack_Pixel(stack, 5, 1, 0, 0, 3.0);
    Set_Stack_Pixel(stack, 5, 5, 0, 0, 3.0);
    
    Stack *out = Stack_Watershed(stack, ws);

    //Write_Stack("../data/test/watershed/golden/watershed1.tif", out);

    char *golden_file = "../data/test/watershed/golden/watershed1.tif";
    if (fexist(golden_file)) {
      Stack *golden = Read_Stack(golden_file);
      if (Stack_Identical(out, golden) == FALSE) {
	Print_Stack_Value(stack);
	Print_Stack_Value(out);
	Print_Stack_Value(golden);
	PRINT_EXCEPTION("Bug?", "Conflict with golden.");
	return 1;
      }

      Kill_Stack(stack);
      Kill_Stack(out);
      Kill_Stack(golden);
      Kill_Stack_Watershed_Workspace(ws);
    } else {
      printf("%s cannot be found.\n", golden_file);
    }

    char *test_file = "../data/benchmark/rice_label.tif";
    if (fexist(test_file)) {
      stack = Read_Stack_U(test_file);

      ws = Make_Stack_Watershed_Workspace(stack);
      ws->mask = Copy_Stack(stack);
      ws->conn = 26;
      One_Stack(stack);

      out = Stack_Watershed(stack, ws);

      //Write_Stack("../data/test/watershed/golden/watershed2.tif", out);

      Stack *golden = Read_Stack("../data/test/watershed/golden/watershed2.tif");
      if (Stack_Identical(out, golden) == FALSE) {
	PRINT_EXCEPTION("Bug?", "Conflict with golden.");
	return 1;
      }
    } else {
      printf("%s cannot be found.\n", test_file);
    }

    printf(":) Testing passed.\n");

    return 0;
  }

#if 0
  /* Initialize */
  Watershed_3d *watershed = New_Watershed_3d();
  Watershed_3d_Workspace *ws = New_Watershed_3d_Workspace();
  ws->conn = 26;

  /* Initialize stack */
  Stack *stack = Read_Stack("../data/fly_neuron/dist.tif");
  int nvoxel = Stack_Voxel_Number(stack);
  int i;
  uint16 *array16 = (uint16 *)stack->array;
  for (i = 0; i < nvoxel; i++) {
    array16[i] = 0xFFFF - array16[i];
  }
  
  /* Add mask to ignore dark voxels  */
  ws->mask = Copy_Stack(stack); 
  

  //Translate_Stack(stack, GREY, 1);

  //stack->array = stack->array + stack->width * stack->height * 100;
  //stack->depth = 1;
  // ws->mask = Copy_Stack(stack);
  //Stack_Binarize(ws->mask);
  // ws->mask = NULL;

  //Write_Stack("../data/dist.tif", stack);


  //Stack *stack2 = Copy_Stack(stack);


#  if 0
  Build_3D_Watershed(stack, watershed, ws);
  Write_Stack("../data/test.tif", watershed->labels);
#  endif

#  if 0
  
  Image_View iv = Image_View_Stack(stack2);
  Watershed_Test *watershed2 = Build_2D_Watershed_Test(&(iv.image), 0);
  Write_Image("../data/test2.tif", watershed2->labels);
#  endif
#endif

#if 0
  Image *image = Read_Image("../data/Ring15.tif_Sub1200_Original_inv.tif");
  Watershed_2D *ws = Build_2D_Watershed(image, 0);
  Image *result = Color_Watersheds(ws, image);
  Write_Image("../data/test.tif", result);
  Kill_Image(image);
#endif

#if 0
  Stack *stack = Make_Stack(GREY, 7, 7, 1);
  One_Stack(stack);
  Stack_Watershed_Workspace *ws = Make_Stack_Watershed_Workspace(stack);
  ws->conn = 26;
  ws->mask = Make_Stack(GREY, Stack_Width(stack), Stack_Height(stack),
			Stack_Depth(stack));
  Zero_Stack(ws->mask);
  Set_Stack_Pixel(ws->mask, 1, 1, 0, 0, 1.0);
  Set_Stack_Pixel(ws->mask, 1, 5, 0, 0, 2.0);
  Set_Stack_Pixel(ws->mask, 3, 3, 0, 0, 3.0);
  Set_Stack_Pixel(ws->mask, 5, 1, 0, 0, 4.0);
  Set_Stack_Pixel(ws->mask, 5, 5, 0, 0, 5.0);

  Set_Stack_Pixel(stack, 1, 1, 0, 0, 3.0);
  Set_Stack_Pixel(stack, 1, 5, 0, 0, 3.0);
  Set_Stack_Pixel(stack, 3, 3, 0, 0, 3.0);
  Set_Stack_Pixel(stack, 5, 1, 0, 0, 3.0);
  Set_Stack_Pixel(stack, 5, 5, 0, 0, 3.0);
  
  Stack *out = Stack_Watershed(stack, ws);

  Print_Stack_Value(out);
#endif

#if 0
  Stack *stack = Make_Stack(GREY, 3, 3, 1);
  One_Stack(stack);
  Stack_Watershed_Workspace *ws = Make_Stack_Watershed_Workspace(stack);
  ws->conn = 26;
  ws->mask = Make_Stack(GREY, Stack_Width(stack), Stack_Height(stack),
			Stack_Depth(stack));
  Zero_Stack(ws->mask);

  Set_Stack_Pixel(ws->mask, 0, 0, 0, 0, 1.0);
  Set_Stack_Pixel(ws->mask, 2, 2, 0, 0, 2.0);

  Set_Stack_Pixel(stack, 0, 0, 0, 0, 5.0);
  Set_Stack_Pixel(stack, 2, 2, 0, 0, 3.0);
  Set_Stack_Pixel(stack, 0, 2, 0, 0, 2.0);
  //Set_Stack_Pixel(stack, 1, 1, 0, 0, 2.0);
  Set_Stack_Pixel(stack, 1, 2, 0, 0, 2.0);
  Set_Stack_Pixel(stack, 2, 0, 0, 0, 2.0);
  Set_Stack_Pixel(stack, 2, 1, 0, 0, 2.0);

  Print_Stack_Value(stack);

  ws->start_level = 6;

  Stack *out = Stack_Watershed(stack, ws);

  Print_Stack_Value(out);
#endif
 
#if 0
  Stack *stack = Read_Stack_U("../data/benchmark/rice_label.tif");

  Stack_Watershed_Workspace *ws = Make_Stack_Watershed_Workspace(stack);
  ws->mask = Copy_Stack(stack);
  ws->conn = 26;
  One_Stack(stack);

  tic();
  Stack *out = Stack_Watershed(stack, ws);
  printf("%lld\n", toc());

  Write_Stack("../data/test.tif", out); 
#endif

#if 0
  Stack *stack = Read_Stack_U("../data/diadem_d1_147.xml");
  Stack_Watershed_Workspace *ws = Make_Stack_Watershed_Workspace(stack);
  ws->mask = Make_Stack(GREY, Stack_Width(stack), Stack_Height(stack),
			Stack_Depth(stack));
  Zero_Stack(ws->mask);
  ws->conn = 26;

  const int *dx = Stack_Neighbor_X_Offset(ws->conn);
  const int *dy = Stack_Neighbor_X_Offset(ws->conn);
  const int *dz = Stack_Neighbor_X_Offset(ws->conn);
  
  int seed[3];
  String_Workspace *sw = New_String_Workspace();
  char *line = NULL;
  FILE *fp = fopen("../data/diadem_d1_root_z.txt", "r");
  int k = 1;
    
  while ((line = Read_Line(fp, sw)) != NULL) {
    int n;
    String_To_Integer_Array(line, seed, &n);
    double maxv = -1;
    if (n == 3) {
      maxv = Get_Stack_Pixel(stack, seed[0], seed[1], seed[2], 0);
      printf("%g\n", maxv);
      int i;
      for (i = 0; i < ws->conn; i++) {
	if (maxv < Get_Stack_Pixel(stack, seed[0] + dx[i], seed[1] + dy[i], 
				   seed[2] + dz[i], 0)) {
	  maxv = Get_Stack_Pixel(stack, seed[0] + dx[i], seed[1] + dy[i], 
				 seed[2] + dz[i], 0);
	}
      }

      //Set_Stack_Pixel(stack, seed[0], seed[1], seed[2], 0, maxv);
      Set_Stack_Pixel(ws->mask, seed[0], seed[1], seed[2], 0, k);
      for (i = 0; i < ws->conn; i++) {
	//Set_Stack_Pixel(stack, seed[0] + dx[i], seed[1] + dy[i], 
	//		seed[2] + dz[i], 0, maxv);
	Set_Stack_Pixel(ws->mask, seed[0] + dx[i], seed[1] + dy[i], 
			seed[2] + dz[i], 0, k);	
      }
      k++;
    }
  }
  fclose(fp);
  Kill_String_Workspace(sw);
  
  /*
  Set_Stack_Pixel(ws->mask, 19, 605, 112, 0, 1.0);
  Set_Stack_Pixel(ws->mask, 28, 565, 112, 0, 1.0);
  */
  Stack_Watershed_Infer_Parameter(stack, ws);

  tic();
  Stack *out = Stack_Watershed(stack, ws);
  printf("%lld\n", toc());

  Write_Stack("../data/diadem_d1_147_label.tif", out);

  static const uint8 Color_Map[][3] = { 
    {0, 0, 0},
    {0, 224, 64}, {32, 64, 128}, {64, 64, 0}, {64, 128, 64},
    {96, 64, 128}, {64, 0, 0}, {128, 200, 64}, {160, 128, 128},
    {192, 0, 0}, {192, 160, 64}, {224, 64, 128}, {224, 224, 192}};
  
  Translate_Stack(out, COLOR, 1);
  size_t nvoxel = Stack_Voxel_Number(out);
  size_t i;
  color_t *arrayc = (color_t*) out->array;
  for (i = 0; i < nvoxel; i++) {
    arrayc[i][2] = Color_Map[arrayc[i][0]][2];
    arrayc[i][1] = Color_Map[arrayc[i][0]][1];
    arrayc[i][0] = Color_Map[arrayc[i][0]][0];
  }

  Write_Stack("../data/diadem_d1_147_paint.tif", out);
#endif

#if 0
  Stack *stack = Read_Stack_U("../data/diadem_d1_146.xml");
  Stack_Watershed_Workspace *ws = Make_Stack_Watershed_Workspace(stack);
  Stack *mask = Read_Stack("../data/test3.tif");
  ws->mask = Crop_Stack(mask, 252, -937, 2, 1024, 1024, 63, NULL);
  ws->conn = 26;

  Stack_Watershed_Infer_Parameter(stack, ws);

  tic();
  Stack *out = Stack_Watershed(stack, ws);
  printf("%lld\n", toc());
  
  Write_Stack("../data/test2.tif", out);
#endif

#if 0
  Stack *stack = Make_Stack(GREY, 7, 7, 1);
  One_Stack(stack);
  Stack_Watershed_Workspace *ws = Make_Stack_Watershed_Workspace(stack);
  ws->conn = 26;
  ws->mask = Make_Stack(GREY, Stack_Width(stack), Stack_Height(stack),
			Stack_Depth(stack));
  Zero_Stack(ws->mask);
  Set_Stack_Pixel(ws->mask, 1, 1, 0, 0, 1.0);
  Set_Stack_Pixel(ws->mask, 1, 5, 0, 0, 2.0);
  Set_Stack_Pixel(ws->mask, 3, 3, 0, 0, 3.0);
  Set_Stack_Pixel(ws->mask, 5, 1, 0, 0, 4.0);
  Set_Stack_Pixel(ws->mask, 5, 5, 0, 0, 5.0);

  Set_Stack_Pixel(stack, 1, 1, 0, 0, 3.0);
  Set_Stack_Pixel(stack, 1, 5, 0, 0, 3.0);
  Set_Stack_Pixel(stack, 3, 3, 0, 0, 3.0);
  Set_Stack_Pixel(stack, 5, 1, 0, 0, 3.0);
  Set_Stack_Pixel(stack, 5, 5, 0, 0, 3.0);
  
  Stack *out = Stack_Watershed(stack, ws);
  Print_Stack_Value(out);

  Stack *out2 = Stack_Region_Border_Shrink(out, ws);
  
  Print_Stack_Value(out2);

#endif

#if 0
  Stack *stack = Read_Stack("../data/diadem_d1_013_label.tif");
  Stack_Watershed_Workspace *ws = Make_Stack_Watershed_Workspace(stack);
  //ws->conn = 26;

  Stack *out = Stack_Region_Border_Shrink(stack, ws);
  Write_Stack("../data/test.tif", out);
#endif

#if 0
  Stack *stack = Make_Stack(GREY, 1, 1, 19);
  One_Stack(stack);
  
  stack->array[3] = 5;
  stack->array[4] = 5;
  stack->array[7] = 5;
  stack->array[8] = 5;
  stack->array[10] = 4;
  stack->array[11] = 5;
  stack->array[12] = 5;
  stack->array[13] = 4;
  stack->array[16] = 5;
  stack->array[17] = 5;

  Stack_Watershed_Workspace *ws = Make_Stack_Watershed_Workspace(stack);
  ws->conn = 26;
  ws->mask = Make_Stack(GREY, Stack_Width(stack), Stack_Height(stack),
			Stack_Depth(stack));
  Zero_Stack(ws->mask);

  
  Print_Stack_Value(stack);
  Stack_Watershed_Zgap_Barrier(stack, ws->mask);
  Print_Stack_Value(ws->mask);
#endif

#if 0
  Stack *stack = Read_Stack("../data/diadem_d1_047_label.tif");
  Stack_Binarize(stack);

  Stack *stack2 = Stack_Bwdist_L_U16(stack, NULL, 0);
  Write_Stack("../data/test.tif", stack2);  
#endif

#if 0
  char stack_path[100];
  char mask_path[100];

  strcpy(stack_path, "../data/diadem_d1_064.xml");

  strcpy(mask_path, stack_path);
  strsplit(mask_path, '.', -1);
  sprintf(mask_path, "%s_label.tif", mask_path);

  if (!fexist(stack_path)) {
    fprintf(stderr, "Cannot find %s\n", stack_path);
    return 1;
  }
  
  printf("Processing %s\n", stack_path);
  Stack *stack = Read_Stack_U(stack_path);
  Stack *mask = Make_Stack(GREY, Stack_Width(stack), Stack_Height(stack),
			   Stack_Depth(stack));
  Zero_Stack(mask);

  int conn = 26;
  const int *dx = Stack_Neighbor_X_Offset(conn);
  const int *dy = Stack_Neighbor_X_Offset(conn);
  const int *dz = Stack_Neighbor_X_Offset(conn);
  
  int seed[3];
  String_Workspace *sw = New_String_Workspace();
  char *line = NULL;
  FILE *fp = fopen("../data/064.seeds.txt", "r");
  int k = 1;

  /* label seeds */
  while ((line = Read_Line(fp, sw)) != NULL) {
    int n;
    String_To_Integer_Array(line, seed, &n);
    double maxv = -1;
    if (n == 3) {
      maxv = Get_Stack_Pixel(stack, seed[0], seed[1], seed[2], 0);
      int i;
      for (i = 0; i < conn; i++) {
	if (maxv < Get_Stack_Pixel(stack, seed[0] + dx[i], seed[1] + dy[i], 
				   seed[2] + dz[i], 0)) {
	  maxv = Get_Stack_Pixel(stack, seed[0] + dx[i], seed[1] + dy[i], 
				 seed[2] + dz[i], 0);
	}
      }
      
      Set_Stack_Pixel(mask, seed[0], seed[1], seed[2], 0, k);
      for (i = 0; i < conn; i++) {
	Set_Stack_Pixel(mask, seed[0] + dx[i], seed[1] + dy[i], 
			seed[2] + dz[i], 0, k);	
      }
      k++;
    }
  }
  fclose(fp);
  Kill_String_Workspace(sw);

  Stack_Running_Median(stack, 0, stack);
  Stack_Running_Median(stack, 1, stack);
    
  Stack_Watershed_Workspace *ws = Make_Stack_Watershed_Workspace(stack);
  ws->mask = mask;
    
  Filter_3d *filter = Gaussian_Filter_3d(2.0, 2.0, 1.5);
  Stack *filtered_stack  = Filter_Stack(stack, filter);
  Stack_Watershed_Zgap_Barrier(filtered_stack, ws->mask);
  Stack_Running_Max(ws->mask, 0, ws->mask);
  Stack_Running_Max(ws->mask, 1, ws->mask);
  //Write_Stack("../data/test.tif", ws->mask);
    
  Kill_Stack(filtered_stack);
  filtered_stack = NULL;

    
  FMatrix *dm = Mexihat_3D1_F(2.0, NULL, 2);
  //FMatrix *dm = Mexihat_3D_F(2.0, NULL);
  FMatrix_Negative(dm);

  filtered_stack = Filter_Stack(stack, dm);
  
  Stack_Threshold_Common(filtered_stack, 0, 65535);
  Stack_Binarize(filtered_stack);
  Translate_Stack(filtered_stack, GREY, 1);
    
    
  {
    int i, j, k;
    int offset = 0;
    uint16 *array = (uint16*) stack->array;
    for (k = 0; k < stack->depth; k++) {
      for (j = 0; j < stack->height; j++) {
	for (i = 0; i < stack->width; i++) {
	  if (filtered_stack != NULL) {
	    if (filtered_stack->array[offset] == 1) {
	      ws->mask->array[offset] = STACK_WATERSHED_BARRIER;
	    }
	  }
	  array[offset++] += k * 2;
	}
      }
    }
  }

  Kill_Stack(filtered_stack);
  
  Stack_Watershed_Infer_Parameter(stack, ws);
  ws->conn = 6;

  double weights[26] = {0.5, 0.5, 1.0, 1.0, 0.2, 0.2, 0.75, 0.75, 0.75, 0.75,
			0.35, 0.35, 0.35, 0.35, 0.6, 0.6, 0.6, 0.6, 
			0.45, 0.45, 0.45, 0.45,
			0.45, 0.45, 0.45, 0.45};

  ws->weights = weights;
  ws->weights = NULL;
  
  if (ws->weights != NULL) {
    ws->min_level /= 3;
  }
    
  Stack_Running_Median(stack, 0, stack);
  Stack_Running_Median(stack, 1, stack);
  
  Stack *out = Stack_Watershed(stack, ws);
  
  strcpy(mask_path, stack_path);
  strsplit(mask_path, '.', -1);
  sprintf(mask_path, "%s_label.tif", mask_path);
  
  Write_Stack("../data/test.tif", out);

#endif

#if 0
  Stack *stack = Read_Stack("../data/test/soma2.tif");

  int thre = Stack_Find_Threshold_A(stack, THRESHOLD_LOCMAX_TRIANGLE);
  Filter_3d *filter = Gaussian_Filter_3d(1.0, 1.0, 0.5);
  Stack *out = Filter_Stack(stack, filter);
  stack = Copy_Stack(out);
  Stack_Threshold_Binarize(out, thre);
  Stack *out2 = Stack_Bwdist_L_U16P(out, NULL, 0);
  int nvoxel = Stack_Voxel_Number(out);
  uint16_t *dist_array = (uint16_t*) out2->array;
  int i;
  for (i = 0; i < nvoxel; i++) {
    dist_array[i] = sqrt(dist_array[i]);
  }

  Write_Stack("../data/labmeeting13/distp.tif", out2);
#endif

#if 0
  //Stack *stack = Read_Stack("../data/test/soma2.tif");

  Stack *stack = Read_Stack("/Users/zhaot/Data/jinny/slice7_2to34ds_soma_c2.tif");
  int thre = Stack_Find_Threshold_A(stack, THRESHOLD_LOCMAX_TRIANGLE);
  Filter_3d *filter = Gaussian_Filter_3d(1.0, 1.0, 0.5);
  Stack *out = Filter_Stack(stack, filter);
  stack = Copy_Stack(out);
  Stack_Threshold_Binarize(out, thre);
  Stack *out2 = Stack_Bwdist_L_U16P(out, NULL, 0);

  /*
  out = Stack_Locmax_Region(out2, 26);
  */
  Stack_Watershed_Workspace *ws = Make_Stack_Watershed_Workspace(stack);
  ws->mask = Make_Stack(GREY, Stack_Width(stack), Stack_Height(stack),
      Stack_Depth(stack));
  Zero_Stack(ws->mask);
  size_t nvoxel = Stack_Voxel_Number(stack);
  size_t offset;
  uint16_t *dist_array = (uint16_t*) out2->array;
  for (offset = 0; offset < nvoxel; offset++) {
    if (/*(out->array[offset] == 1) && */(dist_array[offset] > 1000)) {
      ws->mask->array[offset] = 1;
    }
  }

  //Objlabel_Workspace *ow = New_Objlabel_Workspace();
  //Stack_Label_Large_Objects_W(ws->mask, 1, 2, 10, ow);
  //Stack_Label_Objects_N(ws->mask, NULL, 1, 2, 26);
  Object_3d_List *objs = Stack_Find_Object(ws->mask, 1, 100);
  Zero_Stack(ws->mask);
  Stack_Draw_Objects_Bw(ws->mask, objs, -255);
  printf("%g\n", Stack_Max(ws->mask, NULL));

  /*
  Write_Stack("../data/test.tif", ws->mask);
  return 1;
*/

  ws->min_level = thre;
  ws->start_level = 65535;

  Stack *out3 = Stack_Watershed(stack, ws);
  /*
  Write_Stack("../data/labmeeting13/watershed.tif", out3);
  return 1;
*/
  for (offset = 0; offset < nvoxel; offset++) {
    if (dist_array[offset] < 300) {
      out3->array[offset] = 0;
    }
  }

  int nregion = Stack_Max(out3, NULL);

  Kill_Stack(out2);
  Stack *filtered = Copy_Stack(stack);
  Kill_Stack(stack);
  
  ws->conn = 8;
  stack = Stack_Region_Border_Shrink(out3, ws);
  out2 = Stack_Region_Expand(stack, 4, 30, NULL);

  for (offset = 0; offset < nvoxel; offset++) {
    if (out->array[offset] == 0) {
      out2->array[offset] = 0;
    }
  }

  Write_Stack("../data/test2.tif", out2);

  Kill_Stack(stack);
  //stack = Read_Stack("../data/test/soma2.tif");
  stack = Read_Stack("/Users/zhaot/Data/jinny/slice7_2to34ds_soma_c2.tif");
  Translate_Stack(stack, COLOR, 1);
  int i;
  double h = 0.0;
  double s = 1.0;
  for (i = 0; i < nregion; i++) {
    Stack_Label_Color_L(stack, out2, i+1, h+=0.35, s, filtered);
    /*
    Rgb_Color color;
    Set_Color_Jet(&color, i*3);
    Stack_Label_Level(stack, out2, i+1, color);
    */
  }
  Write_Stack("../data/test.tif", stack);
#endif

#if 0
  Stack *stack = Read_Stack("../data/leaktest/leak3.tif");
  Stack *distmap = Stack_Bwdist_L_U16P(stack, NULL, 0);

  Stack_Watershed_Workspace *ws = Make_Stack_Watershed_Workspace(stack);
  ws->mask = Copy_Stack(distmap);
  Stack_Threshold_Binarize(ws->mask, 10);
  Translate_Stack(ws->mask, GREY, 1);
  Object_3d_List *objs = Stack_Find_Object(ws->mask, 1, 100);
  Zero_Stack(ws->mask);
  Stack_Draw_Objects_Bw(ws->mask, objs, -255);

  ws->min_level = 1;
  ws->start_level = 65535;
  Stack *out3 = Stack_Watershed(distmap, ws);
  Write_Stack("../data/test.tif", out3);

#endif

#if 0
  Stack *stack = Read_Stack("../data/benchmark/two_disk.tif");
  Stack_Watershed_Workspace *ws = Make_Stack_Watershed_Workspace(stack);
  ws->conn = 26;
  Stack *out = Stack_Watershed(stack, ws);

  Write_Stack("../data/test.tif", out);
#endif
  return 0;
}
예제 #3
0
int main(int argc, char *argv[])
{
#if 0
  Stack *stack = Read_Stack("../data/fly_neuron.tif");

  Stretch_Stack_Value_Q(stack, 0.99);
  Translate_Stack(stack, GREY, 1);

  Write_Stack("../data/test.tif", stack);

  Kill_Stack(stack);
#endif

#if 0
  int idx1, idx2, width, height;
  idx1 = 33332;
  idx2 = 65535;
  width = 111;
  height = 112;

  printf("%g\n", Stack_Util_Voxel_Distance(idx1, idx2, width, height));

  int x1, y1, z1, x2, y2, z2;
  Stack_Util_Coord(idx1, width, height, &x1, &y1, &z1);
  Stack_Util_Coord(idx2, width, height, &x2, &y2, &z2);

  printf("%d, %d, %d\n", x1 - x2, y1 - y2, z1 - z2);
#endif

#if 0
  Stack *stack = Read_Stack("../data/fly_neuron.tif");
  //Translate_Stack(stack, GREY16, 1);
  Image *image = Proj_Stack_Zmax(stack);
  Write_Image("../data/test.tif", image);
#endif
  
#if 0
  Stack *stack = Read_Stack("../data/fly_neuron_a1_org.tif");
  //stack = Crop_Stack(stack, 256, 256, 0, 512, 512, 170, NULL);
  
  int i;
  Stack stack2;
  stack2.width = stack->width;
  stack2.height = stack->height;
  stack2.kind = stack->kind;
  stack2.depth = 1;

  for (i = 0; i < stack->depth; i++) {
    stack2.array = stack->array + i * stack->width * stack->height;
    //Stack *locmax = Stack_Locmax_Region(&stack2, 8);
    Stack *locmax = Stack_Local_Max(&stack2, NULL, STACK_LOCMAX_SINGLE);
    int *hist = Stack_Hist_M(&stack2, locmax);
    int low, high;
    Int_Histogram_Range(hist, &low, &high);
    int thre = Int_Histogram_Triangle_Threshold(hist, low, high);
    printf("Threshold: %d\n", thre);

    Stack_Threshold_Binarize(&stack2, thre);

    Kill_Stack(locmax);
    free(hist);
  }
  //Stack_Bc_Autoadjust(result);
  /*
  Translate_Stack(stack, COLOR, 1);
  Stack_Blend_Mc(stack, result, 0.1);
  */
  Write_Stack("../data/test.tif", stack);
#endif

#if 1
  Stack *stack = Read_Stack("../data/fly_neuron_crop.tif");

  Filter_3d *filter = Gaussian_Filter_3d(1.0, 1.0, 0.5);
  Stack *out = Filter_Stack(stack, filter);
  Kill_FMatrix(filter)

  Write_Stack("../data/test.tif", out);
#endif

#if 0
  Stack *stack = Read_Stack("../data/fly_neuron_a2_org.tif");
  Stack *locmax = Stack_Locmax_Region(stack, 18);
  Stack *mask = Read_Stack("../data/fly_neuron_a2_org/threshold_s.tif");
  //Stack_And(locmax, mask, locmax);

  Object_3d_List *objs = Stack_Find_Object_N(locmax, NULL, 1, 0, 18);
  Zero_Stack(locmax);
  int objnum = 0;
  while (objs != NULL) {
    Object_3d *obj = objs->data;
    Voxel_t center;
    Object_3d_Central_Voxel(obj, center);
    Set_Stack_Pixel(locmax, center[0], center[1], center[2], 0, 1);
    objs = objs->next;
    objnum++;
  }

  Write_Stack("../data/fly_neuron_a2_org/locmax.tif", locmax);

  printf("objnum: %d\n", objnum);

  U8Matrix mat;
  mat.ndim = 3;
  mat.dim[0] = stack->width;
  mat.dim[1] = stack->height;
  mat.dim[2] = stack->depth;
  mat.array = locmax->array;

  dim_type bdim[3];
  bdim[0] = 7;
  bdim[1] = 7;
  bdim[2] = 5;
  U8Matrix *mat2 = U8Matrix_Blocksum(&mat, bdim, NULL);

  int offset[3];
  offset[0] = bdim[0] / 2;
  offset[1] = bdim[1] / 2;
  offset[2] = bdim[2] / 2;
  
  Crop_U8Matrix(mat2, offset, mat.dim, &mat);

  Write_Stack("../data/fly_neuron_a2_org/locmax_sum.tif", locmax);

  Stack_Threshold_Binarize(locmax, 6);
  
  Stack *clear_stack = Stack_Majority_Filter_R(locmax, NULL, 26, 4);
  Struct_Element *se = Make_Cuboid_Se(3, 3, 3);
  Stack *dilate_stack = Stack_Dilate(clear_stack, NULL, se);
  Stack *fill_stack = Stack_Fill_Hole_N(dilate_stack, NULL, 1, 4, NULL);
  Kill_Stack(dilate_stack);
  

  Stack_Not(fill_stack, fill_stack);
  Stack_And(fill_stack, mask, mask);

  Write_Stack("../data/test.tif", mask);
#endif

#if 0
  Stack *stack = Read_Stack("../data/fly_neuron_t1.tif");
  Stack *locmax = Stack_Locmax_Region(stack, 6);
  Stack_Label_Objects_Ns(locmax, NULL, 1, 2, 3, 6);
  
  int nvoxel = Stack_Voxel_Number(locmax);
  int i;
  int s[26];
  for (i = 0; i < nvoxel; i++) {
    if (locmax->array[i] < 3) {
      locmax->array[i] = 0;
    } else {
      locmax->array[i] = 1;
      printf("%u\n", stack->array[i]);
      Stack_Neighbor_Sampling_I(stack, 6, i, -1, s);
      iarray_print2(s, 6, 1);
    }
  }

  //Stack *locmax = Stack_Local_Max(stack, NULL, STACK_LOCMAX_SINGLE);
  Write_Stack("../data/test.tif", locmax);
#endif

#if 0
  Stack *stack = Read_Stack("../data/fly_neuron_n1.tif");
  Stack *stack2 = Flip_Stack_Y(stack, NULL);
  Flip_Stack_Y(stack2, stack2);
  if (!Stack_Identical(stack, stack2)) {
    printf("bug found\n");
  }

  Write_Stack("../data/test.tif", stack);
#endif

#if 0
  Mc_Stack *stack = Read_Mc_Stack("../data/benchmark/L3_12bit.lsm", -1);
  Mc_Stack_Grey16_To_8(stack, 3);
  Write_Mc_Stack("../data/test.lsm", stack, "../data/benchmark/L3_12bit.lsm");
#endif

#if 0
  //Stack *stack = Read_Stack("../data/C2-Slice06_R1_GR1_B1_L18.tif");
  Stack *stack = Read_Stack("../data/fly_neuron_n1/traced.tif");
  Print_Stack_Info(stack);
#endif

#if 0
  Mc_Stack *stack = Make_Mc_Stack(GREY, 1024, 1024, 1024, 5);

  /*
  stack.width = 1024;
  stack.height = 1024;
  stack.depth = 1024;
  stack.kind = GREY;
  stack.nchannel = 5;
  printf("%zd\n", ((size_t)stack.kind * stack.width * stack.height *
		 stack.depth * stack.nchannel));
  */
#endif

#if 0
  Stack *stack = Make_Stack(GREY, 1, 1, 1);
  printf("stack usage: %d\n", Stack_Usage());
  uint8 *data = stack->array;
  stack->array = NULL;
  Kill_Stack(stack);
  stack = Read_Stack("../data/benchmark/line.tif");
  free(data);
  printf("stack usage: %d\n", Stack_Usage());
#endif

#if 0
  Stack *stack = Read_Stack("../data/test.tif");
  int *hist = Stack_Hist(stack);
  Print_Int_Histogram(hist);
#endif

#if 0
  Stack *stack = Read_Stack("../data/benchmark/mouse_neuron_single/stack.tif");
  Stack dst;
  dst.text = "\0";
  dst.array = stack->array;

  Crop_Stack(stack, 0, 0, 0, stack->width - 100, stack->height - 100, 
	     stack->depth - 30, &dst);
  Write_Stack("../data/test.tif", &dst);
#endif

#if 0
  Stack *stack = Make_Stack(GREY, 5, 5, 3);
  Zero_Stack(stack);
  Set_Stack_Pixel(stack, 2, 2, 1, 0, 1.0);
  
  Print_Stack_Value(stack);

  Stack *out = Stack_Running_Max(stack, 0, NULL);
  out = Stack_Running_Max(out, 1, out);
  out = Stack_Running_Max(out, 2, out);

  Print_Stack_Value(out);
#endif

#if 0
  Stack *stack = Read_Stack("../data/benchmark/stack_graph/fork/fork.tif");
  Stack *out = Stack_Running_Median(stack, 0, NULL);
  Stack_Running_Median(out, 1, out);
  //Stack_Running_Max(stack, 0, out);
  //Stack_Running_Max(out, 1, out);

  Write_Stack("../data/test.tif", out);

  Stack *out2 = Stack_Running_Median(stack, 0, NULL);
  Stack *out3 = Stack_Running_Median(out2, 1, NULL);
  
  if (Stack_Identical(out, out3)) {
    printf("Same in-place and out-place\n");
  }
#endif

#if 0
  Stack *stack = Read_Stack_U("../data/diadem_d1_147.xml");
  printf("%d\n", Stack_Threshold_Quantile(stack, 0.9));
#endif

#if 0
  const char *filepath = "/Users/zhaot/Data/Julie/All_tiled_nsyb5_Sum.lsm";
  char filename[100];
  fname(filepath, filename);
  
  Mc_Stack *stack = Read_Mc_Stack(filepath, -1);
  Print_Mc_Stack_Info(stack);

  Mc_Stack *tmpstack = Make_Mc_Stack(stack->kind, stack->width, stack->height,
      stack->depth / 8, stack->nchannel);

  size_t channel_size = stack->kind * stack->width *stack->height
    * stack->depth;
  size_t channel_size2 = tmpstack->kind * tmpstack->width *tmpstack->height
    * tmpstack->depth;

  int i;
  int k;
  uint8_t *array = stack->array;
  for (k = 0; k < 8; k++) {
    int offset = 0;
    int offset2 = 0;
    for (i = 0; i < stack->nchannel; i++) {
      memcpy(tmpstack->array + offset2, array + offset, channel_size2);
      offset += channel_size;
      offset2 += channel_size2;
    }
    array += channel_size2;

    char outpath[500];
    
    sprintf(outpath, "../data/test/%s_%03d.lsm", filename, k);
    Write_Mc_Stack(outpath, tmpstack, filepath);
  }
#endif

#if 0
  Stack *stack = Index_Stack(GREY16, 5, 5, 1);
  Set_Stack_Pixel(stack, 1, 1, 0, 0, 0);
  Set_Stack_Pixel(stack, 1, 2, 0, 0, 0);
  Print_Stack_Value(stack);
  Stack *out = Stack_Neighbor_Median(stack, 8, NULL);
  Print_Stack_Value(out);
#endif

#if 0
  Stack *stack = Make_Stack(GREY, 10, 10, 3);

  Zero_Stack(stack);
  Cuboid_I bound_box;

  Set_Stack_Pixel(stack, 1, 1, 1, 0, 1);
  Set_Stack_Pixel(stack, 1, 2, 1, 0, 1);
  Set_Stack_Pixel(stack, 3, 1, 2, 0, 1);

  Stack_Bound_Box(stack, &bound_box);

  Print_Cuboid_I(&bound_box);
  
#endif

#if 0
  Stack_Document *doc = Xml_Read_Stack_Document("../data/test.xml");
  File_List *list = (File_List*) doc->ci;

  Cuboid_I bound_box;
  Stack_Bound_Box_F(list, &bound_box);
  Print_Cuboid_I(&bound_box);
#endif
  
#if 0
  Stack_Document *doc = Xml_Read_Stack_Document("../data/test.xml");
  File_List *list = (File_List*) doc->ci;
  Print_File_List(list);
  Stack *stack = Read_Image_List_Bounded(list);

  Stack *out = stack;
  out = Stack_Region_Expand(stack, 8, 1, NULL);
  out = Downsample_Stack(out, 4, 4, 0);
  Write_Stack("../data/test.tif", out);

#endif

#if 0
  Stack_Document *doc = Xml_Read_Stack_Document(
      "../data/ting_example_stack/test.xml");
  File_List *list = (File_List*) doc->ci;
  Print_File_List(list);

  int i;
  for (i = 0; i < list->file_number; i++) {
    Stack *stack = Read_Stack_U(list->file_path[i]);
    Stack *ds = Downsample_Stack(stack, 39, 39, 0);
    char file_path[500];
    sprintf(file_path, "../data/ting_example_stack/thumbnails/tb%05d.tif", i);
    Write_Stack(file_path, ds);
    Free_Stack(stack);
  }

#endif

#if 0
  Stack *stack = Read_Stack("../data/test2.tif");

  Stack_Threshold_Binarize(stack, 6);

  Objlabel_Workspace ow;
  STACK_OBJLABEL_OPEN_WORKSPACE(stack, (&ow));

  Object_3d *obj = Stack_Find_Largest_Object_N(stack, ow.chord, 1, 26);
  //Print_Object_3d(obj);
  //printf("%llu\n", obj->size);

  double vec[3];
  Object_3d_Orientation(obj, vec, MAJOR_AXIS);
  double center[3];
  Object_3d_Centroid(obj, center);

  darray_print2(vec, 3, 1);

  double span[2] = {100000, -100000};

  for (int i = 0; i < obj->size; i++) {
    double proj = Geo3d_Dot_Product(vec[0], vec[1], vec[2], 
        (double) obj->voxels[i][0] - center[0], 
        (double) obj->voxels[i][1] - center[1], 
        (double) obj->voxels[i][2] - center[2]);
    if (proj < span[0]) {
      span[0] = proj;
    }
    if (proj > span[1]) {
      span[1] = proj;
    }
  }
  
  darray_print2(span, 2, 1);
  
  double vec2[3];
  Object_3d_Orientation(obj, vec2, PLANE_NORMAL); 
  darray_print2(vec2, 3, 1);

  double span2[2] = {100000, -100000};

  for (int i = 0; i < obj->size; i++) {
    double proj = Geo3d_Dot_Product(vec2[0], vec2[1], vec2[2], 
        (double) obj->voxels[i][0] - center[0], 
        (double) obj->voxels[i][1] - center[1], 
        (double) obj->voxels[i][2] - center[2]);
    if (proj < span2[0]) {
      span2[0] = proj;
    }
    if (proj > span2[1]) {
      span2[1] = proj;
    }
  }
  
  darray_print2(span2, 2, 1);

  double vec3[3];
  Geo3d_Cross_Product(vec[0], vec[1], vec[2], vec2[0], vec2[1], vec2[2],
      vec3, vec3+1, vec3+2);
  double span3[2] = {100000, -100000};

  int i;
  for (i = 0; i < obj->size; i++) {
    double proj = Geo3d_Dot_Product(vec3[0], vec3[1], vec3[2], 
        (double) obj->voxels[i][0] - center[0], 
        (double) obj->voxels[i][1] - center[1], 
        (double) obj->voxels[i][2] - center[2]);
    if (proj < span3[0]) {
      span3[0] = proj;
    }
    if (proj > span3[1]) {
      span3[1] = proj;
    }
  }
  
  darray_print2(span3, 2, 1);

  coordinate_3d_t vertex[8];
  for (i = 0; i < 8; i++) {
    Coordinate_3d_Copy(vertex[i], center);
    int j;
    for (j = 0; j < 3; j++) {
      vertex[i][j] += 
        span[0] * vec[j] + span2[0] * vec2[j] + span3[0] * vec3[j];    
    }
  }

  for (i = 0; i < 3; i++) {
    vertex[1][i] += (span[1] - span[0]) * vec[i]; 
    vertex[2][i] += (span2[1] - span2[0]) * vec2[i]; 
    vertex[3][i] += (span3[1] - span3[0]) * vec3[i]; 

    vertex[4][i] = vertex[1][i] + (span2[1] - span2[0]) * vec2[i]; 
    vertex[5][i] = vertex[2][i] + (span3[1] - span3[0]) * vec3[i]; 
    vertex[6][i] = vertex[3][i] + (span[1] - span[0]) * vec[i]; 

    vertex[7][i] = vertex[5][i] + (span[1] - span[0]) * vec[i]; 
  }

  FILE *fp = fopen("../data/test.swc", "w");
  fprintf(fp, "%d %d %g %g %g %g %d\n", 1, 2, vertex[0][0], vertex[0][1],
      vertex[0][2], 3.0, -1);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 2, 2, vertex[1][0], vertex[1][1],
      vertex[1][2], 3.0, 1);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 3, 2, vertex[2][0], vertex[2][1],
      vertex[2][2], 3.0, 1);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 4, 2, vertex[3][0], vertex[3][1],
      vertex[3][2], 3.0, 1);

  fprintf(fp, "%d %d %g %g %g %g %d\n", 5, 2, vertex[4][0], vertex[4][1],
      vertex[4][2], 3.0, 2);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 6, 2, vertex[5][0], vertex[5][1],
      vertex[5][2], 3.0, 3);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 7, 2, vertex[6][0], vertex[6][1],
      vertex[6][2], 3.0, 4);

  fprintf(fp, "%d %d %g %g %g %g %d\n", 8, 2, vertex[7][0], vertex[7][1],
      vertex[7][2], 3.0, 7);

  fprintf(fp, "%d %d %g %g %g %g %d\n", 9, 2, vertex[4][0], vertex[4][1],
      vertex[4][2], 3.0, 8);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 10, 2, vertex[4][0], vertex[4][1],
      vertex[4][2], 3.0, 3);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 11, 2, vertex[5][0], vertex[5][1],
      vertex[5][2], 3.0, 8);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 12, 2, vertex[5][0], vertex[5][1],
      vertex[5][2], 3.0, 4);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 13, 2, vertex[6][0], vertex[6][1],
      vertex[6][2], 3.0, 2);
  /*
  Geo3d_Scalar_Field *field = Make_Geo3d_Scalar_Field(6);
  field->points[0][0] = span[0] * vec[0] + center[0];
  field->points[0][1] = span[0] * vec[1] + center[1];
  field->points[0][2] = span[0] * vec[2] + center[2];
  field->points[1][0] = span[1] * vec[0] + center[0];
  field->points[1][1] = span[1] * vec[1] + center[1];
  field->points[1][2] = span[1] * vec[2] + center[2];

  field->points[2][0] = span2[0] * vec2[0] + center[0];
  field->points[2][1] = span2[0] * vec2[1] + center[1];
  field->points[2][2] = span2[0] * vec2[2] + center[2];
  field->points[3][0] = span2[1] * vec2[0] + center[0];
  field->points[3][1] = span2[1] * vec2[1] + center[1];
  field->points[3][2] = span2[1] * vec2[2] + center[2];

  field->points[4][0] = span3[0] * vec3[0] + center[0];
  field->points[4][1] = span3[0] * vec3[1] + center[1];
  field->points[4][2] = span3[0] * vec3[2] + center[2];
  field->points[5][0] = span3[1] * vec3[0] + center[0];
  field->points[5][1] = span3[1] * vec3[1] + center[1];
  field->points[5][2] = span3[1] * vec3[2] + center[2];

  coordinate_3d_t corner[2];
  Geo3d_Scalar_Field_Boundbox(field, corner);
  darray_print2(corner[0], 3, 1);
  darray_print2(corner[1], 3, 1);

  fprintf(fp, "%d %d %g %g %g %g %d\n", 1, 2, corner[0][0], corner[0][1],
      corner[0][2], 3.0, -1);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 2, 2, corner[1][0], corner[0][1],
      corner[0][2], 3.0, 1);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 3, 2, corner[1][0], corner[1][1],
      corner[0][2], 3.0, 2);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 4, 2, corner[0][0], corner[1][1],
      corner[0][2], 3.0, 3);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 5, 2, corner[0][0], corner[0][1],
      corner[0][2], 3.0, 4);

  fprintf(fp, "%d %d %g %g %g %g %d\n", 6, 2, corner[0][0], corner[0][1],
      corner[1][2], 3.0, -1);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 7, 2, corner[1][0], corner[0][1],
      corner[1][2], 3.0, 6);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 8, 2, corner[1][0], corner[1][1],
      corner[1][2], 3.0, 7);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 9, 2, corner[0][0], corner[1][1],
      corner[1][2], 3.0, 8);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 10, 2, corner[0][0], corner[0][1],
      corner[1][2], 3.0, 9);

  fprintf(fp, "%d %d %g %g %g %g %d\n", 11, 2, corner[0][0], corner[0][1],
      corner[1][2], 3.0, 1);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 12, 2, corner[1][0], corner[0][1],
      corner[1][2], 3.0, 2);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 13, 2, corner[1][0], corner[1][1],
      corner[1][2], 3.0, 3);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 14, 2, corner[0][0], corner[1][1],
      corner[1][2], 3.0, 4);
      */
  /*
  fprintf(fp, "%d %d %g %g %g %g %d\n", 5, 2, corner[1][0], corner[1][1],
      corner[1][2], 3.0, -1);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 6, 2, corner[0][0], corner[1][1],
      corner[1][2], 3.0, 6);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 7, 2, corner[1][0], corner[0][1],
      corner[1][2], 3.0, 7);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 8, 2, corner[1][0], corner[1][1],
      corner[0][2], 3.0, 7);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 12, 2, corner[1][0], corner[1][1],
      corner[0][2], 3.0, 2);

  fprintf(fp, "%d %d %g %g %g %g %d\n", 9, 2, corner[1][0], corner[1][1],
      corner[1][2], 3.0, 6);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 10, 2, corner[0][0], corner[1][1],
      corner[1][2], 3.0, 3);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 11, 2, corner[1][0], corner[0][1],
      corner[1][2], 3.0, 4);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 12, 2, corner[1][0], corner[1][1],
      corner[0][2], 3.0, -1);
      */

  fprintf(fp, "%d %d %g %g %g %g %d\n", 21, 2, span[0] * vec[0] + center[0], 
      span[0] * vec[1] + center[1], span[0] * vec[2] + center[2], 3.0, -1);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 22, 2, span[1] * vec[0] + center[0], 
      span[1] * vec[1] + center[1], span[1] * vec[2] + center[2], 3.0, 21);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 23, 2, span2[0] * vec2[0] + center[0], 
      span2[0] * vec2[1] + center[1], span2[0] * vec2[2] + center[2], 3.0, -1);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 24, 2, span2[1] * vec2[0] + center[0], 
      span2[1] * vec2[1] + center[1], span2[1] * vec2[2] + center[2], 3.0, 23);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 25, 2, span3[0] * vec3[0] + center[0], 
      span3[0] * vec3[1] + center[1], span3[0] * vec3[2] + center[2], 3.0, -1);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 26, 2, span3[1] * vec3[0] + center[0], 
      span3[1] * vec3[1] + center[1], span3[1] * vec3[2] + center[2], 3.0, 25);

  fclose(fp);
  //double corner[6];

  /*
  FILE *fp = fopen("../data/test.swc", "w");
  fprintf(fp, "%d %d %g %g %g %g %d\n", 1, 2, span[0] * vec[0] + center[0], 
      span[0] * vec[1] + center[1], span[0] * vec[2] + center[2], 3.0, -1);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 2, 2, span[1] * vec[0] + center[0], 
      span[1] * vec[1] + center[1], span[1] * vec[2] + center[2], 3.0, 1);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 3, 2, span2[0] * vec2[0] + center[0], 
      span2[0] * vec2[1] + center[1], span2[0] * vec2[2] + center[2], 3.0, -1);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 4, 2, span2[1] * vec2[0] + center[0], 
      span2[1] * vec2[1] + center[1], span2[1] * vec2[2] + center[2], 3.0, 3);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 5, 2, span3[0] * vec3[0] + center[0], 
      span3[0] * vec3[1] + center[1], span3[0] * vec3[2] + center[2], 3.0, -1);
  fprintf(fp, "%d %d %g %g %g %g %d\n", 6, 2, span3[1] * vec3[0] + center[0], 
      span3[1] * vec3[1] + center[1], span3[1] * vec3[2] + center[2], 3.0, 5);
  fclose(fp);
  */
  //calculate corners

  //Draw the six line of the corners

  /*
  Stack *stack2 = Copy_Stack(stack);
  Zero_Stack(stack2);
  int i = 0;
  for (i = 0; i < obj->size; i++) {

    stack2->array[Stack_Util_Offset(obj->voxels[i][0], obj->voxels[i][1],
        obj->voxels[i][2], stack->width, stack->height, stack->depth)] = 1;
  }

  Write_Stack("../data/test.tif", stack2);
  */
#endif

#if 0
  Stack *stack = Read_Stack("../data/test2.tif");

  Stack_Threshold_Binarize(stack, 6);

  Objlabel_Workspace ow;
  STACK_OBJLABEL_OPEN_WORKSPACE(stack, (&ow));
  ow.conn = 26;
  ow.init_chord = TRUE;

  int obj_size = Stack_Label_Largest_Object_W(stack, 1, 2, &ow);

  Object_3d *obj = Make_Object_3d(obj_size, ow.conn);
  extract_object(ow.chord, ow.seed, obj);
  //Print_Object_3d(obj);

  /*
  STACK_OBJLABEL_CLOSE_WORKSPACE((&ow));
  Objlabel_Workspace *ow = New_Objlabel_Workspace();
  ow->conn = 26;
  ow->init_chord = TRUE;

  STACK_OBJLABEL_OPEN_WORKSPACE(stack, ow);
  Stack_Label_Largest_Object_W(stack, 1, 2, ow); 
*/
  Write_Stack("../data/test3.tif", stack);
#endif
  

#if 0
  Mc_Stack *stack = Read_Mc_Stack("../data/test2.tif", -1);

  Print_Mc_Stack_Info(stack);

  size_t offset;
  size_t voxelNumber = Mc_Stack_Voxel_Number(stack);

  uint8_t* arrayc[3] = {NULL, NULL, NULL};
  arrayc[0] = stack->array;
  arrayc[1] = stack->array + voxelNumber;
  arrayc[2] = stack->array + voxelNumber * 2;

  for (offset = 0; offset < voxelNumber; ++offset) {
    if ((arrayc[0][offset] != 128) || (arrayc[1][offset] != 6) ||
        (arrayc[2][offset] != 0)) {
      arrayc[0][offset] = 0;
      arrayc[1][offset] = 0;
      arrayc[2][offset] = 0;
    }
  }

  Write_Mc_Stack("../data/test.tif", stack, NULL);

  Kill_Mc_Stack(stack);
#endif

#if 0
  Mc_Stack *stack = Read_Mc_Stack("../data/flyem/TEM/slice_figure/segmentation/selected_body.tif", -1);

  Print_Mc_Stack_Info(stack);

  size_t offset;
  size_t voxelNumber = Mc_Stack_Voxel_Number(stack);

  Stack *mask = Make_Stack(GREY, stack->width, stack->height, stack->depth);

  uint8_t* arrayc[4] = {NULL, NULL, NULL, NULL};
  int i;
  for (i = 0; i < 4; ++i) {
    arrayc[i] = stack->array + voxelNumber * i;
  }

  for (offset = 0; offset < voxelNumber; ++offset) {
    if ((arrayc[0][offset] > 0) || (arrayc[1][offset] > 0) ||
        (arrayc[2][offset] > 0) || (arrayc[3][offset] > 0)) {
      mask->array[offset] = 1;
    } else {
      mask->array[offset] = 0;
    }
  }

  mask = Downsample_Stack_Max(mask, 7, 7, 0, NULL);

  Write_Stack("../data/test.tif", mask);
#endif

#if 0
  Stack *stack = Read_Stack("../data/test2.tif");
  size_t offset;
  size_t voxelNumber = Stack_Voxel_Number(stack);
  color_t *arrayc = (color_t*) stack->array;
  for (offset = 0; offset < voxelNumber; ++offset) {
    if ((arrayc[offset][0] != 128) || (arrayc[offset][1] != 6) ||
        (arrayc[offset][2] != 0)) {
      arrayc[offset][0] = 0;
      arrayc[offset][1] = 0;
      arrayc[offset][2] = 0;
    }
  }

  Write_Stack("../data/test.tif", stack);
#endif

#if 0
  Stack *stack = Read_Stack("../data/flyem/TEM/slice_figure/segmentation/selected_body_volume.tif");
  Stack *out = Make_Stack(COLOR, stack->width, stack->height, stack->depth);
  Zero_Stack(out);

  Object_3d_List *objs = Stack_Find_Object_N(stack, NULL, 255, 0, 26);
  Print_Object_3d_List_Compact(objs);
  uint8_t color[] = {0, 200, 50, 200, 0, 0};
  uint8_t *color2 = color;
  while (objs != NULL) {
    Object_3d *obj = objs->data;

    Stack_Draw_Object_C(out, obj, color2[0], color2[1], color2[2]);
    color2 += 3;

    objs = objs->next;
    break;
  }

  Write_Stack("../data/test.tif", out);
  
#endif

#if 0
  //Stack *stack = Read_Stack("../data/benchmark/binary/2d/btrig2.tif");
  Stack *stack = Make_Stack(GREY, 3, 3, 3);
  One_Stack(stack);
  //Zero_Stack(stack);
  //Set_Stack_Pixel(stack, 0, 1, 1, 1, 1);
  Set_Stack_Pixel(stack, 0, 1, 1, 1, 0);
  Set_Stack_Pixel(stack, 0, 1, 1, 0, 0);
  Set_Stack_Pixel(stack, 0, 0, 0, 0, 0);
  Set_Stack_Pixel(stack, 0, 0, 2, 0, 0);
  Set_Stack_Pixel(stack, 0, 2, 0, 0, 0);
  Set_Stack_Pixel(stack, 0, 2, 2, 0, 0);
  Set_Stack_Pixel(stack, 0, 0, 0, 2, 0);
  Set_Stack_Pixel(stack, 0, 0, 2, 2, 0);
  Set_Stack_Pixel(stack, 0, 2, 0, 2, 0);
  Set_Stack_Pixel(stack, 0, 2, 2, 2, 0);
  //Set_Stack_Pixel(stack, 0, 1, 1, 2, 0);
  Stack_Graph_Workspace *sgw = New_Stack_Graph_Workspace();
  //Default_Stack_Graph_Workspace(sgw);
  sgw->signal_mask = stack;
  Graph *graph = Stack_Graph_W(stack, sgw);
  sgw->signal_mask = NULL;
  //Print_Graph(graph);
  //Graph_To_Dot_File(graph, "../data/test.dot");

  if (Graph_Has_Hole(graph) == TRUE) {
    printf("The graph has a hole.\n");
  }
#endif

#if 0
  Stack *stack = Read_Stack("../data/flyem/skeletonization/session3/T1_207.tif");

  size_t voxel_number = Stack_Voxel_Number(stack);
  size_t i;
  for (i = 0; i < voxel_number; ++i) {
    if (stack->array[i] == 1) {
      stack->array[i] = 255;
    }
  }
  
  Filter_3d *filter = Gaussian_Filter_3d(0.5, 0.5, 0.5);
  Stack *out = Filter_Stack(stack, filter);

  Write_Stack("../data/test2.tif", out);
#endif

#if 0
  Stack *stack = Make_Stack(GREY, 3, 3, 3);
  Zero_Stack(stack);
  Cuboid_I cuboid;
  Cuboid_I_Set_S(&cuboid, 0, 0, 0, 4, 2, 3);
  Cuboid_I_Label_Stack(&cuboid, 1, stack);
  Print_Stack_Value(stack);

#endif

#if 0
  Stack *stack = Make_Stack(GREY, 3, 3, 1);
  Zero_Stack(stack);
  Set_Stack_Pixel(stack, 1, 1, 0, 0, 1);
  Print_Stack_Value(stack);

  Stack *out = Downsample_Stack_Max(stack, 2, 2, 2, NULL);

  Print_Stack_Value(out);
#endif

#if 0
  double t[3] = {1, 2 * 256 + 12, 255 * 256};
  printf("%g\n", Stack_Voxel_Weight_C(t));

#endif

#if 0
  Stack *stack = Read_Stack_U("../data/vr/label.tif");
  Stack_Binarize_Level(stack, 1);
  Stack_Label_Large_Objects_N(stack, NULL, 1, 2, 2000, 4);

  Stack_Threshold_Binarize(stack, 2);
  Write_Stack("../data/vr/label1.tif", stack);
#endif

#if 0
  Stack *stack = Read_Stack_U("../data/vr/label.tif");
  Stack_Binarize_Level(stack, 5);
  Stack_Label_Large_Objects_N(stack, NULL, 1, 2, 5000, 4);

  Stack_Threshold_Binarize(stack, 2);
  Write_Stack("../data/vr/label5.tif", stack);
#endif


#if 0
  Stack *stack = Read_Stack_U("../data/vr/original.tif");

  /* Make mask */
  Stack *mask = Make_Stack(GREY, Stack_Width(stack), Stack_Height(stack),
			   Stack_Depth(stack));
  Zero_Stack(mask);

  Stack *overallLabel = Copy_Stack(mask);

  Stack *labelStack[5];

  size_t voxelNumber = Stack_Voxel_Number(mask);
  size_t k;

  Struct_Element *se = Make_Disc_Se(5);

  int i;
  char filePath[100];
  for (i = 0; i < 5; ++i) {
    sprintf(filePath, "../data/vr/label%d.tif", i + 1);
    labelStack[i] = Read_Stack_U(filePath);
    //labelStack[i] = Stack_Erode_Fast(labelStack[i], NULL, se);
    Stack_Or(mask, labelStack[i], mask);
    for (k = 0; k < voxelNumber; ++k) {
      if (labelStack[i]->array[k] == 1) {
        overallLabel->array[k] = i + 1;
      }
    }
  }
  
  for (k = 0; k < voxelNumber; ++k) {
    if (mask->array[k] == 1) {
      mask->array[k] = SP_GROW_SOURCE;
    }
  }

  Sp_Grow_Workspace *sgw = New_Sp_Grow_Workspace();
  sgw->size = voxelNumber;
  Sp_Grow_Workspace_Set_Mask(sgw, mask->array);
  sgw->wf = Stack_Voxel_Weight_C;
  sgw->sp_option = 1;

  tic();
  Int_Arraylist *path = Stack_Sp_Grow(stack, NULL, 0, NULL, 0, sgw);
  printf("time: %llu\n", toc());

  Kill_Int_Arraylist(path);

  for (k = 0; k < voxelNumber; ++k) {
    if (mask->array[k] == 0) {
      int idx = (int) k;
      while (overallLabel->array[idx] == 0) {
        idx = sgw->path[idx];
      }
      int label = overallLabel->array[idx];
      idx = (int) k;
      while (overallLabel->array[idx] == 0) {
        overallLabel->array[idx] = label;
        idx = sgw->path[idx];
      }
    }
  }

  for (k = 0; k < voxelNumber; ++k) {
    if (overallLabel->array[k] == 1 || overallLabel->array[k] == 5) {
      overallLabel->array[k] = 0;
    }
  }

  Write_Stack("../data/test.tif", overallLabel);   
  
  Kill_Stack(stack);
#endif

  return 0;
}