SampleTrack.cpp
Go to the documentation of this file.
00001 #include "CvTestbed.h"
00002 #include "Camera.h"
00003 #include "TrackerPsa.h"
00004 #include "TrackerStat.h"
00005 #include "TrackerFeatures.h"
00006 #include "Shared.h"
00007 using namespace alvar;
00008 using namespace std;
00009 
00010 int tracker = 0;
00011 bool reset = true;
00012 CvFont font;
00013 std::stringstream calibrationFilename;
00014 
00015 void track_none(IplImage *image, IplImage *img_gray) {
00016 }
00017 
00018 void track_psa(IplImage *image, IplImage *img_gray) {
00019     static TrackerPsa tracker_psa;
00020     static double x, y;
00021     if (reset) {
00022         reset = false;
00023         x = img_gray->width / 2;
00024         y = img_gray->height / 2;
00025         tracker_psa.Track(img_gray); // To reset tracker call it twice
00026     }
00027     tracker_psa.Track(img_gray);
00028     tracker_psa.Compensate(&x, &y);
00029     cvCircle(image, cvPoint(int(x), int(y)), 10, CV_RGB(255,0,0));
00030 }
00031 
00032 void track_psa_rot(IplImage *image, IplImage *img_gray) {
00033     static TrackerPsaRot tracker_psa_rot;
00034     static double x, y, r;
00035     if (reset) {
00036         reset = false;
00037         x = img_gray->width / 2;
00038         y = img_gray->height / 2;
00039         r = 0;
00040         tracker_psa_rot.Track(img_gray); // To reset tracker call it twice
00041     }
00042     tracker_psa_rot.Track(img_gray);
00043     tracker_psa_rot.Compensate(&x, &y);
00044     r += tracker_psa_rot.rotd;
00045     cvCircle(image, cvPoint(int(x), int(y)), 15, CV_RGB(255,0,0));
00046     double r_rad = r*3.1415926535/180;
00047     cvLine(image, cvPoint(int(x), int(y)), cvPoint(int(x-sin(r_rad)*15), int(y+cos(r_rad)*15)), CV_RGB(255,0,0));
00048 }
00049 
00050 void track_stat(IplImage *image, IplImage *img_gray) {
00051     static TrackerStat tracker_stat;
00052     static double x, y;
00053     if (reset) {
00054         reset = false;
00055         x = img_gray->width / 2;
00056         y = img_gray->height / 2;
00057         tracker_stat.Track(img_gray); // To reset tracker call it twice
00058     }
00059     tracker_stat.Track(img_gray);
00060     tracker_stat.Compensate(&x, &y);
00061     cvCircle(image, cvPoint(int(x), int(y)), 10, CV_RGB(0,255,0));
00062 }
00063 
00064 void track_stat_rot(IplImage *image, IplImage *img_gray) {
00065     static TrackerStatRot tracker_stat_rot;
00066     static double x, y, r;
00067     if (reset) {
00068         reset = false;
00069         x = img_gray->width / 2;
00070         y = img_gray->height / 2;
00071         r = 0;
00072         tracker_stat_rot.Track(img_gray); // To reset tracker call it twice
00073     }
00074     tracker_stat_rot.Track(img_gray);
00075     tracker_stat_rot.Compensate(&x, &y);
00076     r += tracker_stat_rot.rotd;
00077     cvCircle(image, cvPoint(int(x), int(y)), 15, CV_RGB(0,255,0));
00078     double r_rad = r*3.1415926535/180;
00079     cvLine(image, cvPoint(int(x), int(y)), cvPoint(int(x-sin(r_rad)*15), int(y+cos(r_rad)*15)), CV_RGB(0,255,0));
00080 }
00081 
00082 void track_features(IplImage *image, IplImage *img_gray) {
00083     static TrackerFeatures tracker_features(200, 190, 0.01, 0, 4, 6);
00084     if (reset) {
00085         reset = false;
00086         tracker_features.Reset();
00087     }
00088     tracker_features.Purge();
00089     tracker_features.Track(img_gray);
00090     for (int i=0; i<tracker_features.feature_count; i++) {
00091         cvCircle(image, 
00092             cvPoint(int(tracker_features.features[i].x), int(tracker_features.features[i].y)), 2, 
00093             CV_RGB(tracker_features.ids[i]%255,(tracker_features.ids[i]*7)%255,(tracker_features.ids[i]*11)%255));
00094     }
00095 }
00096 
00097 const int nof_trackers = 6;
00098 void (*(trackers[nof_trackers]))(IplImage *image, IplImage *img_gray) = {
00099     track_none,
00100     track_psa,
00101     track_psa_rot,
00102     track_stat,
00103     track_stat_rot,
00104     track_features,
00105 };
00106 char tracker_names[nof_trackers][64]={
00107     "No tracker - Press any key to change",
00108     "TrackerPsa",
00109     "TrackerPsaRot",
00110     "TrackerStat",
00111     "TrackerStatRot",
00112     "TrackerFeatures",
00113 };
00114 
00115 void videocallback(IplImage *image)
00116 {
00117     assert(image);
00118     static Camera cam;
00119     static IplImage *img_gray=NULL;
00120     static bool init=true;
00121 
00122     if (init) {
00123         init = false;
00124         img_gray = CvTestbed::Instance().CreateImageWithProto("img_gray", image, 0, 1);
00125         cout<<"Loading calibration: "<<calibrationFilename.str();
00126         if (cam.SetCalib(calibrationFilename.str().c_str(), image->width, image->height)) {
00127             cout<<" [Ok]"<<endl;
00128         } 
00129         else {
00130             cam.SetRes(image->width, image->height);
00131             cout<<" [Fail]"<<endl;
00132         }
00133     }
00134     if (image->nChannels == 1) cvCopy(image, img_gray);
00135     else cvCvtColor(image, img_gray, CV_RGB2GRAY);
00136 
00137     trackers[tracker](image, img_gray);
00138     cvPutText(image, tracker_names[tracker], cvPoint(3, image->height - 20), &font, CV_RGB(255, 255, 255));
00139 }
00140 
00141 int keycallback(int key) {
00142     if ((key == 'r') || (key == 't')) {
00143         reset = true;
00144         return 0;
00145     }
00146     else if ((key == 'n') || (key == ' ')){
00147         tracker = ((tracker+1)%nof_trackers);
00148         reset = true;
00149         return 0;
00150     }
00151     return key;
00152 }
00153 
00154 int main(int argc, char *argv[])
00155 {
00156     try {
00157         // Output usage message
00158         std::string filename(argv[0]);
00159         filename = filename.substr(filename.find_last_of('\\') + 1);
00160         std::cout << "SampleTrack" << std::endl;
00161         std::cout << "===========" << std::endl;
00162         std::cout << std::endl;
00163         std::cout << "Description:" << std::endl;
00164         std::cout << "  This is an example of how to use the 'TrackerPsa', 'TrackerPsaRot'," << std::endl;
00165         std::cout << "  'TrackerFeatures', 'TrackerStat' and 'TrackerStatRot' classes to" << std::endl;
00166         std::cout << "  track the optical flow of the video." << std::endl;
00167         std::cout << std::endl;
00168         std::cout << "Usage:" << std::endl;
00169         std::cout << "  " << filename << " [device]" << std::endl;
00170         std::cout << std::endl;
00171         std::cout << "    device    integer selecting device from enumeration list (default 0)" << std::endl;
00172         std::cout << "              highgui capture devices are prefered" << std::endl;
00173         std::cout << std::endl;
00174         std::cout << "Keyboard Shortcuts:" << std::endl;
00175         std::cout << "  r,t: reset tracker" << std::endl;
00176         std::cout << "  n,space: cycle through tracking algorithms" << std::endl;
00177         std::cout << "  q: quit" << std::endl;
00178         std::cout << std::endl;
00179 
00180         // Initialize font
00181         cvInitFont(&font, CV_FONT_HERSHEY_PLAIN, 1.0, 1.0);
00182 
00183         // Initialise CvTestbed
00184         CvTestbed::Instance().SetVideoCallback(videocallback);
00185         CvTestbed::Instance().SetKeyCallback(keycallback);
00186 
00187         // Enumerate possible capture plugins
00188         CaptureFactory::CapturePluginVector plugins = CaptureFactory::instance()->enumeratePlugins();
00189         if (plugins.size() < 1) {
00190             std::cout << "Could not find any capture plugins." << std::endl;
00191             return 0;
00192         }
00193 
00194         // Display capture plugins
00195         std::cout << "Available Plugins: ";
00196         outputEnumeratedPlugins(plugins);
00197         std::cout << std::endl;
00198 
00199         // Enumerate possible capture devices
00200         CaptureFactory::CaptureDeviceVector devices = CaptureFactory::instance()->enumerateDevices();
00201         if (devices.size() < 1) {
00202             std::cout << "Could not find any capture devices." << std::endl;
00203             return 0;
00204         }
00205 
00206         // Check command line argument for which device to use
00207         int selectedDevice = defaultDevice(devices);
00208         if (argc > 1) {
00209             selectedDevice = atoi(argv[1]);
00210         }
00211         if (selectedDevice >= (int)devices.size()) {
00212             selectedDevice = defaultDevice(devices);
00213         }
00214         
00215         // Display capture devices
00216         std::cout << "Enumerated Capture Devices:" << std::endl;
00217         outputEnumeratedDevices(devices, selectedDevice);
00218         std::cout << std::endl;
00219         
00220         // Create capture object from camera
00221         Capture *cap = CaptureFactory::instance()->createCapture(devices[selectedDevice]);
00222         std::string uniqueName = devices[selectedDevice].uniqueName();
00223 
00224         // Handle capture lifecycle and start video capture
00225         // Note that loadSettings/saveSettings are not supported by all plugins
00226         if (cap) {
00227             std::stringstream settingsFilename;
00228             settingsFilename << "camera_settings_" << uniqueName << ".xml";
00229             calibrationFilename << "camera_calibration_" << uniqueName << ".xml";
00230             
00231             cap->start();
00232             cap->setResolution(640, 480);
00233             
00234             if (cap->loadSettings(settingsFilename.str())) {
00235                 std::cout << "Loading settings: " << settingsFilename.str() << std::endl;
00236             }
00237 
00238             std::stringstream title;
00239             title << "SampleTrack (" << cap->captureDevice().captureType() << ")";
00240 
00241             CvTestbed::Instance().StartVideo(cap, title.str().c_str());
00242 
00243             if (cap->saveSettings(settingsFilename.str())) {
00244                 std::cout << "Saving settings: " << settingsFilename.str() << std::endl;
00245             }
00246 
00247             cap->stop();
00248             delete cap;
00249         }
00250         else if (CvTestbed::Instance().StartVideo(0, argv[0])) {
00251         }
00252         else {
00253             std::cout << "Could not initialize the selected capture backend." << std::endl;
00254         }
00255 
00256         return 0;
00257     }
00258     catch (const std::exception &e) {
00259         std::cout << "Exception: " << e.what() << endl;
00260     }
00261     catch (...) {
00262         std::cout << "Exception: unknown" << std::endl;
00263     }
00264 }


ar_track_alvar
Author(s): Scott Niekum
autogenerated on Thu Jun 6 2019 21:12:54