profilesequence.cc
来自「VC视频对象的跟踪提取原代码(vc视频监控源码)」· CC 代码 · 共 1,022 行 · 第 1/2 页
CC
1,022 行
/* * ProfileSequence.cc * * This class allows a number of manipulations to `ProfileSet's stored in a file. * */#ifdef USE_GL#include <gl/gl.h>#ifndef LINUX// We might have doublebuffering available. Try it...#define MULTI_BUFFER 1#endif#else#include <vogl.h>#include <vodevice.h>#include <X11/Xutil.h>#endif#include <cmath>#include <cstdlib>#include <ctime>#include <cassert>#include "ProfileSequence.h"#include "PCAclass.h"#include "MeanProfile.h"#include "NagVector.h"#include "Tracker.h"namespace ReadingPeopleTracker{// definition and initialisation of static member variablesrealno ProfileSequence::NO_DEVIATIONS = 1.6;// FIXME: Maybe ok to have these former configuration variable constant. Or is it?// Configuration.register_real("DISPLAY_STD_DEVIATIONS", 1.6,// 0.0, 4.0,// &no_deviations, false,// "ProfileSequence", // "The maximum number of std deviations from // the mean shape to the displayed animated shape.");unsigned int ProfileSequence::NO_SHAPE_MODES = 10;// FIXME: Maybe ok to have these former configuration variable constant. Or is it?// Configuration.register_int("NO_SHAPE_MODES", 10,// &NO_SHAPE_MODES, false,// "ProfileSequence",// "The number of shape parameters used to // represent shapes. This is used if you specify a model file and a // sequence file and specify the SHOW_SHAPES option.");unsigned int ProfileSequence::WAIT_TIME = 50;// FIXME: Maybe ok to have these former configuration variable constant. Or is it?// Configuration.register_int("WAIT_TIME", 50,// &WAIT_TIME, false,// "ProfileSequence",// "A delay factor (in msec) used when displaying a set of shapes");ProfileSequence::ProfileSequence(char *name, char *model_name) : weights(){ if (name != NULL) { strncpy(filename, name, 99); ifstream data_in(filename); data_in >> no_ctrl_pnts; Profile::NO_CONTROL_POINTS = no_ctrl_pnts; data_in.close(); } else filename[0] = '\0'; if (model_name != NULL) { pca = new PCAclass(model_name); Profile::NO_CONTROL_POINTS = pca->data_size / 2; if (Profile::draw_arrow == true) Profile::NO_CONTROL_POINTS--; mean = new MeanProfile(); mean->PCA_flag = 0; pca->mean.copy(*mean); mean->get_size(); pca->weights.copy(weights); } else { mean = new MeanProfile(); mean->PCA_flag = 0; pca = NULL; } setup_H_metric(mean->get_data_size()); // initialise pscale pscale = 1.0;}ProfileSequence::~ProfileSequence(){ delete mean; if (pca != NULL) delete pca;}void ProfileSequence::calculate_mean(){ ifstream data_in(filename); ProfileSet curr_profiles; mean->reset_mean(); data_in >> Profile::NO_CONTROL_POINTS; while (data_in.peek() != EOF) { data_in >> curr_profiles; // remove profiles that are not ok curr_profiles.keep_ok(); // transform to "V" space curr_profiles.transform(*get_H_root()); mean->update(curr_profiles); curr_profiles.destroy_all(); } data_in.close();// setup_H_metric(*mean);// get_H_root()->multiply(*mean);}unsigned int ProfileSequence::count_profiles(){ ifstream data_in(filename); data_in >> Profile::NO_CONTROL_POINTS; unsigned int no_items = 0; ProfileSet curr_profiles; while (data_in.peek() != EOF) { data_in >> curr_profiles; no_items += curr_profiles.no_items; curr_profiles.destroy_all(); } data_in.close(); return no_items;}void ProfileSequence::convert_to_matrix(NagMatrix &training_matrix, realno use_fraction){ normal_random(); // initialise seed if necessary unsigned short seed_buff[3]; unsigned short *curr_seed = seed48(seed_buff); // copy over the current seed into the buffer memcpy(&seed_buff[0], curr_seed, sizeof(unsigned short[3])); seed48(seed_buff); ifstream data_in(filename); data_in >> Profile::NO_CONTROL_POINTS; cinfo << " counting training set " << endl; unsigned int no_items = 0; ProfileSet curr_profiles; while (data_in.peek() != EOF) { data_in >> curr_profiles; for (ListNode<Profile> *curr = curr_profiles.first; curr != NULL; curr = curr->next) if (drand48() <= use_fraction) no_items++; curr_profiles.destroy_all(); } data_in.close(); cinfo << " found " << no_items << " training items " << endl; Profile dummy; training_matrix.reconstruct(dummy.get_data_size(), no_items); ifstream data_in2(filename); data_in2 >> Profile::NO_CONTROL_POINTS; int indx = 0; seed48(seed_buff); // reset the seed to buffer while (data_in2.peek() != EOF) { data_in2 >> curr_profiles; curr_profiles.transform(*get_H_root()); for (ListNode<Profile> *curr = curr_profiles.first; curr != NULL; curr = curr->next) if (drand48() <= use_fraction) curr->dat->copy(training_matrix.column(indx++)); curr_profiles.destroy_all(); } } void ProfileSequence::do_analysis(){ calculate_mean(); if (pca != NULL) delete pca; pca = new PCAclass(mean->get_data_size()); setup_weights(); weights.copy(pca->weights); // mean->recenter(weights.get_data()); mean->direction = Point2(0,0); // don't need to copy over mean // it will be recalculated ! // mean->NagVector::copy(pca->mean); ifstream data_in(filename); ProfileSet *curr_profiles = new ProfileSet(); ProfileSet *old_profiles = NULL; ListNode<Profile> *curr; data_in >> Profile::NO_CONTROL_POINTS; cinfo << " calculating covariance .." << flush; bool do_noise_model = false; while(data_in.peek() != EOF) { data_in >> *curr_profiles; // remove profiles that are not ok curr_profiles->keep_ok(); curr_profiles->transform(*get_H_root()); if (((pca->no_processed) % 10) == 0) cinfo << "." << flush; for (curr = curr_profiles->first; curr != NULL; curr = curr->next) { curr->dat->direction.normalise(); //align_profile(*curr->dat); if (curr->dat->PCA_flag == 0) pca->process_cov(*curr->dat); cdebug << " ProfileSequence::do_analysis(): assign a value to PCA_flag??? " << endl; if ((old_profiles != NULL) && (old_profiles->first != NULL) && (curr->dat->PCA_flag > 0)) { ListNode<Profile> *oldp; for (oldp = old_profiles->first; (oldp != NULL) && (oldp->dat->PCA_flag != curr->dat->PCA_flag); oldp = oldp->next) { // nothing }; if (oldp != NULL) { pca->process_cov(*curr->dat, *oldp->dat); do_noise_model = true; } } } if (old_profiles != NULL) { old_profiles->destroy_all(); delete old_profiles; } old_profiles = curr_profiles; curr_profiles = new ProfileSet(); } old_profiles->destroy_all(); delete old_profiles; delete curr_profiles; data_in.close(); cinfo << endl << pca->no_processed << " items processed " << endl; cinfo << " finding modes ..." << endl; pca->restore_covariance(); pca->get_evals(); pca->mean.NagVector::copy(*mean); if (do_noise_model) pca->get_noise_model();}void ProfileSequence::display_shapes(Point2 shape_origin) { ifstream data_in(filename); ProfileSet curr_profiles; data_in >> Profile::NO_CONTROL_POINTS; clear(); ListNode<Profile> *curr; while (!data_in.eof()) { data_in >> curr_profiles; for (curr = curr_profiles.first; curr != NULL; curr = curr->next) {// 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 (curr->dat->is_ok())// FIXME: { if (pca != NULL) { NagVector shape_parms(NO_SHAPE_MODES); //DONT ALIGN -- ASSUME profiles are in // model frame // otherwise components are not available!!! align_profile(*curr->dat); pca->map_to_model(NO_SHAPE_MODES, *curr->dat, shape_parms); for (unsigned int i = 0; i < NO_SHAPE_MODES; i++) cout << shape_parms[i] << " "; cout << endl; } if (pca != NULL) pca->map_to_shape(NO_SHAPE_MODES, *curr->dat, 1e12); curr->dat->display_shape(shape_origin.x, shape_origin.y); // sginap(WAIT_TIME);#ifdef LINUX // usleep uses microseconds as input usleep(long(WAIT_TIME*1000));#else usleep(long((WAIT_TIME/CLK_TCK)*1000000));#endif// FIXME: } } RGBcolor(0,0,0); clear(); curr_profiles.destroy_all(); } data_in.close();}void ProfileSequence::edit_shapes(Point2 shape_origin, char *file_out_name){ ofstream data_out(file_out_name); ifstream data_in(filename); ProfileSet curr_profiles; ProfileSet new_profiles; data_in >> Profile::NO_CONTROL_POINTS; data_out << Profile::NO_CONTROL_POINTS << " control points " << endl; unsigned char input; // RGBcolor(0,0,0);// clear(); ListNode<Profile> *curr; while (!data_in.eof()) { data_in >> curr_profiles; for (curr = curr_profiles.first; curr != NULL; curr = curr->next) { RGBcolor(0,0,0); clear(); curr->dat->display_shape(shape_origin.x, shape_origin.y); gflush(); input = getchar(); if ((input != 'n') && (input != 'N')) { new_profiles.add_a_copy(curr->dat); cdebug << "."; } else cdebug << "X"; } data_out << new_profiles; data_out << "********************" << endl; curr_profiles.destroy_all(); new_profiles.destroy_all(); } data_in.close(); data_out.close(); }void ProfileSequence::reset_window(realno &ox, realno &oy, int mode){ Int32 x1; Int32 y1; Int32 dx; Int32 dy; getorigin(&x1, &y1); getsize(&dx, &dy); viewport(0, dx-1, 0, dy-1); ortho2(-0.5,(dx / pscale) + 0.5,-0.5, (dy /pscale) +0.5); ox = (dx / (2.0 * pscale)); oy = (dy / (2.0 * pscale));#ifdef MULTI_BUFFER doublebuffer();#endif gconfig(); unsigned char title_str[20]; sprintf(title_str,"mode %d", mode); #ifdef MULTI_BUFFER for (int i = 0; i < 2; i++) {#endif viewport(0, dx-1, 0, dy-1); RGBcolor(0,0,0); clear(); RGBcolor(255,255,255); ortho2(-0.5,dx + 0.5,-0.5, dy +0.5); cmov2i(32,32); unsigned charstr(title_str); ortho2(-0.5,(dx / pscale) + 0.5,-0.5, (dy /pscale) +0.5);#ifdef MULTI_BUFFER swapbuffers(); }#endif}void ProfileSequence::display_mode(int &mode){ if (mode < 1) mode = 1; if (mode > (2 * Profile::NO_CONTROL_POINTS)) mode = 2 * Profile::NO_CONTROL_POINTS; realno theta = 0; realno small_ang = (2 * 3.14159265358979323) / 160; realno max = no_deviations * sqrt(pca->get_eval(mode)); Profile old_profile = Profile(*mean); Profile current_profile = Profile(*mean); current_profile.get_size(); current_profile.draw_in_colour = true; realno ox,oy; prefsize(300,400); foreground(); winopen("PCA");#ifdef USE_GL RGBmode(); gconfig();// RGBcolor(0,0,0);// clear();#endif winconstraints(); reset_window(ox,oy,mode); qdevice(REDRAW); qdevice(ESCKEY); qdevice(ONEKEY); qdevice(TWOKEY); qdevice(AKEY); #ifdef USE_GL qdevice(UPARROWKEY); qdevice(DOWNARROWKEY); qdevice(RIGHTARROWKEY); qdevice(LEFTARROWKEY);#endif short val; long dev; qreset(); int old_mode = mode; while (mode != 0) { unsigned int count = 0; while (qtest() == 0) // waiting for X / GL events { max = no_deviations * sqrt(pca->get_eval(mode)); pca->get_mode(mode, max * sin (theta), current_profile); theta += small_ang; if (count > 0) old_profile.display_shape(ox,oy,false); current_profile.display_shape(ox,oy,true); old_profile = current_profile; count++; usleep(10000); } dev = qread(&val); if (mode != old_mode) { old_mode = mode; reset_window(ox, oy, mode); } switch (dev) { case REDRAW: reset_window(ox,oy,mode); current_profile.display_shape(ox,oy,true);#ifdef MULTI_BUFFER swapbuffers();#endif break; case AKEY: reset_window(ox,oy,mode); current_profile.display_shape(ox,oy,true);#ifdef MULTI_BUFFER swapbuffers();#endif break; case ONEKEY: pscale = 1.0;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?