SampleIntegralImage.cpp
Go to the documentation of this file.
00001 #include "CvTestbed.h"
00002 #include "IntegralImage.h"
00003 #include "Shared.h"
00004 using namespace alvar;
00005 using namespace std;
00006 
00007 void videocallback(IplImage *image)
00008 {
00009     static IplImage *img_grad = NULL;
00010     static IplImage *img_gray = NULL;
00011     static IplImage *img_ver = NULL;
00012     static IplImage *img_hor = NULL;
00013     static IplImage *img_canny = NULL;
00014     static IntegralImage integ;
00015     static IntegralGradient grad;
00016 
00017     assert(image);
00018     if (img_gray == NULL) {
00019         // Following image is toggled visible using key '0'
00020         img_grad = CvTestbed::Instance().CreateImageWithProto("Gradient", image);
00021         CvTestbed::Instance().ToggleImageVisible(0);
00022         img_gray = CvTestbed::Instance().CreateImageWithProto("Grayscale", image, 0, 1);
00023         img_ver = CvTestbed::Instance().CreateImage("Vertical", cvSize(1,image->height), IPL_DEPTH_8U, 1);
00024         img_hor = CvTestbed::Instance().CreateImage("Horizontal", cvSize(image->width,1), IPL_DEPTH_8U, 1);
00025         img_canny = CvTestbed::Instance().CreateImageWithProto("Canny", image, 0, 1);
00026         img_canny->origin = img_ver->origin = img_hor->origin = image->origin;
00027     }
00028     if (image->nChannels > 1) { 
00029         cvCvtColor(image, img_gray, CV_RGB2GRAY);
00030     } else {
00031         cvCopy(image, img_gray);
00032     }
00033 
00034     // Show PerformanceTimer
00035     //PerformanceTimer timer;
00036     //timer.Start();
00037 
00038     // Update the integral images
00039     integ.Update(img_gray);
00040     grad.Update(img_gray);
00041 
00042     // Whole image projections
00043     integ.GetSubimage(cvRect(0,0,image->width,image->height), img_ver);
00044     integ.GetSubimage(cvRect(0,0,image->width,image->height), img_hor);
00045     for (int y=1; y<image->height; y++) {
00046         cvLine(image, 
00047                cvPoint(int(cvGet2D(img_ver, y-1, 0).val[0]), y-1), 
00048                cvPoint(int(cvGet2D(img_ver, y, 0).val[0]), y), 
00049                CV_RGB(255,0,0));
00050     }
00051     for (int x=1; x<image->width; x++) {
00052         cvLine(image, 
00053                cvPoint(x-1, int(cvGet2D(img_hor, 0, x-1).val[0])), 
00054                cvPoint(x, int(cvGet2D(img_hor, 0, x).val[0])), 
00055                CV_RGB(0,255,0));
00056     }
00057 
00058     // Gradients
00059     // Mark gradients for 4x4 sub-blocks
00060     /*
00061     cvZero(img_grad);
00062     CvRect r = {0,0,4,4};
00063     for (int y=0; y<image->height/4; y++) {
00064         r.y = y*4;
00065         for (int x=0; x<image->width/4; x++) {
00066             r.x = x*4;
00067             double dirx, diry;
00068             grad.GetAveGradient(r, &dirx, &diry);
00069             cvLine(img_grad, cvPoint(r.x+2,r.y+2), cvPoint(r.x+2+int(dirx),r.y+2+int(diry)), CV_RGB(255,0,0));
00070         }
00071     }
00072     */
00073 
00074     // Gradients on canny
00075     cvZero(img_grad);
00076     static int t1=64, t2=192;
00077     cvCreateTrackbar("t1", "Gradient", &t1, 255, NULL);
00078     cvCreateTrackbar("t2", "Gradient", &t2, 255, NULL);
00079     cvCanny(img_gray, img_canny, t1, t2);
00080     CvRect r = {0,0,4,4};
00081     for (r.y=0; r.y<img_canny->height-4; r.y++) {
00082         for (r.x=0; r.x<img_canny->width-4; r.x++) {
00083             if (img_canny->imageData[r.y*img_canny->widthStep+r.x]) {
00084                 double dirx, diry;
00085                 grad.GetAveGradient(r, &dirx, &diry);
00086                 cvLine(img_grad, cvPoint(r.x+2,r.y+2), cvPoint(r.x+2+int(dirx),r.y+2+int(diry)), CV_RGB(0,0,255));
00087                 cvLine(img_grad, cvPoint(r.x+2,r.y+2), cvPoint(r.x+2+int(-diry),r.y+2+int(+dirx)), CV_RGB(255,0,0));
00088                 cvLine(img_grad, cvPoint(r.x+2,r.y+2), cvPoint(r.x+2+int(+diry),r.y+2+int(-dirx)), CV_RGB(255,0,0));
00089             }
00090         }
00091     }
00092 
00093     // Show PerformanceTimer
00094     //cout<<"Processing: "<<1.0 / timer.Stop()<<" fps"<<endl;
00095 }
00096 
00097 int main(int argc, char *argv[])
00098 {
00099     try {
00100         // Output usage message
00101         std::string filename(argv[0]);
00102         filename = filename.substr(filename.find_last_of('\\') + 1);
00103         std::cout << "SampleIntegralImage" << std::endl;
00104         std::cout << "===================" << std::endl;
00105         std::cout << std::endl;
00106         std::cout << "Description:" << std::endl;
00107         std::cout << "  This is an example of how to use the 'IntegralImage' and" << std::endl;
00108         std::cout << "  'IntegralGradient' classes. The vertical (green) and horizontal (red)" << std::endl;
00109         std::cout << "  whole image projections are computed using 'IntegralImage::GetSubimage'" << std::endl;
00110         std::cout << "  and shown in the SampleIntegralImage window. The gradients of the" << std::endl;
00111         std::cout << "  image edges are shown in the Gradient window. The edges are detected" << std::endl;
00112         std::cout << "  using the Canny edge detector where t1 and t2 are parameters for the" << std::endl;
00113         std::cout << "  Canny algorithm. The gradients are drawn in red and their local normals" << std::endl;
00114         std::cout << "  are drawn in blue." << std::endl;
00115         std::cout << std::endl;
00116         std::cout << "Usage:" << std::endl;
00117         std::cout << "  " << filename << " [device]" << std::endl;
00118         std::cout << std::endl;
00119         std::cout << "    device    integer selecting device from enumeration list (default 0)" << std::endl;
00120         std::cout << "              highgui capture devices are prefered" << std::endl;
00121         std::cout << std::endl;
00122         std::cout << "Keyboard Shortcuts:" << std::endl;
00123         std::cout << "  0: show/hide gradient image" << std::endl;
00124         std::cout << "  1: show/hide grayscale image" << std::endl;
00125         std::cout << "  2: show/hide vertical image" << std::endl;
00126         std::cout << "  3: show/hide horizontal image" << std::endl;
00127         std::cout << "  4: show/hide canny image" << std::endl;
00128         std::cout << "  q: quit" << std::endl;
00129         std::cout << std::endl;
00130 
00131         // Initialise CvTestbed
00132         CvTestbed::Instance().SetVideoCallback(videocallback);
00133 
00134         // Enumerate possible capture plugins
00135         CaptureFactory::CapturePluginVector plugins = CaptureFactory::instance()->enumeratePlugins();
00136         if (plugins.size() < 1) {
00137             std::cout << "Could not find any capture plugins." << std::endl;
00138             return 0;
00139         }
00140 
00141         // Display capture plugins
00142         std::cout << "Available Plugins: ";
00143         outputEnumeratedPlugins(plugins);
00144         std::cout << std::endl;
00145 
00146         // Enumerate possible capture devices
00147         CaptureFactory::CaptureDeviceVector devices = CaptureFactory::instance()->enumerateDevices();
00148         if (devices.size() < 1) {
00149             std::cout << "Could not find any capture devices." << std::endl;
00150             return 0;
00151         }
00152 
00153         // Check command line argument for which device to use
00154         int selectedDevice = defaultDevice(devices);
00155         if (argc > 1) {
00156             selectedDevice = atoi(argv[1]);
00157         }
00158         if (selectedDevice >= (int)devices.size()) {
00159             selectedDevice = defaultDevice(devices);
00160         }
00161         
00162         // Display capture devices
00163         std::cout << "Enumerated Capture Devices:" << std::endl;
00164         outputEnumeratedDevices(devices, selectedDevice);
00165         std::cout << std::endl;
00166         
00167         // Create capture object from camera
00168         Capture *cap = CaptureFactory::instance()->createCapture(devices[selectedDevice]);
00169         std::string uniqueName = devices[selectedDevice].uniqueName();
00170 
00171         // Handle capture lifecycle and start video capture
00172         // Note that loadSettings/saveSettings are not supported by all plugins
00173         if (cap) {
00174             std::stringstream settingsFilename;
00175             settingsFilename << "camera_settings_" << uniqueName << ".xml";
00176             
00177             cap->start();
00178             cap->setResolution(640, 480);
00179             
00180             if (cap->loadSettings(settingsFilename.str())) {
00181                 std::cout << "Loading settings: " << settingsFilename.str() << std::endl;
00182             }
00183 
00184             std::stringstream title;
00185             title << "SampleIntegralImage (" << cap->captureDevice().captureType() << ")";
00186 
00187             CvTestbed::Instance().StartVideo(cap, title.str().c_str());
00188 
00189             if (cap->saveSettings(settingsFilename.str())) {
00190                 std::cout << "Saving settings: " << settingsFilename.str() << std::endl;
00191             }
00192 
00193             cap->stop();
00194             delete cap;
00195         }
00196         else if (CvTestbed::Instance().StartVideo(0, argv[0])) {
00197         }
00198         else {
00199             std::cout << "Could not initialize the selected capture backend." << std::endl;
00200         }
00201 
00202         return 0;
00203     }
00204     catch (const std::exception &e) {
00205         std::cout << "Exception: " << e.what() << endl;
00206     }
00207     catch (...) {
00208         std::cout << "Exception: unknown" << std::endl;
00209     }
00210 }


ar_track_alvar
Author(s): Scott Niekum
autogenerated on Sat Dec 28 2013 16:46:15