Exemplo n.º 1
0
mat utils::load_matrix(string file_path, int xdim, int ydim){
    mat tmp_mat = randu<mat>(xdim+1, ydim);

    FILE * fin = fopen(file_path.c_str(), "r");
    if (!fin) {
        printf("Can't open file %s to read!", file_path.c_str());
        exit(1);
    }

    int xidx=0;
    char buff[BUFF_SIZE_LONG];
    while (fgets(buff, BUFF_SIZE_LONG-1, fin) != NULL){
        vector<char *> line_segments = split_str(buff, ' ');
        if (line_segments.size() != (unsigned int)ydim){
            printf("Dimension of latent factor can't match with model's.\n");
            exit(1);
        }
        for (unsigned int i=0; i<line_segments.size(); i++)
            tmp_mat(xidx+1, i) = atof(line_segments[i]);
        xidx++;
        memset(buff, 0, BUFF_SIZE_LONG);
    }
   
    if (xidx != xdim){
        cout << "read number of user:"******"original number of user:"******"Number of rows can't match.\n");
        exit(1);
    }

    return tmp_mat;
}
Exemplo n.º 2
0
NOX::Abstract::Group::ReturnType
LOCA::Hopf::MinimallyAugmented::Constraint::
computeDOmega(NOX::Abstract::MultiVector::DenseMatrix& domega)
{
  string callingFunction = 
    "LOCA::Hopf::MinimallyAugmented::Constraint::computeDOmega()";
  NOX::Abstract::Group::ReturnType status;
  NOX::Abstract::Group::ReturnType finalStatus = NOX::Abstract::Group::Ok;

  // Compute sigma, w and v if necessary
  if (!isValidConstraints) {
    status = computeConstraints();
    finalStatus = 
      globalData->locaErrorCheck->combineAndCheckReturnTypes(status, 
							     finalStatus,
							     callingFunction);
  }

  // Compute mass matrix M
  status = grpPtr->computeShiftedMatrix(0.0, 1.0);
  finalStatus = 
    globalData->locaErrorCheck->combineAndCheckReturnTypes(status, 
							   finalStatus,
							   callingFunction);

  // Compute M*v
  Teuchos::RCP<NOX::Abstract::MultiVector> tmp_vector =
    v_vector->clone(NOX::ShapeCopy);
  status = grpPtr->applyShiftedMatrixMultiVector(*v_vector, *tmp_vector);
  finalStatus = 
    globalData->locaErrorCheck->combineAndCheckReturnTypes(status, 
							   finalStatus,
							   callingFunction);

  // Compute u^T*M*v
  NOX::Abstract::MultiVector::DenseMatrix tmp_mat(2,2);
  tmp_vector->multiply(1.0, *w_vector, tmp_mat);

  // Compute domega
  domega(0,0) =   tmp_mat(0,1) - tmp_mat(1,0);
  domega(1,0) = -(tmp_mat(0,0) + tmp_mat(1,1));

  domega.scale(1.0/sigma_scale);

  return finalStatus;
}
CalibrationParams
CalibrationParams::createCalibrationParams(std::string serial, cv::FileNode node) {
    cv::Mat camera_matrix (cv::Mat::eye(3, 3, CV_64FC1));
    cv::Mat dist_coeffs   (5, 1, CV_64FC1, cv::Scalar::all(0));
    cv::Mat r_mat         (3, 3, CV_64FC1, cv::Scalar::all(0));
    cv::Mat t             (3, 1, CV_64FC1, cv::Scalar::all(0));

    node["camera_matrix"]           >> camera_matrix;
    node["distortion_coefficients"] >> dist_coeffs;
    node["rotation"]                >> r_mat;
    node["translation"]             >> t;

    float fx = camera_matrix.ptr<double>(0)[0];
    float fy = camera_matrix.ptr<double>(0)[4];
    float cx = camera_matrix.ptr<double>(0)[2];
    float cy = camera_matrix.ptr<double>(0)[5];

    Eigen::Matrix4f tmp_mat = Eigen::Matrix4f::Identity();

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            tmp_mat(i, j) = r_mat.at<double>(i, j);
        }
    }

    for (int i = 0; i < 3; i++) {
        tmp_mat(i, 3) = t.at<double>(i);
    }

    return {
        serial,
        camera_matrix,
        dist_coeffs,
        r_mat,
        t,
        tmp_mat.inverse(),
        fx,
        fy,
        cx,
        cy
    };
}
Exemplo n.º 4
0
int ImagePreProcess::ShowInvisibleImage(cv::Mat input_mat,int rows)
{
    std::vector<std::string> window_name = {"1","2","3","4","5"};
    for (int i = 0; i < input_mat.rows; i++)
    {
        cv::Mat tmp_mat(input_mat.cols,1,CV_32F);
        input_mat.row(i).copyTo(tmp_mat);
        tmp_mat = tmp_mat.reshape(0, rows);
        cv::normalize(tmp_mat, tmp_mat, 1.0, 0.0, cv::NORM_MINMAX);
        cv::Mat show_mat;
        
        tmp_mat.convertTo(show_mat, CV_8UC1,255.0);
        imshow(window_name[i],show_mat);
    }
    cv::waitKey(0);
    return 0;
}
Exemplo n.º 5
0
Matrix::Matrix (const ::VSMap &in, ::VSMap &out, void * /*user_data_ptr*/, ::VSCore &core, const ::VSAPI &vsapi)
:	vsutl::FilterBase (vsapi, "matrix", ::fmParallel, 0)
,	_clip_src_sptr (vsapi.propGetNode (&in, "clip", 0, 0), vsapi)
,	_vi_in (*_vsapi.getVideoInfo (_clip_src_sptr.get ()))
,	_vi_out (_vi_in)
,	_sse_flag (false)
,	_sse2_flag (false)
,	_avx_flag (false)
,	_avx2_flag (false)
,	_range_set_src_flag (false)
,	_range_set_dst_flag (false)
,	_full_range_src_flag (false)
,	_full_range_dst_flag (false)
/*,	_mat_main ()*/
,	_csp_out (fmtcl::ColorSpaceH265_UNSPECIFIED)
,	_plane_out (get_arg_int (in, out, "singleout", -1))
,	_proc_uptr ()
{
	assert (&in != 0);
	assert (&out != 0);
	assert (&core != 0);
	assert (&vsapi != 0);

	vsutl::CpuOpt  cpu_opt (*this, in, out);
	_sse_flag  = cpu_opt.has_sse ();
	_sse2_flag = cpu_opt.has_sse2 ();
	_avx_flag  = cpu_opt.has_avx ();
	_avx2_flag = cpu_opt.has_avx2 ();

	_proc_uptr = std::unique_ptr <fmtcl::MatrixProc> (new fmtcl::MatrixProc (
		_sse_flag, _sse2_flag, _avx_flag, _avx2_flag
	));

	// Checks the input clip
	if (_vi_in.format == 0)
	{
		throw_inval_arg ("only constant pixel formats are supported.");
	}

	const ::VSFormat &   fmt_src = *_vi_in.format;

	if (fmt_src.subSamplingW != 0 || fmt_src.subSamplingH != 0)
	{
		throw_inval_arg ("input must be 4:4:4.");
	}
	if (fmt_src.numPlanes != NBR_PLANES)
	{
		throw_inval_arg ("greyscale format not supported as input.");
	}
	if (   (   fmt_src.sampleType == ::stInteger
	        && (   fmt_src.bitsPerSample <  8
	            || fmt_src.bitsPerSample > 12)
	        && fmt_src.bitsPerSample != 16)
	    || (   fmt_src.sampleType == ::stFloat
	        && fmt_src.bitsPerSample != 32))
	{
		throw_inval_arg ("pixel bitdepth not supported.");
	}

	if (_plane_out >= NBR_PLANES)
	{
		throw_inval_arg (
			"singleout is a plane index and must be -1 or ranging from 0 to 3."
		);
	}

	// Destination colorspace
	bool           force_col_fam_flag;
	const ::VSFormat *   fmt_dst_ptr = get_output_colorspace (
		in, out, core, fmt_src, _plane_out, force_col_fam_flag
	);

	if (   fmt_dst_ptr->colorFamily != ::cmGray
	    && fmt_dst_ptr->colorFamily != ::cmRGB
	    && fmt_dst_ptr->colorFamily != ::cmYUV
	    && fmt_dst_ptr->colorFamily != ::cmYCoCg)
	{
		throw_inval_arg ("unsupported color family for output.");
	}
	if (   (   fmt_dst_ptr->sampleType == ::stInteger
	        && (   fmt_dst_ptr->bitsPerSample <  8
	            || fmt_dst_ptr->bitsPerSample > 12)
	        && fmt_dst_ptr->bitsPerSample != 16)
	    || (   fmt_dst_ptr->sampleType == ::stFloat
	        && fmt_dst_ptr->bitsPerSample != 32))
	{
		throw_inval_arg ("output bitdepth not supported.");
	}
	if (   fmt_dst_ptr->sampleType    != fmt_src.sampleType
	    || fmt_dst_ptr->bitsPerSample <  fmt_src.bitsPerSample
	    || fmt_dst_ptr->subSamplingW  != fmt_src.subSamplingW
	    || fmt_dst_ptr->subSamplingH  != fmt_src.subSamplingH)
	{
		throw_inval_arg (
			"specified output colorspace is not compatible with the input."
		);
	}

	// Preliminary matrix test: deduce the target color family if unspecified
	if (   ! force_col_fam_flag
	    && fmt_dst_ptr->colorFamily != ::cmGray)
	{
		int               def_count = 0;
		def_count += is_arg_defined (in, "mat" ) ? 1 : 0;
		def_count += is_arg_defined (in, "mats") ? 1 : 0;
		def_count += is_arg_defined (in, "matd") ? 1 : 0;
		if (def_count == 1)
		{
			std::string    tmp_mat (get_arg_str (in, out, "mat", ""));
			tmp_mat = get_arg_str (in, out, "mats", tmp_mat);
			tmp_mat = get_arg_str (in, out, "matd", tmp_mat);

			fmtcl::ColorSpaceH265   tmp_csp =
				find_cs_from_mat_str (*this, tmp_mat, false);

			fmt_dst_ptr = find_dst_col_fam (tmp_csp, fmt_dst_ptr, fmt_src, core);
		}
	}

	// Output format is validated.
	_vi_out.format = fmt_dst_ptr;
	const ::VSFormat &fmt_dst = *fmt_dst_ptr;

	const int      nbr_expected_coef = NBR_PLANES * (NBR_PLANES + 1);

	bool           mat_init_flag = false;

	// Matrix presets
	std::string    mat (get_arg_str (in, out, "mat", ""));
	std::string    mats ((   fmt_src.colorFamily == ::cmYUV ) ? mat : "");
	std::string    matd ((   fmt_dst.colorFamily == ::cmYUV
	                      || fmt_dst.colorFamily == ::cmGray) ? mat : "");
	mats = get_arg_str (in, out, "mats", mats);
	matd = get_arg_str (in, out, "matd", matd);
	if (! mats.empty () || ! matd.empty ())
	{
		fstb::conv_to_lower_case (mats);
		fstb::conv_to_lower_case (matd);
		select_def_mat (mats, fmt_src);
		select_def_mat (matd, fmt_dst);

		fmtcl::Mat4    m2s;
		fmtcl::Mat4    m2d;
		make_mat_from_str (m2s, mats, true);
		make_mat_from_str (m2d, matd, false);
		_csp_out = find_cs_from_mat_str (*this, matd, false);

		_mat_main = m2d * m2s;

		mat_init_flag = true;
	}

	// Range
	_full_range_src_flag = (get_arg_int (
		in, out, "fulls" ,
		vsutl::is_full_range_default (fmt_src) ? 1 : 0,
		0, &_range_set_src_flag
	) != 0);
	_full_range_dst_flag = (get_arg_int (
		in, out, "fulld",
		vsutl::is_full_range_default (fmt_dst) ? 1 : 0,
		0, &_range_set_dst_flag
	) != 0);

	// Custom coefficients
	const int      nbr_coef = _vsapi.propNumElements (&in, "coef");
	const bool     custom_mat_flag = (nbr_coef > 0);
	if (custom_mat_flag)
	{
		if (nbr_coef != nbr_expected_coef)
		{
			throw_inval_arg ("coef has a wrong number of elements.");
		}

		for (int y = 0; y < NBR_PLANES + 1; ++y)
		{
			for (int x = 0; x < NBR_PLANES + 1; ++x)
			{
				_mat_main [y] [x] = (x == y) ? 1 : 0;

				if (   (x < fmt_src.numPlanes || x == NBR_PLANES)
				    &&  y < fmt_dst.numPlanes)
				{
					int            err = 0;
					const int      index = y * (fmt_src.numPlanes + 1) + x;
					const double   c = _vsapi.propGetFloat (&in, "coef", index, &err);
					if (err != 0)
					{
						throw_rt_err ("error while reading the matrix coefficients.");
					}
					_mat_main [y] [x] = c;
				}
			}
		}

		mat_init_flag = true;
	}

	if (! mat_init_flag)
	{
		throw_inval_arg (
			"you must specify a matrix preset or a custom coefficient list."
		);
	}

	prepare_coef (fmt_dst, fmt_src);

	if (_vsapi.getError (&out) != 0)
	{
		throw -1;
	}
}