some samples updated according to new CommandLineParser class
authorKirill Kornyakov <no@email>
Sat, 21 May 2011 14:09:03 +0000 (14:09 +0000)
committerKirill Kornyakov <no@email>
Sat, 21 May 2011 14:09:03 +0000 (14:09 +0000)
modules/core/src/cmdparser.cpp [moved from modules/core/src/CommandLineParser.cpp with 95% similarity, mode: 0644]
samples/c/adaptiveskindetector.cpp
samples/c/bgfg_codebook.cpp
samples/c/facedetect.cpp
samples/c/find_obj.cpp
samples/c/find_obj_ferns.cpp
samples/c/latentsvmdetect.cpp
samples/c/mser_sample.cpp
samples/c/one_way_sample.cpp

old mode 100755 (executable)
new mode 100644 (file)
similarity index 95%
rename from modules/core/src/CommandLineParser.cpp
rename to modules/core/src/cmdparser.cpp
index dbef9ad..8745e6b
-#include "precomp.hpp"\r
-\r
-using namespace std;\r
-using namespace cv;\r
-\r
-\r
-vector<string> split_string(const string& str, const string& delimiters)\r
-{\r
-    vector<string> res;\r
-    string::size_type lastPos = str.find_first_not_of(delimiters, 0);\r
-    string::size_type pos     = str.find_first_of(delimiters, lastPos);\r
-    while (string::npos != pos || string::npos != lastPos)\r
-    {\r
-        res.push_back(str.substr(lastPos, pos - lastPos));\r
-        lastPos = str.find_first_not_of(delimiters, pos);\r
-        pos = str.find_first_of(delimiters, lastPos);\r
-    }\r
-\r
-    return res;\r
-}\r
-\r
-void PreprocessArgs(int _argc, const char* _argv[], int& argc, char**& argv)\r
-{\r
-    std::vector<std::string> buffer_vector;\r
-    std::string buffer_string;\r
-    std::string buffer2_string;\r
-    int find_symbol;\r
-\r
-    for (int i = 0; i < _argc; i++)\r
-    {\r
-        buffer_string = _argv[i];\r
-        find_symbol = buffer_string.find('=');\r
-        if (find_symbol == -1)\r
-            buffer_vector.push_back(buffer_string);\r
-        else if (find_symbol == 0 || find_symbol == ((int)buffer_string.length() - 1))\r
-        {\r
-            buffer_string.erase(find_symbol, (find_symbol + 1));\r
-            buffer_vector.push_back(buffer_string);\r
-        }\r
-        else\r
-        {\r
-            buffer2_string = buffer_string;\r
-            buffer_string.erase(find_symbol);\r
-            buffer_vector.push_back(buffer_string);\r
-            buffer2_string.erase(0, find_symbol + 1);\r
-            buffer_vector.push_back(buffer2_string);\r
-        }\r
-    }\r
-\r
-    argc = buffer_vector.size();\r
-    argv = new char* [argc];\r
-    for (int i=0; i < argc; i++)\r
-    {\r
-        argv[i] = new char[buffer_vector[i].length() + 1];\r
-        memcpy(argv[i], buffer_vector[i].c_str(), buffer_vector[i].length() + 1);\r
-    }\r
-}\r
-\r
-CommandLineParser::CommandLineParser(int _argc, const char* _argv[])\r
-{\r
-    std::string cur_name;\r
-    bool was_pushed=false;\r
-    int argc;\r
-    char** argv;\r
-\r
-    PreprocessArgs(_argc, _argv, argc, argv);\r
-\r
-    for(int i=1; i < argc; i++)\r
-    {\r
-        if(!argv[i])\r
-            break;\r
-\r
-        if( (argv[i][0]== '-') && (strlen(argv[i]) > 1) &&\r
-            ((argv[i][1] < '0') || (argv[i][1] > '9'))   )\r
-        {\r
-            if (!cur_name.empty() && !was_pushed)\r
-            {\r
-                data[cur_name].push_back("");\r
-            }\r
-            cur_name=argv[i];\r
-            was_pushed=false;\r
-\r
-            if (data.find(cur_name) != data.end())\r
-            {\r
-                string str_exception = "dublicating parameters for name='" + cur_name + "'";\r
-                CV_Error(CV_StsParseError, str_exception);\r
-            }\r
-            continue;\r
-        }\r
-\r
-        data[cur_name].push_back(argv[i]);\r
-        was_pushed=true;\r
-    }\r
-    if (!cur_name.empty() && !was_pushed)\r
-        data[cur_name].push_back("");\r
-}\r
-\r
-bool CommandLineParser::has(const std::string& keys) const\r
-{\r
-    vector<string> names=split_string(keys, " |");\r
-    for(size_t j=0; j < names.size(); j++)\r
-    {\r
-        if (data.find(names[j])!=data.end())\r
-            return true;\r
-    }\r
-    return false;\r
-}\r
-\r
-template<>\r
-std::vector<std::string> CommandLineParser::getVec<std::string>(const std::string& keys)\r
-{\r
-    vector<string> names=split_string(keys, " |");\r
-\r
-    int found_index=-1;\r
-    for(size_t j=0; j < names.size(); j++)\r
-    {\r
-        const string& cur_name=names[j];\r
-        bool is_cur_found=has(cur_name);\r
-\r
-        if (is_cur_found && (found_index >= 0))\r
-        {\r
-            string str_exception = "dublicating parameters for "\r
-                                   "name='" + names[found_index] + "' and name='"+cur_name+"'";\r
-            CV_Error(CV_StsParseError, str_exception);\r
-        }\r
-\r
-        if (is_cur_found)\r
-            found_index=j;\r
-    }\r
-\r
-    if (found_index<0)\r
-        return vector<string>();\r
-\r
-    return data.find(names[found_index])->second;\r
-}\r
-\r
-template<>\r
-std::string CommandLineParser::fromString<std::string>(const std::string& str)\r
-{\r
-    return str;\r
-}\r
-\r
-template<>\r
-int CommandLineParser::fromString<int>(const std::string& str)\r
-{\r
-    return fromStringNumber<int>(str);\r
-}\r
-\r
-template<>\r
-unsigned int CommandLineParser::fromString<unsigned int>(const std::string& str)\r
-{\r
-    return fromStringNumber<unsigned int>(str);\r
-}\r
-\r
-template<>\r
-double CommandLineParser::fromString<double>(const std::string& str)\r
-{\r
-    return fromStringNumber<double>(str);\r
-}\r
-\r
-template<>\r
-cv::Size CommandLineParser::fromStringsVec<cv::Size>(const std::vector<std::string>& vec_str)\r
-{\r
-    if (vec_str.size() < 2)\r
-        CV_Error(CV_StsParseError, "Cannot convert vector of string to cv::Size : less than two strings");\r
-\r
-    cv::Size res;\r
-    res.width=fromString<int>(vec_str[0]);\r
-    res.height=fromString<int>(vec_str[1]);\r
-\r
-    return res;\r
-}\r
+#include "precomp.hpp"
+
+using namespace std;
+using namespace cv;
+
+
+vector<string> split_string(const string& str, const string& delimiters)
+{
+    vector<string> res;
+    string::size_type lastPos = str.find_first_not_of(delimiters, 0);
+    string::size_type pos     = str.find_first_of(delimiters, lastPos);
+    while (string::npos != pos || string::npos != lastPos)
+    {
+        res.push_back(str.substr(lastPos, pos - lastPos));
+        lastPos = str.find_first_not_of(delimiters, pos);
+        pos = str.find_first_of(delimiters, lastPos);
+    }
+
+    return res;
+}
+
+void PreprocessArgs(int _argc, const char* _argv[], int& argc, char**& argv)
+{
+    std::vector<std::string> buffer_vector;
+    std::string buffer_string;
+    std::string buffer2_string;
+    int find_symbol;
+
+    for (int i = 0; i < _argc; i++)
+    {
+        buffer_string = _argv[i];
+        find_symbol = buffer_string.find('=');
+        if (find_symbol == -1)
+            buffer_vector.push_back(buffer_string);
+        else if (find_symbol == 0 || find_symbol == ((int)buffer_string.length() - 1))
+        {
+            buffer_string.erase(find_symbol, (find_symbol + 1));
+            if(!buffer_string.empty())
+                buffer_vector.push_back(buffer_string);
+        }
+        else
+        {
+            buffer2_string = buffer_string;
+            buffer_string.erase(find_symbol);
+            buffer_vector.push_back(buffer_string);
+            buffer2_string.erase(0, find_symbol + 1);
+            buffer_vector.push_back(buffer2_string);
+        }
+    }
+
+    argc = buffer_vector.size();
+    argv = new char* [argc];
+    for (int i=0; i < argc; i++)
+    {
+        argv[i] = new char[buffer_vector[i].length() + 1];
+        memcpy(argv[i], buffer_vector[i].c_str(), buffer_vector[i].length() + 1);
+    }
+}
+
+CommandLineParser::CommandLineParser(int _argc, const char* _argv[])
+{
+    std::string cur_name;
+    bool was_pushed=false;
+    int argc;
+    char** argv;
+
+    PreprocessArgs(_argc, _argv, argc, argv);
+
+    for(int i=1; i < argc; i++)
+    {
+        if(!argv[i])
+            break;
+
+        if( (argv[i][0]== '-') && (strlen(argv[i]) > 1) &&
+            ((argv[i][1] < '0') || (argv[i][1] > '9'))   )
+        {
+            if (!cur_name.empty() && !was_pushed)
+            {
+                data[cur_name].push_back("");
+            }
+
+            cur_name=argv[i];
+
+            while (cur_name.find('-') == 0)
+            {
+                cur_name.erase(0,1);
+            }
+
+            was_pushed=false;
+
+            if (data.find(cur_name) != data.end())
+            {
+                string str_exception = "dublicating parameters for name='" + cur_name + "'";
+                CV_Error(CV_StsParseError, str_exception);
+            }
+            continue;
+        }
+
+        data[cur_name].push_back(argv[i]);
+        was_pushed=true;
+    }
+    if (!cur_name.empty() && !was_pushed)
+        data[cur_name].push_back("");
+}
+
+bool CommandLineParser::has(const std::string& keys) const
+{
+    vector<string> names=split_string(keys, " |");
+    for(size_t j=0; j < names.size(); j++)
+    {
+        if (data.find(names[j])!=data.end())
+            return true;
+    }
+    return false;
+}
+
+template<>
+std::vector<std::string> CommandLineParser::getVec<std::string>(const std::string& keys)
+{
+    vector<string> names=split_string(keys, " |");
+
+    int found_index=-1;
+    for(size_t j=0; j < names.size(); j++)
+    {
+        const string& cur_name=names[j];
+        bool is_cur_found=has(cur_name);
+
+        if (is_cur_found && (found_index >= 0))
+        {
+            string str_exception = "dublicating parameters for "
+                                   "name='" + names[found_index] + "' and name='"+cur_name+"'";
+            CV_Error(CV_StsParseError, str_exception);
+        }
+
+        if (is_cur_found)
+            found_index=j;
+    }
+
+    if (found_index<0)
+        return vector<string>();
+
+    return data.find(names[found_index])->second;
+}
+
+template<>
+std::string CommandLineParser::fromString<std::string>(const std::string& str)
+{
+    return str;
+}
+
+template<>
+int CommandLineParser::fromString<int>(const std::string& str)
+{
+    return fromStringNumber<int>(str);
+}
+
+template<>
+unsigned int CommandLineParser::fromString<unsigned int>(const std::string& str)
+{
+    return fromStringNumber<unsigned int>(str);
+}
+
+template<>
+double CommandLineParser::fromString<double>(const std::string& str)
+{
+    return fromStringNumber<double>(str);
+}
+
+template<>
+cv::Size CommandLineParser::fromStringsVec<cv::Size>(const std::vector<std::string>& vec_str)
+{
+    if (vec_str.size() < 2)
+        CV_Error(CV_StsParseError, "Cannot convert vector of string to cv::Size : less than two strings");
+
+    cv::Size res;
+    res.width=fromString<int>(vec_str[0]);
+    res.height=fromString<int>(vec_str[1]);
+
+    return res;
+}
+
index f91e5f3..90e4d06 100644 (file)
 //M*/\r
 \r
 \r
