예제 #1
0
/***************************************************************************************
Deconvolve the B-spline basis functions from the image volume
	vol - a handle for the volume to deconvolve
	c - the coefficients (arising from the deconvolution)
	d - the spline degree
	splinc0, splinc1, splinc2	- functions for 1D deconvolutions
*/
static int vol_coeffs(MAPTYPE *vol, double c[], int d[], void (*splinc[])())
{
	double	p[4], *cp;
	int	np;
	int	i, j, k, n;
	double f[10240];

	/* Check that dimensions don't exceed size of f */
	if (vol->dim[1]>10240 ||vol->dim[2]>10240)
		return(1);

	/* Do a straight copy */
	cp = c;
	for(k=0; k<vol->dim[2]; k++)
	{
		double dk = k+1;
		for(j=0; j<vol->dim[1]; j++)
		{
			double dj = j+1;
			for(i=0;i<vol->dim[0];i++, cp++)
			{
				double di = i+1;
				resample(1,vol,cp,&di,&dj,&dk,0, 0.0);

				/* Not sure how best to handle NaNs */
				if (!mxIsFinite(*cp)) *cp = 0.0;
			}
		}
	}

	/* Deconvolve along the fastest dimension (X) */
	if (d[0]>1 && vol->dim[0]>1)
	{
		if (get_poles(d[0], &np, p)) return(1);
		for(k=0; k<vol->dim[2]; k++)
		{
			/* double dk = k+1; */
			for(j=0; j<vol->dim[1]; j++)
			{
				cp = &c[vol->dim[0]*(j+vol->dim[1]*k)];
				splinc[0](cp, vol->dim[0], p, np);
			}
		}
	}

	/* Deconvolve along the middle dimension (Y) */
	if (d[1]>1 && vol->dim[1]>1)
	{
		if (get_poles(d[1], &np, p)) return(1);
		n =vol->dim[0];
		for(k=0; k<vol->dim[2]; k++)
		{
			for(i=0;i<vol->dim[0];i++)
			{
				cp = &c[i+vol->dim[0]*vol->dim[1]*k];
				for(j=0; j<vol->dim[1]; j++, cp+=n)
					f[j] = *cp;
				splinc[1](f, vol->dim[1], p, np);
				cp = &c[i+vol->dim[0]*vol->dim[1]*k];
				for(j=0; j<vol->dim[1]; j++, cp+=n)
					*cp = f[j];
			}
		}
	}

	/* Deconvolve along the slowest dimension (Z) */
	if (d[2]>1 && vol->dim[2]>1)
	{
		if (get_poles(d[2], &np, p)) return(1);
		n = vol->dim[0]*vol->dim[1];
		for(j=0; j<vol->dim[1]; j++)
		{
			for(i=0;i<vol->dim[0];i++)
			{
				cp = &c[i+vol->dim[0]*j];
				for(k=0; k<vol->dim[2]; k++, cp+=n)
					f[k] = *cp;
				splinc[2](f, vol->dim[2], p, np);
				cp = &c[i+vol->dim[0]*j];
				for(k=0; k<vol->dim[2]; k++, cp+=n)
					*cp = f[k];
			}
		}
	}
	return(0);
}
void PolesExtractor::run() {
	config.stream->open();

    // use the video recorder if necessary
    VideoRecorder* vid_recorder;
    if (config.save_result_video) {
        vid_recorder = new VideoRecorder(config.output_path + config.result_video_name);
    }

    PolesArray all_poles;
    while (!config.stream->has_ended()) {
    	cv::Mat frame = config.stream->get_next_frame();
        int frame_number = config.stream->get_current_frame_number();
        std::cout << "Fr:" << frame_number  << " " << std::flush;
        
        PolesArray curr_poles = get_poles(frame, frame_number);

        // plot all the frames
        for (int i=0; i < curr_poles.size(); ++i) {
        	curr_poles[i].plot(frame);
        }

        if (config.save_result_video) {
            vid_recorder->save_frame(frame);
        }

        
        if (config.manual_validation && curr_poles.size() > 0) {
            std::vector<bool> this_frame_validations;
            this_frame_validations = manual_validation(curr_poles, frame);
            for (int i = 0; i < this_frame_validations.size(); ++i) {
                if (this_frame_validations[i]) {
                    all_poles.push_back(curr_poles[i]);
                }
            }
        }
        else {
            cv::imshow("Poles extracted", frame);
            if ((char)cv::waitKey(10) == 27) break;
        }
    }

    if (config.save_matlab_file) {
        // save in a txt file to import in matlab
        int n_poles = all_poles.size();
        cv::Mat_<double> h_pts(2, n_poles);
        cv::Mat_<double> f_pts(2, n_poles);

        for (int i = 0; i < n_poles; ++i) {
            h_pts(0, i) = all_poles[i].head_point.x;
            h_pts(1, i) = all_poles[i].head_point.y;

            f_pts(0, i) = all_poles[i].feet_point.x;
            f_pts(1, i) = all_poles[i].feet_point.y;
        }

        std::ofstream out_file;
        std::cout << std::endl << "Saving file: " << config.output_path + config.out_eval_matlab_file << std::endl;
        out_file.open(config.output_path + config.out_eval_matlab_file);
        out_file << "h_pts = " << h_pts << ";" << std::endl;
        out_file << "f_pts = " << f_pts << ";" << std::endl; 
        out_file.close();
    }
}