Пример #1
0
Geo3d_Scalar_Field* ZNeuronTracer::extractLineSeed(
    const Stack *mask, const Stack *dist, int minObjSize)
{
  Object_3d_List *objList = Stack_Find_Object_N(
        const_cast<Stack*>(mask), NULL, 1, minObjSize, 26);
  ZObject3dArray objArray;
  objArray.append(objList);

  Geo3d_Scalar_Field *field = Make_Geo3d_Scalar_Field(objArray.size());
  for (size_t i = 0; i < objArray.size(); ++i) {
    ZObject3d *obj = objArray[i];
    ZIntPoint pt = obj->getCentralVoxel();
    field->points[i][0] = pt.getX();
    field->points[i][1] = pt.getY();
    field->points[i][2] = pt.getZ();
    field->values[i] = sqrt(
          C_Stack::value(dist, pt.getX(), pt.getY(), pt.getZ()));
  }

  return field;
}
Пример #2
0
int main(int argc, char *argv[])
{
  /* Takes '-ver' option and show the version */
  if (Show_Version(argc, argv, "0.1") == 1) {
    return 0;
  }

  //For testing
#if 0
  Object_3d *obj_test = Make_Object_3d(27, 6);
  ndim_type ndim_test = 3;
  dim_type dim_test[3] = {3, 3, 3};

  FMatrix *detected1_test = FMatrix_Ones(dim_test, ndim_test);
  detected1_test->array[0] = 2.0;
  detected1_test->array[7] = 2.0;
  size_t offset = 0;
  for (int k = 0; k < 3; k++) {
      for (int j = 0; j < 3; j++) {
          for (int i = 0; i < 3; i++) {
              obj_test->voxels[offset][0] = i;
              obj_test->voxels[offset][1] = j;
              obj_test->voxels[offset][2] = k;
              offset++;
          }
      }
  }
  vector<int> labelArray_test = apply_clustering(obj_test, detected1_test, 3);

  return 1;
#endif

#if 1
  static const char *Spec[] = {"--result_file <string>",
    "--param_file <string> --jsonoutputfile <string>", 
    "[--test <string>]", "[--golden <string>]", NULL};

  ZArgumentProcessor::processArguments(argc, argv, Spec);

  cout << argv[0] << " started ..." << endl;

  cout << "Parsing parameters ..." << endl;
  TbarSaParameter opts;
  parse_param(ZArgumentProcessor::getStringArg("--param_file"), &opts);

  cout << "TBar width " << opts.tbarWidth << endl;
  cout << "cluster_cc " << opts.clusterCC << endl;
  cout << "max_cluster_sz " << opts.max_cluster_sz << endl;
  cout << "boundary_offset " << opts.offset << endl;
  cout << "thds ";
  darray_print(&(opts.thds[0]), opts.thds.size());
  cout << "min_dist_ctrs " << opts.minDistCtrs << endl;
  cout << "save_screenshot " << opts.saveScreenShot << endl;

  cout << "Loading result file ..." << endl;
  if (opts.saveScreenShot) {
      cout << "Screenshot function is not avaible yet" << endl;
    //void *vol_all = hdf5read(Get_String_Arg("--result_file"), "volume/data");
    //double *vol = hdf5_make_double_array(vol_all, type);
  }

  FMatrix *detected_all =
          hdf5read_prediction(ZArgumentProcessor::getStringArg("--result_file"));
  size_t length = Matrix_Size(detected_all->dim, detected_all->ndim);
  cout << "Overall length: " << length << endl;

  int vold = detected_all->dim[1];
  int imh = detected_all->dim[2];
  int imw = detected_all->dim[3];

  dim_type dims[3];
  dims[0] = detected_all->dim[1];
  dims[1] = detected_all->dim[2];
  dims[2] = detected_all->dim[3];
  FMatrix *detected1 = Make_FMatrix(dims, 3);

  length = Matrix_Size(detected1->dim, detected1->ndim);
  size_t offset_all = 0;
  for (size_t offset1 = 0; offset1 < length; offset1++) {
    detected1->array[offset1] = detected_all->array[offset_all];
    offset_all += detected_all->dim[0];
  }

  string testDir;
  string goldenDir;
  if (ZArgumentProcessor::isArgMatched("--golden")) {
    goldenDir = ZArgumentProcessor::getStringArg("--golden");
  }
  if (ZArgumentProcessor::isArgMatched("--test")) {
    testDir = ZArgumentProcessor::getStringArg("--test");
  }
  
  if (ZArgumentProcessor::isArgMatched("--golden") ||
      ZArgumentProcessor::isArgMatched("--test")) {
    Stack golden;
    golden.kind = FLOAT32;
    golden.width = detected1->dim[0];
    golden.height = detected1->dim[1];
    golden.depth = detected1->dim[2];
    golden.array = (uint8*) detected1->array;
    golden.text = const_cast<char*>("\0");
    if (ZArgumentProcessor::isArgMatched("--golden")) {
      Write_Stack(const_cast<char*>((goldenDir + "/detected1.tif").c_str()), 
          &golden);
    } 
    if (ZArgumentProcessor::isArgMatched("--test")) {
      Write_Stack(const_cast<char*>((testDir + "/detected1.tif").c_str()), 
          &golden);
    }
  }

  //////testing//////////
  /*
  Stack *stack = Scale_Float_Stack(detected1->array, detected1->dim[0],
                                   detected1->dim[1], detected1->dim[2],
                                   GREY);
  Write_Stack("/Users/zhaot/Work/neutube/neurolabi/data/test.tif", stack);
  */
  ///////////////////

  double avg_sz = opts.tbarWidth * opts.tbarWidth * opts.tbarWidth;
  double min_sz = avg_sz / 125.0;

  double tbar_cube_w = floor(opts.tbarWidth / 2.0);

  static const int morpho_w = 3;
  Struct_Element *se = Make_Cuboid_Se(morpho_w, morpho_w, morpho_w);

  Stack *bb = Make_Stack(GREY, vold, imh, imw);
  Stack *bb2 = Make_Stack(GREY, vold, imh, imw);
  Stack *bb3 = Make_Stack(GREY, vold, imh, imw);
  size_t volume = Stack_Voxel_Number(bb);

  for (int ti = 0; ti < opts.thds.size(); ti++) {
    double thd = opts.thds[ti];
    cout << "thd = " << thd << endl;
    for (size_t offset = 0; offset < volume; offset++) {
      if (detected1->array[offset] > thd) {
        bb->array[offset] = 1;
      } else {
        bb->array[offset] = 0;
      }
    }

    //Write_Stack(const_cast<char*>("../../../data/test.tif"), bb);
    if (ZArgumentProcessor::isArgMatched("--golden")) {
      Write_Stack(const_cast<char*>((goldenDir + "/bb.tif").c_str()), bb);
    } 

    if (ZArgumentProcessor::isArgMatched("--test")) {
      Write_Stack(const_cast<char*>((testDir + "/bb.tif").c_str()), bb);
    }

    tic();
    Stack_Erode_Fast(bb, bb2, se);
    ptoc();

    //Write_Stack(const_cast<char*>("../../../data/test2.tif"), bb2);
    if (ZArgumentProcessor::isArgMatched("--golden")) {
      Write_Stack(const_cast<char*>((goldenDir + "/bb2.tif").c_str()), bb2);
    } 

    if (ZArgumentProcessor::isArgMatched("--test")) {
      Write_Stack(const_cast<char*>((testDir + "/bb2.tif").c_str()), bb2);
    }

    tic();
    Stack_Dilate_Fast(bb2, bb3, se);
    ptoc();

    if (ZArgumentProcessor::isArgMatched("--golden")) {
      Write_Stack(const_cast<char*>((goldenDir + "/bb3.tif").c_str()), bb3);
    } 

    if (ZArgumentProcessor::isArgMatched("--test")) {
      Write_Stack(const_cast<char*>((testDir + "/bb3.tif").c_str()), bb3);
    }

    tic();
    cout << "Finding objects ..." << endl;
    Object_3d_List *objList = Stack_Find_Object_N(bb3, NULL, 1, 0, 6);
    ptoc();

    if (ZArgumentProcessor::isArgMatched("--golden")) {
      Object_3d_List_Export_Csv(const_cast<char*>((goldenDir + "/objlist.csv").c_str()), objList);
    } 

    if (ZArgumentProcessor::isArgMatched("--test")) {
      Object_3d_List_Export_Csv(const_cast<char*>((testDir + "/objlist.csv").c_str()), objList);
    }

    ZObject3dArray objArray;
    Object_3d_List *head = objList;
    cout << "split objects ..." << endl;
    tic();
    if (opts.clusterCC) {
      while (objList != NULL) {
        vector<int> labelArray =
          apply_clustering(objList->data, detected1,
              opts.max_cluster_sz);
        objArray.append(objList->data, labelArray);
        //cout << "Object number: " << objArray.size() << endl;
        objList = objList->next;
      }
      Kill_Object_3d_List(head);
    } else {
      objArray.append(objList);
    }
    ptoc();

    cout << objArray.size() << " objects" << endl;

    if (ZArgumentProcessor::isArgMatched("--golden")) {
      objArray.exportCsvFile((goldenDir + "/split_objlist.csv").c_str());
    } 

    if (ZArgumentProcessor::isArgMatched("--test")) {
      objArray.exportCsvFile((testDir + "/split_objlist.csv").c_str());
    }

    cout << "Computer centers ..." << endl;
    ZObject3d centerArray;

    int nselected = 0;
    for (size_t i = 0; i < objArray.size(); i++) {
      if (objArray[i]->size() >= min_sz) {
        nselected++;
        //objArray[i]->translate(-1, -1, -1);

        ZPoint center = objArray[i]->computeCentroid(detected1);
        int x = floor(center.x()) + 1;
        int y = floor(center.y()) + 1;
        int z = floor(center.z()) + 1;

        if (z >= opts.offset &&
            z <= (detected1->dim[2] - opts.offset) &&
            y >= opts.offset &&
            y <= (detected1->dim[1] - opts.offset)) {
          centerArray.append(x, y, z);
        }
      }
    }
    cout << centerArray.size() << " centers calculated." << endl;

    if (ZArgumentProcessor::isArgMatched("--golden")) {
      centerArray.exportCsvFile((goldenDir + "/center.csv").c_str());
    } 

    if (ZArgumentProcessor::isArgMatched("--test")) {
      centerArray.exportCsvFile((testDir + "/center.csv").c_str());
    }

    cout << "Merge centers ..." << endl;

    vector<size_t> indexArray = centerArray.toIndexArray<size_t>(
        detected1->dim[0], detected1->dim[1], detected1->dim[2],
        -1, -1, -1);

    vector<float> weightArray(indexArray.size());
    for (int i = 0; i < indexArray.size(); i++) {
      weightArray[i] = detected1->array[indexArray[i]];
    }

    vector<int> rankArray(indexArray.size());
    farray_qsort(&(weightArray[0]), &(rankArray[0]), weightArray.size());

    vector<bool> merged(centerArray.size(), false);
    int nmerged = 0;
    for (int i = centerArray.size() - 1; i >= 0; i--) {
      if (!merged[rankArray[i]]) {
        for (int j = 0; j < centerArray.size(); j++) {
          if (!merged[j] && j != rankArray[i]) {
            int dx = centerArray.x(rankArray[i]) - centerArray.x(j);
            int dy = centerArray.y(rankArray[i]) - centerArray.y(j);
            int dz = centerArray.z(rankArray[i]) - centerArray.z(j);
            int d = dx * dx + dy * dy + dz * dz;
            if (sqrt(d) < opts.minDistCtrs) {
              merged[j] = true;
              nmerged++;
            }
          }
        }
      }
    }

    //centerArray.exportSwcFile("/Users/zhaot/Work/neutube/neurolabi/data/test.swc");
    cout << nmerged << " merged" << endl;

    ///test///

    if (ZArgumentProcessor::isArgMatched("--golden")) {
      ofstream stream((goldenDir + "/merged_center.csv").c_str());

      for (size_t i = 0; i < centerArray.size(); i++) {
        if (!merged[i]) {
          stream << centerArray.x(i) - 1 << "," << centerArray.y(i) - 1 << ","
            << centerArray.z(i) - 1 << endl;
        }
      }

      stream.close();
    } 

    if (ZArgumentProcessor::isArgMatched("--test")) {
      ofstream stream((testDir + "/merged_center.csv").c_str());

      for (size_t i = 0; i < centerArray.size(); i++) {
        if (!merged[i]) {
          stream << centerArray.x(i) - 1 << "," << centerArray.y(i) - 1 << ","
            << centerArray.z(i) - 1 << endl;
        }
      }

      stream.close();
    } 

    ofstream stream("/Users/zhaot/Work/neutube/neurolabi/data/test2.swc");

    for (size_t i = 0; i < centerArray.size(); i++) {
      if (!merged[i]) {
        stream << i + 1 << " " << 2 << " "
               << centerArray.x(i) - 1 << " " << centerArray.y(i) - 1 << " "
               << centerArray.z(i) - 1
               << " " << 3.0 << " " << -1 << endl;
      }
    }

    stream.close();


    //////////

    cout << "Writing results to "
         << ZArgumentProcessor::getStringArg("--jsonoutputfile")
         << "..." << endl;

    FILE *jfid = fopen(ZArgumentProcessor::getStringArg(("--jsonoutputfile")),
                     "w");
    fprintf(jfid,"{\n  \"data\": [\n");
    bool tbarcheck = false;
    for (int i = 0; i < centerArray.size(); i++) {
      if (!merged[i] /*|| kept[i]*/) {
        int mx = centerArray.z(i);
        int my = centerArray.y(i);
        int mz = centerArray.x(i);

        if (tbarcheck) {
          fprintf(jfid, ",\n    { \n");
        } else {
          fprintf(jfid, "    { \n");
        }

        fprintf(jfid,"\t \"T-bar\": { \n");
        fprintf(jfid,"\t\t\"status\": \"working\", \n");
        fprintf(jfid,"\t\t\"confidence\": 1.0, \n");
        fprintf(jfid,"\t\t\"body ID\": -1, \n");
        fprintf(jfid,"\t\t\"location\": [ \n");
        fprintf(jfid,"\t\t   %d, \n", mx);
        fprintf(jfid,"\t\t   %d, \n", detected1->dim[1] - my);
        fprintf(jfid,"\t\t   %d  \n", mz);
        fprintf(jfid,"\t\t ] \n");
        fprintf(jfid,"\t }, \n");
        fprintf(jfid,"\t\"partners\": [  ]\n");
        fprintf(jfid,"    } ");

        tbarcheck=true;
      }
    }

    fprintf(jfid,"\n  ],\n");
    fprintf(jfid,"  \"metadata\": {\n");
    fprintf(jfid,"\t  \"username\": \"dummy\",\n");
    fprintf(jfid,"\t  \"software version\": \"dummy\",\n");
    fprintf(jfid,"\t  \"description\": \"synapse annotations\",\n");
    fprintf(jfid,"\t  \"file version\": 1, \n");
    fprintf(jfid,"\t  \"software version\": \"dummy\",\n");
    fprintf(jfid,"\t  \"computer\": \"dummy\",\n");
    fprintf(jfid,"\t  \"date\": \"dummy\",\n");
    fprintf(jfid,"\t  \"session path\": \"dummy\",\n");
    fprintf(jfid,"\t  \"software\": \"dummy\"\n");
    fprintf(jfid,"  }\n}");

    fclose(jfid);
  }
#endif


#ifdef _TEST_
  cout << "Testing hdf5 now ... " << endl;

  hid_t file_id;
  herr_t status;

  file_id = H5Fcreate("../../../data/test.h5", H5F_ACC_TRUNC, H5P_DEFAULT,
      H5P_DEFAULT);

  status = H5Fclose(file_id);

  hid_t dataset_id, dataspace_id;
  hsize_t dims[2];

  file_id = H5Fcreate("../../../data/test.h5", H5F_ACC_TRUNC, H5P_DEFAULT,
      H5P_DEFAULT);
  
  dims[0] = 4;
  dims[1] = 6;
  dataspace_id = H5Screate_simple(2, dims, NULL);

  dataset_id = H5Dcreate(file_id, "/dset", H5T_STD_I32BE, dataspace_id,
      H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

  status = H5Dclose(dataset_id);
  status = H5Sclose(dataspace_id);
  status = H5Fclose(file_id);

  int i, j, dset_data[4][6];
  for (i = 0; i < 4; i++) {
    for (j = 0; j < 6; j++) {
      dset_data[i][j] = i * 6 + j + 1;
    }
  }

  file_id = H5Fopen("../../../data/test.h5", H5F_ACC_RDWR, H5P_DEFAULT);

  dataset_id = H5Dopen(file_id, "/dset", H5P_DEFAULT);

  status = H5Dwrite(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
      dset_data);
  status = H5Dread(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
      dset_data);

  status = H5Dclose(dataset_id);
  status = H5Fclose(file_id);

  dims[0] = 2;
  dims[1] = 3;
  int data[6] = {1,2,3,4,5,6};

  file_id = H5Fcreate("../../../data/test.h5", H5F_ACC_TRUNC, H5P_DEFAULT,
      H5P_DEFAULT);

  status = H5LTmake_dataset(file_id, "/dset", 2, dims, H5T_NATIVE_INT, data);

  status = H5Fclose(file_id);

  size_t nrow, n_values;

  file_id = H5Fopen("../../../data/test.h5", H5F_ACC_RDONLY, H5P_DEFAULT);
  status = H5LTread_dataset_int(file_id, "/dset", data);

  status = H5LTget_dataset_info(file_id, "/dset", dims, NULL, NULL);
  n_values = (size_t) (dims[0] * dims[1]);
  nrow = (size_t) dims[1];

  cout << n_values << " " << nrow << endl;
  for (i = 0; i < n_values / nrow; i++) {
    for (j = 0; j < nrow; j++) {
      printf(" %d", data[i * nrow + j]);
    }
    printf("\n");
  }
  status = H5Fclose(file_id);

  hid_t group_id;
  file_id = H5Fcreate("../../../data/test.h5", H5F_ACC_TRUNC, H5P_DEFAULT,
      H5P_DEFAULT);
  group_id = H5Gcreate(file_id, "/MyGroup", H5P_DEFAULT, H5P_DEFAULT,
      H5P_DEFAULT);
  status = H5Gclose(group_id);
  status = H5Fclose(file_id);

  hid_t group1_id, group2_id, group3_id;

  file_id = H5Fcreate("../../../data/test.h5", H5F_ACC_TRUNC, H5P_DEFAULT,
      H5P_DEFAULT);

  group1_id = H5Gcreate(file_id, "/MyGroup", H5P_DEFAULT, H5P_DEFAULT,
      H5P_DEFAULT);
  group2_id = H5Gcreate(file_id, "/MyGroup/Group_A", H5P_DEFAULT, H5P_DEFAULT,
      H5P_DEFAULT);

  group3_id = H5Gcreate(group1_id, "Group_B", H5P_DEFAULT, H5P_DEFAULT, 
      H5P_DEFAULT);

  status = H5Gclose(group1_id);
  status = H5Gclose(group2_id);
  status = H5Gclose(group3_id);

  status = H5Fclose(file_id);

  int dset1_data[3][3], dset2_data[2][10];
  for (i = 0; i < 3; i++) {
    for (j = 0; j < 3; j++) {
      dset1_data[i][j] = j + 1;
    }
  }

  for (i = 0; i < 2; i++) {
    for (j = 0; j < 10; j++) {
      dset2_data[i][j] = j + 1;
    }
  }

  file_id = H5Fopen("../../../data/test.h5", H5F_ACC_RDWR, H5P_DEFAULT);

  dims[0] = 3;
  dims[1] = 3;
  dataspace_id = H5Screate_simple(2, dims, NULL);

  dataset_id = H5Dcreate(file_id, "/MyGroup/dset1", H5T_STD_I32BE, dataspace_id,
      H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

  status = H5Dwrite(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
      dset1_data);

  status = H5Sclose(dataspace_id);
  status = H5Dclose(dataset_id);

  group_id = H5Gopen(file_id, "/MyGroup/Group_A", H5P_DEFAULT);

  dims[0] = 2;
  dims[1] = 10;
  dataspace_id = H5Screate_simple(2, dims, NULL);

  dataset_id = H5Dcreate(group_id, "dset2", H5T_STD_I32BE, dataspace_id,
      H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

  status = H5Dwrite(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
      dset2_data);

  status = H5Sclose(dataspace_id);
  status = H5Gclose(group_id);
  status = H5Fclose(file_id);

  cout << "Done!" << endl;
#endif

  return 0;
}