+#include <opencv2/core/core.hpp>\r
+#include <opencv2/contrib/contrib.hpp>\r
+#include <opencv2/highgui/highgui.hpp>\r
+\r
 #include <iostream>\r
 #include <cstdio>\r
 #include <cstring>\r
 #include <ctime>\r
-#include <opencv2/contrib/contrib.hpp>\r
-#include <opencv2/highgui/highgui.hpp>\r
 \r
-void help(char **argv)\r
-{\r
-       std::cout << "\nThis program demonstrates the contributed flesh detector CvAdaptiveSkinDetector which can be found in contrib.cpp\n"\r
-                       << "Usage: " << std::endl <<\r
-               argv[0] << " fileMask firstFrame lastFrame" << std::endl << std::endl <<\r
-               "Example: " << std::endl <<\r
-               argv[0] << " C:\\VideoSequences\\sample1\\right_view\\temp_%05d.jpg  0  1000" << std::endl <<\r
-               "       iterates through temp_00000.jpg  to  temp_01000.jpg" << std::endl << std::endl <<\r
-               "If no parameter specified, this application will try to capture from the default Webcam." << std::endl <<\r
-               "Please note: Background should not contain large surfaces with skin tone." <<\r
-               "\n\n ESC will stop\n"\r
-               "Using OpenCV version %s\n" << CV_VERSION << "\n"\r
-               << std::endl;\r
-}\r
+using namespace std;\r
+using namespace cv;\r
 \r
 class ASDFrameHolder\r
 {\r
@@ -159,7 +149,6 @@ void ASDFrameHolder::setImage(IplImage *sourceImage)
 \r
 \r
 //-------------------- ASDFrameSequencer -----------------------//\r
-\r
 ASDFrameSequencer::~ASDFrameSequencer()\r
 {\r
        close();\r
@@ -215,7 +204,6 @@ bool ASDCVFrameSequencer::isOpen()
 \r
 \r
 //-------------------- ASDFrameSequencerWebCam -----------------------//\r
-\r
 bool ASDFrameSequencerWebCam::open(int cameraIndex)\r
 {\r
        close();\r
@@ -335,19 +323,41 @@ void displayBuffer(IplImage *rgbDestImage, IplImage *buffer, int rValue, int gVa
        }\r
 };\r
 \r
-int main(int argc, char** argv )\r
+void help(const char *exe_name)\r
+{\r
+    std::cout << "\nThis program demonstrates the contributed flesh detector CvAdaptiveSkinDetector which can be found in contrib.cpp\n"\r
+            << "Usage: " << std::endl <<\r
+                exe_name << " --fileMask --firstFrame --lastFrame" << std::endl << std::endl <<\r
+        "Example: " << std::endl <<\r
+                exe_name << " --fileMask=C:\\VideoSequences\\sample1\\right_view\\temp_%05d.jpg  --firstFrame=0  --lastFrame=1000" << std::endl <<\r
+        "      iterates through temp_00000.jpg  to  temp_01000.jpg" << std::endl << std::endl <<\r
+        "If no parameter specified, this application will try to capture from the default Webcam." << std::endl <<\r
+        "Please note: Background should not contain large surfaces with skin tone." <<\r
+        "\n\n ESC will stop\n"\r
+        "Using OpenCV version %s\n" << CV_VERSION << "\n"\r
+        << std::endl;\r
+}\r
+\r
+int main(int argc, const char** argv )\r
 {\r
+    help(argv[0]);\r
+\r
+    CommandLineParser parser(argc, argv);\r
+\r
+    string fileMask = parser.get<string>("fileMask");\r
+    int firstFrame = parser.get<int>("firstFrame", 0);\r
+    int lastFrame = parser.get<int>("lastFrame", 0);\r
+\r
        IplImage *img, *filterMask = NULL;\r
        CvAdaptiveSkinDetector filter(1, CvAdaptiveSkinDetector::MORPHING_METHOD_ERODE_DILATE);\r
        ASDFrameSequencer *sequencer;\r
        CvFont base_font;\r
        char caption[2048], s[256], windowName[256];\r
        long int clockTotal = 0, numFrames = 0;\r
-       std::clock_t clock;\r
+    std::clock_t clock;\r
 \r
        if (argc < 4)\r
        {\r
-               help(argv);\r
                sequencer = new ASDFrameSequencerWebCam();\r
                (dynamic_cast<ASDFrameSequencerWebCam*>(sequencer))->open(-1);\r
 \r
@@ -358,8 +368,9 @@ int main(int argc, char** argv )
        }\r
        else\r
        {\r
+        // A sequence of images captured from video source, is stored here\r
                sequencer = new ASDFrameSequencerImageFile();\r
-               (dynamic_cast<ASDFrameSequencerImageFile*>(sequencer))->open(argv[1], std::atoi(argv[2]), std::atoi(argv[3]) ); // A sequence of images captured from video source, is stored here\r
+                (dynamic_cast<ASDFrameSequencerImageFile*>(sequencer))->open(fileMask.c_str(), firstFrame, lastFrame );\r
 \r
        }\r
        std::sprintf(windowName, "%s", "Adaptive Skin Detection Algorithm for Video Sequences");\r
@@ -367,10 +378,6 @@ int main(int argc, char** argv )
        cvNamedWindow(windowName, CV_WINDOW_AUTOSIZE);\r
        cvInitFont( &base_font, CV_FONT_VECTOR0, 0.5, 0.5);\r
 \r
-       // Usage:\r
-       //              c:\>CvASDSample "C:\VideoSequences\sample1\right_view\temp_%05d.jpg" 0 1000\r
-\r
-       std::cout << "Press ESC to stop." << std::endl << std::endl;\r
        while ((img = sequencer->getNextImage()) != 0)\r
        {\r
                numFrames++;\r
index 49542b4..6bbbb80 100644 (file)
 #include <stdlib.h>
 #include <ctype.h>
 
+#include <opencv2/core/core.hpp>
 #include <opencv2/video/background_segm.hpp>
 #include <opencv2/imgproc/imgproc_c.h>
 #include <opencv2/highgui/highgui.hpp>
 
+using namespace std;
+using namespace cv;
+
 //VARIABLES for CODEBOOK METHOD:
 CvBGCodeBookModel* model = 0;
 const int NCHANNELS = 3;
@@ -38,26 +42,28 @@ void help(void)
 {
     printf("\nLearn background and find foreground using simple average and average difference learning method:\n"
                "Originally from the book: Learning OpenCV by O'Reilly press\n"
-        "\nUSAGE:\nbgfg_codebook [--nframes=300] [movie filename, else from camera]\n"
-        "***Keep the focus on the video windows, NOT the consol***\n\n"
-        "INTERACTIVE PARAMETERS:\n"
-        "\tESC,q,Q  - quit the program\n"
-        "\th   - print this help\n"
-        "\tp   - pause toggle\n"
-        "\ts   - single step\n"
-        "\tr   - run mode (single step off)\n"
-        "=== AVG PARAMS ===\n"
-        "\t-    - bump high threshold UP by 0.25\n"
-        "\t=    - bump high threshold DOWN by 0.25\n"
-        "\t[    - bump low threshold UP by 0.25\n"
-        "\t]    - bump low threshold DOWN by 0.25\n"
-        "=== CODEBOOK PARAMS ===\n"
-        "\ty,u,v- only adjust channel 0(y) or 1(u) or 2(v) respectively\n"
-        "\ta   - adjust all 3 channels at once\n"
-        "\tb   - adjust both 2 and 3 at once\n"
-        "\ti,o - bump upper threshold up,down by 1\n"
-        "\tk,l - bump lower threshold up,down by 1\n"
-        "\tSPACE - reset the model\n"
+            "\nUSAGE:\n"
+            "./bgfg_codebook [--nframes=300] \n"
+            "   [--input = movie filename or camera index]\n"
+            "***Keep the focus on the video windows, NOT the consol***\n\n"
+            "INTERACTIVE PARAMETERS:\n"
+            "\tESC,q,Q  - quit the program\n"
+            "\th       - print this help\n"
+            "\tp       - pause toggle\n"
+            "\ts       - single step\n"
+            "\tr       - run mode (single step off)\n"
+            "=== AVG PARAMS ===\n"
+            "\t-    - bump high threshold UP by 0.25\n"
+            "\t=    - bump high threshold DOWN by 0.25\n"
+            "\t[    - bump low threshold UP by 0.25\n"
+            "\t]    - bump low threshold DOWN by 0.25\n"
+            "=== CODEBOOK PARAMS ===\n"
+            "\ty,u,v- only adjust channel 0(y) or 1(u) or 2(v) respectively\n"
+            "\ta       - adjust all 3 channels at once\n"
+            "\tb       - adjust both 2 and 3 at once\n"
+            "\ti,o     - bump upper threshold up,down by 1\n"
+            "\tk,l     - bump lower threshold up,down by 1\n"
+            "\tSPACE - reset the model\n"
         );
 }
 
@@ -65,15 +71,20 @@ void help(void)
 //USAGE:  ch9_background startFrameCollection# endFrameCollection# [movie filename, else from camera]
 //If from AVI, then optionally add HighAvg, LowAvg, HighCB_Y LowCB_Y HighCB_U LowCB_U HighCB_V LowCB_V
 //
-int main(int argc, char** argv)
+int main(int argc, const char** argv)
 {
-    const char* filename = 0;
+    help();
+
+    CommandLineParser parser(argc, argv);
+
+    string inputName = parser.get<string>("input", "0");
+    int nframesToLearnBG = parser.get<int>("nframes", 300);
+
     IplImage* rawImage = 0, *yuvImage = 0; //yuvImage is for codebook method
     IplImage *ImaskCodeBook = 0,*ImaskCodeBookCC = 0;
     CvCapture* capture = 0;
-
     int c, n, nframes = 0;
-    int nframesToLearnBG = 300;
+
 
     model = cvCreateBGCodeBookModel();
     
@@ -87,38 +98,30 @@ int main(int argc, char** argv)
     bool pause = false;
     bool singlestep = false;
 
-    for( n = 1; n < argc; n++ )
+    if( inputName.empty() || (isdigit(inputName.c_str()[0]) && inputName.c_str()[1] == '\0') )
     {
-        static const char* nframesOpt = "--nframes=";
-        if( strncmp(argv[n], nframesOpt, strlen(nframesOpt))==0 )
+        printf("Capture from camera\n");
+        capture = cvCaptureFromCAM( inputName.empty() ? 0 : inputName.c_str()[0] - '0' );
+        int c = inputName.empty() ? 0 : inputName.c_str()[0] - '0' ;
+        if( !capture)
+        {
+            printf ("Capture from CAM %d", c);
+            printf (" didn't work\n");
+        }
+    }
+        else
         {
-            if( sscanf(argv[n] + strlen(nframesOpt), "%d", &nframesToLearnBG) == 0 )
+            printf("Capture from file %s\n",inputName.c_str());
+            capture = cvCreateFileCapture(inputName.c_str());
+            if( !capture)
             {
+                printf ("Capture from file %s", inputName.c_str());
+                printf (" didn't work\n");
                 help();
                 return -1;
             }
-        }
-        else
-            filename = argv[n];
-    }
 
-    if( !filename )
-    {
-        printf("Capture from camera\n");
-        capture = cvCaptureFromCAM( 0 );
-    }
-    else
-    {
-        printf("Capture from file %s\n",filename);
-        capture = cvCreateFileCapture( filename );
-    }
-
-    if( !capture )
-    {
-        printf( "Can not initialize video capturing\n\n" );
-        help();
-        return -1;
-    }
+        }
 
     //MAIN PROCESSING LOOP:
     for(;;)
index 188e6ee..29dae0c 100644 (file)
@@ -34,18 +34,17 @@ int main( int argc, const char** argv )
 
     CommandLineParser parser(argc, argv);
 
-    string cascadeName = parser.get<string>("--cascade", "../../data/haarcascades/haarcascade_frontalface_alt.xml");
+    string cascadeName = parser.get<string>("cascade", "../../data/haarcascades/haarcascade_frontalface_alt.xml");
+    string nestedCascadeName = parser.get<string>("nested-cascade", "../../data/haarcascades/haarcascade_eye_tree_eyeglasses.xml");
+    double scale = parser.get<double>("scale", 1.0);
+    string inputName = parser.get<string>("input", "0"); //read from camera by default
+
     if (!cascadeName.empty())
         cout << "  from which we have cascadeName= " << cascadeName << endl;
 
-    string nestedCascadeName = parser.get<string>("--nested-cascade", "../../data/haarcascades/haarcascade_eye_tree_eyeglasses.xml");
     if (!nestedCascadeName.empty())
         cout << "  from which we have nestedCascadeName= " << nestedCascadeName << endl;
 
-    double scale = parser.get<double>("--scale", 1.0);
-
-    string inputName = parser.get<string>("--input", "0"); //read from camera by default
-
     CvCapture* capture = 0;
     Mat frame, frameCopy, image;
     CascadeClassifier cascade, nestedCascade;
index 1db6f49..3db381a 100644 (file)
@@ -4,6 +4,7 @@
  * Author: Liu Liu
  * liuliu.1987+opencv@gmail.com
  */
+#include <opencv2/core/core.hpp>
 #include <opencv2/objdetect/objdetect.hpp>
 #include <opencv2/features2d/features2d.hpp>
 #include <opencv2/highgui/highgui.hpp>
 #include <vector>
 
 using namespace std;
+using namespace cv;
+
 void help()
 {
-       printf(
-                       "This program demonstrated the use of the SURF Detector and Descriptor using\n"
-                       "either FLANN (fast approx nearst neighbor classification) or brute force matching\n"
-                       "on planar objects.\n"
-                       "Call:\n"
-                       "./find_obj [<object_filename default box.png> <scene_filename default box_in_scene.png>]\n\n"
-                       );
-
+    printf( "This program demonstrated the use of the SURF Detector and Descriptor using\n"
+            "either FLANN (fast approx nearst neighbor classification) or brute force matching\n"
+            "on planar objects.\n"
+            "Call:\n"
+            "./find_obj [--object_filename]=<object_filename, box.png as default> \n"
+                    "[--scene_filename]=<scene_filename box_in_scene.png as default>]\n\n"
+            );
 }
 
 // define whether to use approximate nearest-neighbor search
@@ -209,13 +211,16 @@ locatePlanarObject( const CvSeq* objectKeypoints, const CvSeq* objectDescriptors
     return 1;
 }
 
-int main(int argc, char** argv)
+int main(int argc, const char** argv)
 {
-    const char* object_filename = argc == 3 ? argv[1] : "box.png";
-    const char* scene_filename = argc == 3 ? argv[2] : "box_in_scene.png";
+    help();
+
+    CommandLineParser parser(argc, argv);
+
+    string objectFileName = parser.get<string>("object_filename", "box.png");
+    string sceneFileName = parser.get<string>("scene_filename", "box_in_scene.png");
 
     CvMemStorage* storage = cvCreateMemStorage(0);
-    help();
     cvNamedWindow("Object", 1);
     cvNamedWindow("Object Correspond", 1);
 
@@ -232,13 +237,11 @@ int main(int argc, char** argv)
         {{255,255,255}}
     };
 
-    IplImage* object = cvLoadImage( object_filename, CV_LOAD_IMAGE_GRAYSCALE );
-    IplImage* image = cvLoadImage( scene_filename, CV_LOAD_IMAGE_GRAYSCALE );
+    IplImage* object = cvLoadImage( objectFileName.c_str(), CV_LOAD_IMAGE_GRAYSCALE );
+    IplImage* image = cvLoadImage( sceneFileName.c_str(), CV_LOAD_IMAGE_GRAYSCALE );
     if( !object || !image )
     {
-        fprintf( stderr, "Can not load %s and/or %s\n"
-            "Usage: find_obj [<object_filename> <scene_filename>]\n",
-            object_filename, scene_filename );
+        fprintf( stderr, "Can not load %s and/or %s\n", objectFileName.c_str(), sceneFileName.c_str() );
         exit(-1);
     }
     IplImage* object_color = cvCreateImage(cvGetSize(object), 8, 3);
index 207619f..56c912a 100644 (file)
@@ -9,30 +9,35 @@
 #include <vector>
 
 using namespace cv;
+
 void help()
 {
     printf( "This program shows the use of the \"fern\" plannar PlanarObjectDetector point\n"
-               "descriptor classifier"
-               "Usage:\n"
-               "./find_obj_ferns [<object_filename default: box.png> <scene_filename default:box_in_scene.png>]\n"
-               "\n");
+            "descriptor classifier"
+            "Usage:\n"
+            "./find_obj_ferns [--object_filename]=<object_filename, box.png as default> \n"
+            "[--scene_filename]=<scene_filename box_in_scene.png as default>]\n\n");
 }
-int main(int argc, char** argv)
+
+int main(int argc, const char** argv)
 {
-    const char* object_filename = argc > 1 ? argv[1] : "box.png";
-    const char* scene_filename = argc > 2 ? argv[2] : "box_in_scene.png";
-    int i;
     help();
+
+    CommandLineParser parser(argc, argv);
+
+    string objectFileName = parser.get<string>("object_filename", "box.png");
+    string sceneFileName = parser.get<string>("scene_filename", "box_in_scene.png");
+
     cvNamedWindow("Object", 1);
     cvNamedWindow("Image", 1);
     cvNamedWindow("Object Correspondence", 1);
     
-    Mat object = imread( object_filename, CV_LOAD_IMAGE_GRAYSCALE );
+    Mat object = imread( objectFileName.c_str(), CV_LOAD_IMAGE_GRAYSCALE );
     Mat image;
     
     double imgscale = 1;
 
-    Mat _image = imread( scene_filename, CV_LOAD_IMAGE_GRAYSCALE );
+    Mat _image = imread( sceneFileName.c_str(), CV_LOAD_IMAGE_GRAYSCALE );
     resize(_image, image, Size(), 1./imgscale, 1./imgscale, INTER_CUBIC);
 
 
@@ -40,7 +45,7 @@ int main(int argc, char** argv)
     {
         fprintf( stderr, "Can not load %s and/or %s\n"
                 "Usage: find_obj_ferns [<object_filename> <scene_filename>]\n",
-                object_filename, scene_filename );
+                objectFileName.c_str(), sceneFileName.c_str() );
         exit(-1);
     }
 
@@ -60,7 +65,7 @@ int main(int argc, char** argv)
     vector<KeyPoint> objKeypoints, imgKeypoints;
        PatchGenerator gen(0,256,5,true,0.8,1.2,-CV_PI/2,CV_PI/2,-CV_PI/2,CV_PI/2);
     
-    string model_filename = format("%s_model.xml.gz", object_filename);
+    string model_filename = format("%s_model.xml.gz", objectFileName.c_str());
     printf("Trying to load %s ...\n", model_filename.c_str());
     FileStorage fs(model_filename, FileStorage::READ);
     if( fs.isOpened() )
@@ -106,6 +111,7 @@ int main(int argc, char** argv)
     t = (double)getTickCount() - t;
     printf("%gms\n", t*1000/getTickFrequency());
     
+    int i = 0;
     if( found )
     {
         for( i = 0; i < 4; i++ )
index 9f0ef9c..2361ec2 100644 (file)
@@ -1,5 +1,7 @@
+#include "opencv2/core/core.hpp"\r
 #include "opencv2/objdetect/objdetect.hpp"\r
 #include "opencv2/highgui/highgui.hpp"\r
+\r
 #include <stdio.h>\r
 \r
 #ifdef HAVE_CONFIG_H \r
@@ -13,42 +15,40 @@ using namespace cv;
 \r
 void help()\r
 {\r
-       printf( "This program demonstrated the use of the latentSVM detector.\n"\r
-                       "It reads in a trained object model and then uses that to detect the object in an image\n"\r
-                       "Call:\n"\r
-            "./latentsvmdetect [<image_filename> <model_filename> [<threads_number>]]\n"\r
-                       "  The defaults for image_filename and model_filename are cat.jpg and cat.xml respectively\n"\r
-                       "  Press any key to quit.\n");\r
+    printf( "This program demonstrated the use of the latentSVM detector.\n"\r
+            "It reads in a trained object model and then uses that to detect the object in an image\n"\r
+            "Call:\n"\r
+            "./latentsvmdetect [--image_filename]=<image_filename, cat.jpg as default> \n"\r
+            "       [--model_filename] = <model_filename, cat.xml as default> \n"\r
+            "       [--threads_number] = <number of threads, -1 as default>\n"\r
+            "  The defaults for image_filename and model_filename are cat.jpg and cat.xml respectively\n"\r
+            "  Press any key to quit.\n");\r
 }\r
 \r
-const char* model_filename = "cat.xml";\r
-const char* image_filename = "cat.jpg";\r
-int   tbbNumThreads = -1;\r
 \r
 void detect_and_draw_objects( IplImage* image, CvLatentSvmDetector* detector, int numThreads = -1)\r
 {\r
     CvMemStorage* storage = cvCreateMemStorage(0);\r
     CvSeq* detections = 0;\r
     int i = 0;\r
-       int64 start = 0, finish = 0;\r
+    int64 start = 0, finish = 0;\r
 #ifdef HAVE_TBB\r
     tbb::task_scheduler_init init(tbb::task_scheduler_init::deferred);\r
-       if (numThreads > 0)\r
-       {\r
-               init.initialize(numThreads);\r
+    if (numThreads > 0)\r
+    {\r
+        init.initialize(numThreads);\r
         printf("Number of threads %i\n", numThreads);\r
-       }\r
-       else\r
-       {\r
-               printf("Number of threads is not correct for TBB version");\r
-               return;\r
-       }\r
+    }\r
+    else\r
+    {\r
+        printf("Number of threads is not correct for TBB version");\r
+        return;\r
+    }\r
 #endif\r
-\r
-       start = cvGetTickCount();\r
+    start = cvGetTickCount();\r
     detections = cvLatentSvmDetectObjects(image, detector, storage, 0.5f, numThreads);\r
-       finish = cvGetTickCount();\r
-       printf("detection time = %.3f\n", (float)(finish - start) / (float)(cvGetTickFrequency() * 1000000.0));\r
+    finish = cvGetTickCount();\r
+    printf("detection time = %.3f\n", (float)(finish - start) / (float)(cvGetTickFrequency() * 1000000.0));\r
 \r
 #ifdef HAVE_TBB\r
     init.terminate();\r
@@ -56,43 +56,43 @@ void detect_and_draw_objects( IplImage* image, CvLatentSvmDetector* detector, in
     for( i = 0; i < detections->total; i++ )\r
     {\r
         CvObjectDetection detection = *(CvObjectDetection*)cvGetSeqElem( detections, i );\r
-               CvRect bounding_box = detection.rect;\r
+        CvRect bounding_box = detection.rect;\r
         cvRectangle( image, cvPoint(bounding_box.x, bounding_box.y),\r
                      cvPoint(bounding_box.x + bounding_box.width, \r
-                                                       bounding_box.y + bounding_box.height),\r
+                             bounding_box.y + bounding_box.height),\r
                      CV_RGB(255,0,0), 3 );\r
     }\r
     cvReleaseMemStorage( &storage );\r
 }\r
 \r
-int main(int argc, char* argv[])\r
+int main(int argc, const char* argv[])\r
 {\r
-       help();\r
-       if (argc > 2)\r
-       {\r
-               image_filename = argv[1];\r
-               model_filename = argv[2];\r
-        if (argc > 3)\r
-        {\r
-            tbbNumThreads = atoi(argv[3]);\r
-        }\r
-       }\r
-       IplImage* image = cvLoadImage(image_filename);\r
-       if (!image)\r
-       {\r
-               printf( "Unable to load the image\n"\r
+    help();\r
+\r
+    CommandLineParser parser(argc, argv);\r
+\r
+    string imageFileName = parser.get<string>("image_filename", "cat.jpg");\r
+    string modelFileName = parser.get<string>("model_filename", "cat.xml");\r
+    int tbbNumThreads = parser.get<int>("threads_number", -1);\r
+\r
+    IplImage* image = cvLoadImage(imageFileName.c_str());\r
+    if (!image)\r
+    {\r
+        printf( "Unable to load the image\n"\r
                 "Pass it as the first parameter: latentsvmdetect <path to cat.jpg> <path to cat.xml>\n" );\r
-               return -1;\r
-       }\r
-    CvLatentSvmDetector* detector = cvLoadLatentSvmDetector(model_filename);\r
-       if (!detector)\r
-       {\r
-               printf( "Unable to load the model\n"\r
+        return -1;\r
+    }\r
+    CvLatentSvmDetector* detector = cvLoadLatentSvmDetector(modelFileName.c_str());\r
+    if (!detector)\r
+    {\r
+        printf( "Unable to load the model\n"\r
                 "Pass it as the second parameter: latentsvmdetect <path to cat.jpg> <path to cat.xml>\n" );\r
-               cvReleaseImage( &image );\r
-               return -1;\r
-       }\r
+        cvReleaseImage( &image );\r
+        return -1;\r
+    }\r
+\r
     detect_and_draw_objects( image, detector, tbbNumThreads );\r
+\r
     cvNamedWindow( "test", 0 );\r
     cvShowImage( "test", image );\r
     cvWaitKey(0);\r
@@ -100,5 +100,5 @@ int main(int argc, char* argv[])
     cvReleaseImage( &image );\r
     cvDestroyAllWindows();\r
     \r
-       return 0;\r
+    return 0;\r
 }\r
index 94519d6..5b83004 100644 (file)
@@ -2,17 +2,22 @@
  * Copyright� 2009, Liu Liu All rights reserved.
  */
 
+#include <opencv2/core/core.hpp>
 #include "opencv2/highgui/highgui.hpp"
 #include "opencv2/features2d/features2d.hpp"
 #include "opencv2/imgproc/imgproc_c.h"
 
+#include <iostream>
+
+using namespace std;
+using namespace cv;
 
 void help()
 {
-       printf("\nThis program demonstrates the Maximal Extremal Region interest point detector.\n"
-                       "It finds the most stable (in size) dark and white regions as a threshold is increased.\n"
-                       "\nCall:\n"
-                       "./mser_sample <path_and_image_filename, Default is 'puzzle.png'>\n\n");
+    printf("\nThis program demonstrates the Maximal Extremal Region interest point detector.\n"
+           "It finds the most stable (in size) dark and white regions as a threshold is increased.\n"
+           "\nCall:\n"
+           "./mser_sample [--image_filename] <path_and_image_filename, default is 'puzzle.png'>\n\n");
 }
 
 static CvScalar colors[] = 
@@ -44,90 +49,81 @@ static uchar bcolors[][3] =
 };
 
 
-int main( int argc, char** argv )
+int main( int argc, const char** argv )
 {
-       char path[1024];
-       IplImage* img;
-       help();
-       if (argc!=2)
-       {
-               strcpy(path,"puzzle.png");
-               img = cvLoadImage( path, CV_LOAD_IMAGE_GRAYSCALE );
-               if (!img)
-               {
-                       printf("\nUsage: mser_sample <path_to_image>\n");
-                       return 0;
-               }
-       }
-       else
-       {
-               strcpy(path,argv[1]);
-               img = cvLoadImage( path, CV_LOAD_IMAGE_GRAYSCALE );
-       }
-       
-       if (!img)
-       {
-               printf("Unable to load image %s\n",path);
-               return 0;
-       }
-       IplImage* rsp = cvLoadImage( path, CV_LOAD_IMAGE_COLOR );
-       IplImage* ellipses = cvCloneImage(rsp);
-       cvCvtColor(img,ellipses,CV_GRAY2BGR);
-       CvSeq* contours;
-       CvMemStorage* storage= cvCreateMemStorage();
-       IplImage* hsv = cvCreateImage( cvGetSize( rsp ), IPL_DEPTH_8U, 3 );
-       cvCvtColor( rsp, hsv, CV_BGR2YCrCb );
-       CvMSERParams params = cvMSERParams();//cvMSERParams( 5, 60, cvRound(.2*img->width*img->height), .25, .2 );
-
-       double t = (double)cvGetTickCount();
-       cvExtractMSER( hsv, NULL, &contours, storage, params );
-       t = cvGetTickCount() - t;
-       printf( "MSER extracted %d contours in %g ms.\n", contours->total, t/((double)cvGetTickFrequency()*1000.) );
-       uchar* rsptr = (uchar*)rsp->imageData;
-       // draw mser with different color
-       for ( int i = contours->total-1; i >= 0; i-- )
-       {
-               CvSeq* r = *(CvSeq**)cvGetSeqElem( contours, i );
-               for ( int j = 0; j < r->total; j++ )
-               {
-                       CvPoint* pt = CV_GET_SEQ_ELEM( CvPoint, r, j );
-                       rsptr[pt->x*3+pt->y*rsp->widthStep] = bcolors[i%9][2];
-                       rsptr[pt->x*3+1+pt->y*rsp->widthStep] = bcolors[i%9][1];
-                       rsptr[pt->x*3+2+pt->y*rsp->widthStep] = bcolors[i%9][0];
-               }
-       }
-       // find ellipse ( it seems cvfitellipse2 have error or sth?
-       for ( int i = 0; i < contours->total; i++ )
-       {
-               CvContour* r = *(CvContour**)cvGetSeqElem( contours, i );
-               CvBox2D box = cvFitEllipse2( r );
-               box.angle=(float)CV_PI/2-box.angle;
-               
-               if ( r->color > 0 )
-                       cvEllipseBox( ellipses, box, colors[9], 2 );
-               else
-                       cvEllipseBox( ellipses, box, colors[2], 2 );
-                       
-       }
-
-       cvSaveImage( "rsp.png", rsp );
-
-       cvNamedWindow( "original", 0 );
-       cvShowImage( "original", img );
-       
-       cvNamedWindow( "response", 0 );
-       cvShowImage( "response", rsp );
-
-       cvNamedWindow( "ellipses", 0 );
-       cvShowImage( "ellipses", ellipses );
-
-       cvWaitKey(0);
-
-       cvDestroyWindow( "original" );
-       cvDestroyWindow( "response" );
-       cvDestroyWindow( "ellipses" );
-       cvReleaseImage(&rsp);
-       cvReleaseImage(&img);
-       cvReleaseImage(&ellipses);
-       
+    help();
+
+    CommandLineParser parser(argc, argv);
+
+    string imageFileName = parser.get<string>("image_filename", "puzzle.png");
+
+    IplImage* img;
+
+    img = cvLoadImage( imageFileName.c_str(), CV_LOAD_IMAGE_GRAYSCALE );
+    if (!img)
+    {
+        printf("Unable to load image %s\n",imageFileName.c_str());
+        help();
+        return 0;
+    }
+
+    IplImage* rsp = cvLoadImage( imageFileName.c_str(), CV_LOAD_IMAGE_COLOR );
+    IplImage* ellipses = cvCloneImage(rsp);
+    cvCvtColor(img,ellipses,CV_GRAY2BGR);
+    CvSeq* contours;
+    CvMemStorage* storage= cvCreateMemStorage();
+    IplImage* hsv = cvCreateImage( cvGetSize( rsp ), IPL_DEPTH_8U, 3 );
+    cvCvtColor( rsp, hsv, CV_BGR2YCrCb );
+    CvMSERParams params = cvMSERParams();//cvMSERParams( 5, 60, cvRound(.2*img->width*img->height), .25, .2 );
+
+    double t = (double)cvGetTickCount();
+    cvExtractMSER( hsv, NULL, &contours, storage, params );
+    t = cvGetTickCount() - t;
+    printf( "MSER extracted %d contours in %g ms.\n", contours->total, t/((double)cvGetTickFrequency()*1000.) );
+    uchar* rsptr = (uchar*)rsp->imageData;
+    // draw mser with different color
+    for ( int i = contours->total-1; i >= 0; i-- )
+    {
+        CvSeq* r = *(CvSeq**)cvGetSeqElem( contours, i );
+        for ( int j = 0; j < r->total; j++ )
+        {
+            CvPoint* pt = CV_GET_SEQ_ELEM( CvPoint, r, j );
+            rsptr[pt->x*3+pt->y*rsp->widthStep] = bcolors[i%9][2];
+            rsptr[pt->x*3+1+pt->y*rsp->widthStep] = bcolors[i%9][1];
+            rsptr[pt->x*3+2+pt->y*rsp->widthStep] = bcolors[i%9][0];
+        }
+    }
+    // find ellipse ( it seems cvfitellipse2 have error or sth?
+    for ( int i = 0; i < contours->total; i++ )
+    {
+        CvContour* r = *(CvContour**)cvGetSeqElem( contours, i );
+        CvBox2D box = cvFitEllipse2( r );
+        box.angle=(float)CV_PI/2-box.angle;
+
+        if ( r->color > 0 )
+            cvEllipseBox( ellipses, box, colors[9], 2 );
+        else
+            cvEllipseBox( ellipses, box, colors[2], 2 );
+
+    }
+
+    cvSaveImage( "rsp.png", rsp );
+
+    cvNamedWindow( "original", 0 );
+    cvShowImage( "original", img );
+
+    cvNamedWindow( "response", 0 );
+    cvShowImage( "response", rsp );
+
+    cvNamedWindow( "ellipses", 0 );
+    cvShowImage( "ellipses", ellipses );
+
+    cvWaitKey(0);
+
+    cvDestroyWindow( "original" );
+    cvDestroyWindow( "response" );
+    cvDestroyWindow( "ellipses" );
+    cvReleaseImage(&rsp);
+    cvReleaseImage(&img);
+    cvReleaseImage(&ellipses);
 }
index 817a1d5..9ef6ed2 100644 (file)
@@ -7,18 +7,20 @@
  *
  */
 
+#include <opencv2/core/core.hpp>
 #include "opencv2/imgproc/imgproc.hpp"
 #include "opencv2/features2d/features2d.hpp"
 #include "opencv2/highgui/highgui.hpp"
 #include "opencv2/imgproc/imgproc_c.h"
 
 #include <string>
+
 void help()
 {
-       printf("\nThis program demonstrates the one way interest point descriptor found in features2d.hpp\n"
-                       "Correspondences are drawn\n");
-    printf("Format: \n./one_way_sample [path_to_samples] [image1] [image2]\n");
-    printf("For example: ./one_way_sample ../../../opencv/samples/c scene_l.bmp scene_r.bmp\n");
+    printf("\nThis program demonstrates the one way interest point descriptor found in features2d.hpp\n"
+           "Correspondences are drawn\n");
+    printf("Format: \n./one_way_sample <path_to_samples> <image1> <image2>\n");
+    printf("For example: ./one_way_sample --path=../../../opencv/samples/c --first_image=scene_l.bmp --second_image=scene_r.bmp\n");
 }
 
 using namespace cv;
@@ -26,21 +28,19 @@ using namespace cv;
 IplImage* DrawCorrespondences(IplImage* img1, const vector<KeyPoint>& features1, IplImage* img2,
                               const vector<KeyPoint>& features2, const vector<int>& desc_idx);
 
-int main(int argc, char** argv)
+int main(int argc, const char** argv)
 {
-    const char images_list[] = "one_way_train_images.txt";
-    const CvSize patch_size = cvSize(24, 24);
-    const int pose_count = 50;
+    help();
 
-    if (argc != 3 && argc != 4)
-    {
-       help();
-        return 0;
-    }
+    CommandLineParser parser(argc, argv);
+
+    std::string path_name = parser.get<string>("path", "../../../opencv/samples/c");
+    std::string img1_name = path_name + "/" + parser.get<string>("first_image", "scene_l.bmp");
+    std::string img2_name = path_name + "/" + parser.get<string>("second_image", "scene_r.bmp");
 
-    std::string path_name = argv[1];
-    std::string img1_name = path_name + "/" + std::string(argv[2]);
-    std::string img2_name = path_name + "/" + std::string(argv[3]);
+    const char images_list[] = "one_way_train_images.txt";
+    const CvSize patch_size = cvSize(24, 24);
+    const int pose_count = 1; //50
 
     printf("Reading the images...\n");
     IplImage* img1 = cvLoadImage(img1_name.c_str(), CV_LOAD_IMAGE_GRAYSCALE);