コード例 #1
0
void Fine2DRegistrationRestraint::set_subject_image(em2d::Image *subject) {
  // Read the registration parameters from the subject images

  algebra::Vector3D euler = subject->get_header().get_euler_angles();
  algebra::Rotation3D R =
      algebra::get_rotation_from_fixed_zyz(euler[0], euler[1], euler[2]);
  algebra::Vector3D origin = subject->get_header().get_origin();
  algebra::Vector3D translation(origin[0] * params_.pixel_size,
                                origin[1] * params_.pixel_size, 0.0);

  subject_->set_data(subject->get_data());  // deep copy, avoids leaks
  unsigned int rows = subject_->get_header().get_number_of_rows();
  unsigned int cols = subject_->get_header().get_number_of_columns();
  if (projection_->get_header().get_number_of_columns() != cols ||
      projection_->get_header().get_number_of_rows() != rows) {
    projection_->set_size(rows, cols);
  }

  PP_.set_rotation(R);
  PP_.set_translation(translation);
  double s = params_.pixel_size;
  algebra::Vector3D min_values(-s * rows, -s * cols, 0.0);
  algebra::Vector3D max_values(s * rows, s * cols, 0.0);
  PP_.set_proper_ranges_for_keys(this->get_model(), min_values, max_values);

  IMP_LOG_VERBOSE("Subject set for Fine2DRegistrationRestraint" << std::endl);
}
コード例 #2
0
        void generate_header (Image::Header& header, const std::string& tck_file_path, const std::vector<float>& voxel_size)
        {

          Tractography::Properties properties;
          Tractography::Reader<float> file (tck_file_path, properties);

          Streamline<float> tck;
          size_t track_counter = 0;

          Point<float> min_values ( INFINITY,  INFINITY,  INFINITY);
          Point<float> max_values (-INFINITY, -INFINITY, -INFINITY);

          {
            ProgressBar progress ("creating new template image...", 0);
            while (file (tck) && track_counter++ < MAX_TRACKS_READ_FOR_HEADER) {
              for (std::vector<Point<float> >::const_iterator i = tck.begin(); i != tck.end(); ++i) {
                min_values[0] = std::min (min_values[0], (*i)[0]);
                max_values[0] = std::max (max_values[0], (*i)[0]);
                min_values[1] = std::min (min_values[1], (*i)[1]);
                max_values[1] = std::max (max_values[1], (*i)[1]);
                min_values[2] = std::min (min_values[2], (*i)[2]);
                max_values[2] = std::max (max_values[2], (*i)[2]);
              }
              ++progress;
            }
          }

          min_values -= Point<float> (3.0*voxel_size[0], 3.0*voxel_size[1], 3.0*voxel_size[2]);
          max_values += Point<float> (3.0*voxel_size[0], 3.0*voxel_size[1], 3.0*voxel_size[2]);

          header.name() = "tckmap image header";
          header.set_ndim (3);

          for (size_t i = 0; i != 3; ++i) {
            header.dim(i) = std::ceil((max_values[i] - min_values[i]) / voxel_size[i]);
            header.vox(i) = voxel_size[i];
            header.stride(i) = i+1;
            //header.set_units (i, Image::Axis::millimeters);
          }

          //header.set_description (0, Image::Axis::left_to_right);
          //header.set_description (1, Image::Axis::posterior_to_anterior);
          //header.set_description (2, Image::Axis::inferior_to_superior);

          Math::Matrix<float>& M (header.transform());
          M.allocate (4,4);
          M.identity();
          M(0,3) = min_values[0];
          M(1,3) = min_values[1];
          M(2,3) = min_values[2];
          file.close();
        }
コード例 #3
0
ファイル: mapping.cpp プロジェクト: richardbeare/mrtrix3
        void generate_header (Header& header, const std::string& tck_file_path, const vector<default_type>& voxel_size)
        {

          Properties properties;
          Reader<> file (tck_file_path, properties);

          Streamline<> tck;
          size_t track_counter = 0;

          Eigen::Vector3f min_values ( Inf,  Inf,  Inf);
          Eigen::Vector3f max_values (-Inf, -Inf, -Inf);

          {
            ProgressBar progress ("creating new template image", 0);
            while (file (tck) && track_counter++ < MAX_TRACKS_READ_FOR_HEADER) {
              for (const auto& i : tck) {
                min_values[0] = std::min (min_values[0], i[0]);
                max_values[0] = std::max (max_values[0], i[0]);
                min_values[1] = std::min (min_values[1], i[1]);
                max_values[1] = std::max (max_values[1], i[1]);
                min_values[2] = std::min (min_values[2], i[2]);
                max_values[2] = std::max (max_values[2], i[2]);
              }
              ++progress;
            }
          }

          min_values -= Eigen::Vector3f (3.0*voxel_size[0], 3.0*voxel_size[1], 3.0*voxel_size[2]);
          max_values += Eigen::Vector3f (3.0*voxel_size[0], 3.0*voxel_size[1], 3.0*voxel_size[2]);

          header.name() = "tckmap image header";
          header.ndim() = 3;

          for (size_t i = 0; i != 3; ++i) {
            header.size(i) = std::ceil((max_values[i] - min_values[i]) / voxel_size[i]);
            header.spacing(i) = voxel_size[i];
            header.stride(i) = i+1;
          }

          header.transform().matrix().setIdentity();
          header.transform().translation() = min_values.cast<double>();
          file.close();
        }
