profilesequence.cc

来自「VC视频对象的跟踪提取原代码(vc视频监控源码)」· CC 代码 · 共 1,022 行 · 第 1/2 页

CC
1,022
字号
	    reset_window(ox,oy,mode);	    break;	    	case TWOKEY:	    pscale = 2.0;	    reset_window(ox,oy,mode);	    break;	    #ifdef USE_GL	case UPARROWKEY:	    pscale *= 1.1;	    reset_window(ox,oy,mode);	    while (qread(&val) != UPARROWKEY)		usleep(2000);	    break;	    	case DOWNARROWKEY:	    pscale /= 1.1;	    reset_window(ox,oy,mode);	    while (qread(&val) != DOWNARROWKEY)		usleep(2000);	    break;	    	case RIGHTARROWKEY:	    mode++;	    if (mode >= pca->data_size)		mode = pca->data_size  - 1;	    theta = 0.0;	    while (qread(&val) != RIGHTARROWKEY)		usleep(2000);	    reset_window(ox,oy,mode); 	    break;	    	case LEFTARROWKEY:	    mode--;	    if (mode < 1)		mode = 1;	    theta = 0.0;	    while (qread(&val) != LEFTARROWKEY)		usleep(2000);	    reset_window(ox,oy,mode); 	    break;#endif   // ifdef USE_GL	case ESCKEY:	default:	    break; // and break again, below.	}	if (dev == ESCKEY)	    break;    }    qreset();    }void ProfileSequence::random_walk(int no_modes){    int i;    realno theta = 0;    realno *max = new realno[no_modes];    int *rnd = new int[no_modes];    for (i = 0; i < no_modes; i++)    {	max[i] = sqrt(pca->get_eval(i+1)) * exp(-0.04 * (i+1));	rnd[i] = (rand() % 5) + 1;    }    Profile old_profile = Profile(*mean);    Profile current_profile;    realno ox = mean->width * 2;    realno oy = mean->height * 2;    while (theta < 12)    {	pca->get_mode(1, no_deviations * max[i] * sin (rnd[0] * theta),		      current_profile);	for (i = 1; i < no_modes; i ++)	    pca->adjust_mode(i+1, no_deviations * max[i] * 			     sin (rnd[i] * theta),			     current_profile);	theta += 0.02;	old_profile.display_shape(ox,oy,false);	current_profile.display_shape(ox,oy,true);	//      sginap(1);	usleep(long(1000000/CLK_TCK));	old_profile = current_profile;    }    old_profile.display_shape(ox,oy,false);    delete [] max;    delete [] rnd;}void ProfileSequence::setup_weights(){    Profile::NO_CONTROL_POINTS = no_ctrl_pnts;    // setup weights to be inversely proportional to variance    weights = NagVector(Profile::NO_CONTROL_POINTS);    mean->mean_distances.square_entries(mean->mean_distances);    mean->mean_squares.subtract(mean->mean_distances,				mean->mean_squares);        realno sum;    realno total;    total = 0;    for (int i = 0; i < Profile::NO_CONTROL_POINTS; i++)    {	sum = 0;	for (int j = 0; j < Profile::NO_CONTROL_POINTS; j++)	    sum += mean->mean_squares.read(i,j);	weights[i] = 1.0 / sum;	total += weights[i];    }    weights /= total;}/*  void ProfileSequence::setup_weights()  {  ifstream data_in(filename);  data_in >> Profile::NO_CONTROL_POINTS;  // setup weights to be inversely proportional to variance  if (weights == NULL) weights = new realno[Profile::NO_CONTROL_POINTS];  if (pca != NULL)   {  pca->weights = weights;  pca->no_weights = Profile::NO_CONTROL_POINTS;  }  for (int i = 0; i < Profile::NO_CONTROL_POINTS; i++)  weights[i] = 0;  realno total = 0;  ProfileSet curr_profiles;  ListNode<Profile> *curr;  Point2* pspline;  while(data_in.peek() != EOF)  {  data_in >> curr_profiles;  for (curr = curr_profiles.first; curr != NULL; curr =  curr->next)  {  pspline = &(curr->dat->spline[0]);  realno ymin = 1e6;int min_y;  realno ymax = -1e6;int max_y;  realno xmin = 1e6;int min_x;  realno xmax = -1e6;int max_x;  for (i = 0; i < Profile::NO_CONTROL_POINTS; i++)  {  if (pspline[i].y < ymin)   {  ymin = pspline[i].y;  min_y = i;  }  if (pspline[i].y > ymax)   {  ymax = pspline[i].y;  max_y = i;  }  if (pspline[i].x < xmin)   {  xmin = pspline[i].x;  min_x = i;  }  if (pspline[i].x > xmax)   {  xmax = pspline[i].x;  max_x = i;  }  }  weights[min_y]++; weights[max_y]++;  //weights[min_x]++; weights[max_x]++;    total +=2;  }  curr_profiles.destroy_all();  }  data_in.close();  for (i = 0; i < Profile::NO_CONTROL_POINTS; i++)  weights[i] /= total;  }*/bool ProfileSequence::align_profile(Profile &current){// FIXME: bool Profile::is_ok() commented out!  problem: access to configuration// FIXME: variables MAX_HEIGHT_TO_WIDTH, MIN_HEIGHT_TO_WIDTH, NO_BLOB_FILTER.// FIXME:     if (!current.is_ok()) // FIXME:     {// FIXME: 	//abort();// FIXME: 	return false;// FIXME:     }    realno s, theta;    //current.recenter(weights);    current.align_to(mean,&current, s, theta, weights.get_data());    if ((fabs(theta > 0.78)) || (s < 0.5))     {	//abort();	return false;    }    return true;}void ProfileSequence::align(char *file_out){    char *file_out_name = NULL;    char *tmp_filename = NULL;        if (file_out == NULL)     {	file_out_name = new char[strlen(filename) + strlen(".aligned____")];	tmp_filename = new char[strlen(filename) + strlen(".temp___")];	sprintf(file_out_name, "%s.aligned", filename);	sprintf(tmp_filename, "%s.temp", filename);    }    else     {	file_out_name = new char[strlen(file_out)];	tmp_filename = new char[strlen(file_out) + strlen(".temp___")];	strcpy(file_out_name, file_out);	sprintf(tmp_filename,"%s.temp", file_out);    }        ifstream data_in(filename);    ofstream data_out(tmp_filename);    char *command_buffer = 	new char[strlen(tmp_filename) + strlen(file_out_name) + strlen("mv_____")];        calculate_mean();    const int MAX_ITERATE = 100;    for (unsigned int lp = 0; lp < MAX_ITERATE; lp++)    {	setup_weights();	mean->normalise();	mean->recenter(weights.get_data());	mean->origin = Point2(0,0);	data_in >> Profile::NO_CONTROL_POINTS;	data_out << Profile::NO_CONTROL_POINTS << " control points " << endl;	ProfileSet curr_profiles;	ListNode<Profile> *curr, *nxt;	MeanProfile *new_mean = new MeanProfile();	new_mean->PCA_flag = 0;	new_mean->reset_mean();	while (data_in.peek() != EOF)	{	    data_in >> curr_profiles;	    curr_profiles.transform(*get_H_root());	    	    for (curr = curr_profiles.first; curr != NULL; curr = nxt)	    {		nxt = curr->next;		if (!(align_profile(*(curr->dat))))		    curr_profiles.destroy(curr);	    }	    new_mean->update(curr_profiles);	    	    curr_profiles.transform(*get_H_inv_root());	    data_out << curr_profiles;	    data_out << "******" << endl;	    curr_profiles.destroy_all();	}	new_mean->normalise();	new_mean->recenter(weights.get_data());	NagVector mean_change;	new_mean->NagVector::subtract(*mean, mean_change);	delete mean;	mean = new_mean;	cout << "Change in means = " << mean_change.length2() << endl;	data_in.close(); data_out.close();		sprintf(command_buffer,"mv %s %s", tmp_filename, file_out_name);	system(command_buffer);		if (mean_change.length2() < 0.01)	    break;	if (lp != (MAX_ITERATE-1))	{	    data_in.open(file_out_name);	    data_out.open(tmp_filename);	}    }    delete [] command_buffer;    delete [] file_out_name;    delete [] tmp_filename;    data_in.close();    data_out.close();}void ProfileSequence::reflect(char *file_out){    char file_out_name[256];    if (file_out == NULL)     {	strncpy(file_out_name, filename, 255);	strncat(file_out_name, ".reflect", 255);    }    else	strncpy(file_out_name, file_out, 255);    ofstream data_out(file_out_name);        ifstream data_in(filename);    data_in >> Profile::NO_CONTROL_POINTS;    data_out << Profile::NO_CONTROL_POINTS << " control points " << endl;    ProfileSet curr_profiles;    ProfileSet reflected;    ListNode<Profile> *curr, *nxt;    while (data_in.peek() != EOF)    {	data_in >> curr_profiles;	curr_profiles.reflect(reflected);	data_out << reflected;	data_out << "******" << endl;	curr_profiles.destroy_all();	reflected.destroy_all();    }    data_in.close();    data_out.close();}void ProfileSequence::track(char *file_out){    char file_out_name[256];    if (file_out == NULL)     {	strncpy(file_out_name, filename, 255);	strncat(file_out_name, ".track", 255);    }    else	strncpy(file_out_name, file_out, 255);    ofstream data_out(file_out_name);    Tracker tracker;    ifstream data_in(filename);    data_in >> Profile::NO_CONTROL_POINTS;    data_out << Profile::NO_CONTROL_POINTS << " control points " << endl;    ProfileSet *new_profiles = NULL;    ProfileSet *old_profiles = NULL;    if (data_in.peek() != EOF)     {	new_profiles = new ProfileSet(); 	data_in >> (*new_profiles);	tracker.process_frame(new_profiles);    }    while (data_in.peek() != EOF)    {	old_profiles = new_profiles;	new_profiles = new ProfileSet();	data_in >> (*new_profiles);	tracker.process_frame(new_profiles);	data_out << (*old_profiles);	data_out << "******" << endl;	old_profiles->destroy_all();	delete old_profiles;    }    new_profiles->destroy_all();    delete new_profiles;    data_in.close();    data_out.close();}void ProfileSequence::halve(char *file_out){    if (pca == NULL)	do_analysis();    char *file_out_name1;    char *file_out_name2;    if (file_out == NULL)    {	int slen = strlen(filename) + strlen(".half.1") + 4;	file_out_name1 = new char[slen];	file_out_name2 = new char[slen];	sprintf(file_out_name1, "%s.half.1", filename);	sprintf(file_out_name2, "%s.half.2", filename);    }    else     {	int slen = strlen(filename) + strlen(".half.1") + 4;	file_out_name1 = new char[slen];	file_out_name2 = new char[slen];	sprintf(file_out_name1,"%s.1", file_out);	sprintf(file_out_name2,"%s.2", file_out);    }        ofstream data_out1(file_out_name1);    ofstream data_out2(file_out_name2);        ifstream data_in(filename);        data_in >> Profile::NO_CONTROL_POINTS;    data_out1 << Profile::NO_CONTROL_POINTS << " control points " << endl;    data_out2 << Profile::NO_CONTROL_POINTS << " control points " << endl;        ProfileSet profiles_in;    ProfileSet profiles_out1;    ProfileSet profiles_out2;        while (data_in.peek() != EOF)    {	data_in >> profiles_in;	for (ListNode<Profile> *curr = profiles_in.first;	     curr != NULL; curr = curr->next)	{	    realno m1 = pca->map_to_mode(0,*curr->dat);	    if (m1 >= 0)		profiles_out1.add_a_copy(curr->dat);	    else		profiles_out2.add_a_copy(curr->dat);	}		data_out1 << profiles_out1 << "******" << endl;	data_out2 << profiles_out2 << "******" << endl;		profiles_in.destroy_all();	profiles_out1.destroy_all();	profiles_out2.destroy_all();    }    data_in.close();    data_out1.close();    data_out2.close();}bool ProfileSequence::get_next_chain(ProfileSet &prf_chain,				     unsigned int &last_ref,				     ifstream &data_in){    prf_chain.destroy_all();        ProfileSet tmp_set;    ListNode<Profile> *curr = NULL;        streampos back_pos;        while ((data_in.peek() != EOF) && (curr == NULL))    {	tmp_set.destroy_all();	back_pos = data_in.tellg();	data_in >> tmp_set;	for (curr = tmp_set.first; (curr != NULL) && 		 (curr->dat->PCA_flag <= last_ref); curr = curr->next);    }        if (curr == NULL)     {	data_in.close();	return false;    }        int curr_ref = curr->dat->PCA_flag;        prf_chain.add_a_copy(curr->dat);    tmp_set.destroy_all();    while ((data_in.peek() != EOF) && (curr != NULL))    {	data_in >> tmp_set;	for (curr = tmp_set.first; (curr != NULL) &&		 (curr->dat->PCA_flag != curr_ref); curr = curr->next);	if (curr != NULL)	    prf_chain.add_a_copy(curr->dat);	tmp_set.destroy_all();    }        data_in.seekg(back_pos);    last_ref = curr_ref;    return true;}void ProfileSequence::center(char *file_out){    char file_out_name[256];    if (file_out == NULL)     {	strncpy(file_out_name, filename, 255);	strncat(file_out_name, ".centered", 255);    }    else	strncpy(file_out_name, file_out, 255);    ofstream data_out(file_out_name);    calculate_mean();    setup_weights();         int curr_ref = -1;    data_out << Profile::NO_CONTROL_POINTS << " control points " << endl;    ProfileSet prf_chain;        ifstream data_in(filename);        while (get_next_chain(prf_chain, curr_ref, data_in))    {	ListNode<Profile> *curr;	for (curr = prf_chain.first; curr != NULL; curr = curr->next)	    curr->dat->recenter(weights.get_data()); 		prf_chain.fit_to_line();	ProfileSet tmp_set;	for (curr = prf_chain.first; curr != NULL;	     curr = curr->next)	{	    assert (tmp_set.no_items == 0);	    tmp_set.add(curr->dat);	    data_out << tmp_set << "******" << endl;	    tmp_set.delete_all();	}    }    prf_chain.destroy_all();    data_in.close();    data_out.close();}} // namespace ReadingPeopleTracker

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?