コード例 #4
0
ファイル: surfaceio.hpp プロジェクト: ulvmoen/crava
void  NRLib::ReadSgriSurf(const std::string & filename,
                          RegularSurface<A> & surface,
                          double            & angle)
{
    std::ifstream header_file;
    OpenRead(header_file, filename.c_str(), std::ios::in | std::ios::binary);
    int i;
    std::string tmp_str;
    int dim;
    try {
        //Reading record 1: Version header
        getline(header_file, tmp_str);
        //Reading record 2: Grid dimension
        header_file >> dim;
        if(dim!=2)
            throw Exception("Wrong dimension of Sgri file. We expect a surface, dimension should be 2.\n");

        getline(header_file, tmp_str);
        //Reading record 3 ... 3+dim: Axis labels + grid value label
        std::vector<std::string> axis_labels(dim);
        for (i=0; i<dim; i++)
            getline(header_file, axis_labels[i]);
        if (((axis_labels[0].find("X") == std::string::npos) && (axis_labels[0].find("x") == std::string::npos)) ||
                ((axis_labels[1].find("Y") == std::string::npos) && (axis_labels[1].find("y") == std::string::npos)))
            throw Exception("Wrong axis labels. First axis should be x-axis, second axis should be y-axis.\n");
        // if((axis_labels[0]!="X" && axis_labels[0] !="x") || (axis_labels[1]!="Y" && axis_labels[1]!="y"))
        //  throw Exception("Wrong axis labels. First axis should be x-axis, second axis should be y-axis.\n");
        getline(header_file, tmp_str);
        //int config = IMISSING;

        //Reading record 4+dim: Number of grids
        int n_grid;
        header_file >> n_grid;
        if (n_grid < 1) {
            throw Exception("Error: Number of grids read from sgri file must be >0");
        }
        getline(header_file, tmp_str);
        //Reading record 5+dim ... 5+dim+ngrid-1: Grid labels

        for (i=0; i<n_grid; i++)
            getline(header_file, tmp_str);

        std::vector<float> d_values1(dim);
        std::vector<float> d_values2(dim);
        std::vector<int>   i_values(dim);
        //Reading record 5+dim+ngrid: Scaling factor of grid values
        for (i=0; i<dim; i++)
            header_file >> d_values1[i];
        getline(header_file,tmp_str);
        //Reading record 6+dim+ngrid: Number of samples in each dir.
        for (i=0; i<dim; i++)
            header_file >> i_values[i];
        getline(header_file,tmp_str);
        //Reading record 7+dim+ngrid: Grid sampling in each dir.
        for (i=0; i<dim; i++) {
            header_file >> d_values2[i];
        }
        getline(header_file,tmp_str);
        //Reading record 8+dim+ngrid: First point coord.
        std::vector<float> min_values(dim);
        for (i=0; i<dim; i++)
        {
            header_file >> min_values[i];
        }
        int nx = 1;
        int ny = 1;

        double dx, dy;
        nx      = i_values[0];
        dx     = d_values2[0];
        ny      = i_values[1];
        dy     = d_values2[1];

        if (nx < 1) {
            throw Exception("Error: Number of samples in X-dir must be >= 1.\n");
        }
        if (ny < 1) {
            throw Exception("Error: Number of samples in Y-dir must be >= 1.\n");
        }

        if (dx <= 0.0) {
            throw Exception("Error: Grid sampling in X-dir must be > 0.0.\n");

        }
        if (dy <= 0.0) {
            throw Exception("Error: Grid sampling in Y-dir must be > 0.0.\n");
        }

        double lx = nx*dx;
        double ly = ny*dy;

        double x_min = min_values[0]-0.5*dx; //In regular grid, these are at value;
        double y_min = min_values[1]-0.5*dy; //in sgri, at corner of cell, hence move.

        header_file >> angle;

        surface.Resize(nx, ny, 0.0);
        surface.SetDimensions(x_min, y_min, lx, ly);

        getline(header_file, tmp_str);
        //Reading record 10+dim+ngrid: Undef value
        float missing_code;
        header_file >> missing_code;
        surface.SetMissingValue(missing_code);
        getline(header_file, tmp_str);
        //Reading record 11+dim+ngrid: Filename of binary file
        std::string bin_file_name;
        getline(header_file, tmp_str);
        if (!tmp_str.empty()) {
            std::locale loc;
            int i = 0;
            char c = tmp_str[i];
            while (!std::isspace(c,loc)) {
                i++;
                c = tmp_str[i];
            }
            tmp_str.erase(tmp_str.begin()+i, tmp_str.end());
        }
        if (tmp_str.empty())
            bin_file_name = NRLib::ReplaceExtension(filename, "Sgri");
        else {
            std::string path = GetPath(filename);
            bin_file_name = path + "/" + tmp_str;
        }
        //Reading record 12+dim+ngrid: Complex values
        bool has_complex;
        header_file >> has_complex;
        if (has_complex != 0 ) {
            throw Exception("Error: Can not read Sgri binary file. Complex values?");
        }

        surface.SetName(GetStem(bin_file_name));

        std::ifstream bin_file;
        OpenRead(bin_file, bin_file_name, std::ios::in | std::ios::binary);
        ReadBinaryFloatArray(bin_file, surface.begin(), surface.GetN());
    }
    catch (Exception& e) {
        throw FileFormatError("Error parsing \"" + filename + "\" as a "
                              "Sgri surface file " + e.what() + "\n");
    }
}