Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Retina Demo - segmentation fault

Hello, I'm trying to run tutorial code from opencv 2.4.5 to retina model and after my little modification it goes like that:

#include <iostream>
#include <cstring>
#include "opencv2/opencv.hpp"

using namespace cv;

static void help(std::string errorMessage)
{
    std::cout<<"Program init error : "<<errorMessage<<std::endl;
    std::cout<<"\nProgram call procedure : retinaDemo [processing mode] [Optional : media target] [Optional LAST parameter: \"log\" to activate retina log sampling]"<<std::endl;
    std::cout<<"\t[processing mode] :"<<std::endl;
    std::cout<<"\t -image : for still image processing"<<std::endl;
    std::cout<<"\t -video : for video stream processing"<<std::endl;
    std::cout<<"\t[Optional : media target] :"<<std::endl;
    std::cout<<"\t if processing an image or video file, then, specify the path and filename of the target to process"<<std::endl;
    std::cout<<"\t leave empty if processing video stream coming from a connected video device"<<std::endl;
    std::cout<<"\t[Optional : activate retina log sampling] : an optional last parameter can be specified for retina spatial log sampling"<<std::endl;
    std::cout<<"\t set \"log\" without quotes to activate this sampling, output frame size will be divided by 4"<<std::endl;
    std::cout<<"\nExamples:"<<std::endl;
    std::cout<<"\t-Image processing : ./retinaDemo -image lena.jpg"<<std::endl;
    std::cout<<"\t-Image processing with log sampling : ./retinaDemo -image lena.jpg log"<<std::endl;
    std::cout<<"\t-Video processing : ./retinaDemo -video myMovie.mp4"<<std::endl;
    std::cout<<"\t-Live video processing : ./retinaDemo -video"<<std::endl;
    std::cout<<"\nPlease start again with new parameters"<<std::endl;
    std::cout<<"****************************************************"<<std::endl;
    std::cout<<" NOTE : this program generates the default retina parameters file 'RetinaDefaultParameters.xml'"<<std::endl;
    std::cout<<" => you can use this to fine tune parameters and load them if you save to file 'RetinaSpecificParameters.xml'"<<std::endl;
}

int main(int argc, char* argv[]) {
    // welcome message
    std::cout<<"****************************************************"<<std::endl;
    std::cout<<"* Retina demonstration : demonstrates the use of is a wrapper class of the Gipsa/Listic Labs retina model."<<std::endl;
    std::cout<<"* This demo will try to load the file 'RetinaSpecificParameters.xml' (if exists).\nTo create it, copy the autogenerated template 'RetinaDefaultParameters.xml'.\nThen twaek it with your own retina parameters."<<std::endl;
    // basic input arguments checking
//    if (argc<2)
//    {
//        help("bad number of parameter");
//        return -1;
//    }
    argc = 3;
    bool useLogSampling = !strcmp(argv[argc-1], "log"); // check if user wants retina log sampling processing
    argv[1] = "-image";
    argv[2] = "lenka.jpg";
//    argv[3] = "log";

    std::string inputMediaType=argv[1];

    // declare the retina input buffer... that will be fed differently in regard of the input media
    Mat inputFrame, image;
    VideoCapture videoCapture; // in case a video media is used, its manager is declared here

    //////////////////////////////////////////////////////////////////////////////
    // checking input media type (still image, video file, live video acquisition)
    if (!strcmp(inputMediaType.c_str(), "-image") && argc >= 3)
    {
        std::cout<<"RetinaDemo: processing image "<<argv[2]<<std::endl;
        // image processing case
        inputFrame = imread(std::string(argv[2]), 0); // load image in RGB mode
//        cvtColor(image, inputFrame, CV_BGR2GRAY);


    }else
        if (!strcmp(inputMediaType.c_str(), "-video"))
        {
            if (argc == 2 || (argc == 3 && useLogSampling)) // attempt to grab images from a video capture device
            {
                videoCapture.open(0);
            }else// attempt to grab images from a video filestream
            {
                std::cout<<"RetinaDemo: processing video stream "<<argv[2]<<std::endl;
                videoCapture.open(argv[2]);
            }

            // grab a first frame to check if everything is ok
            videoCapture>>inputFrame;
        }else
        {
            // bad command parameter
            help("bad command parameter");
            return -1;
        }

    if (inputFrame.empty())
    {
        help("Input media could not be loaded, aborting");
        return -1;
    }


    //////////////////////////////////////////////////////////////////////////////
    // Program start in a try/catch safety context (Retina may throw errors)
    try
    {
        // create a retina instance with default parameters setup, uncomment the initialisation you wanna test
        Ptr<Retina> myRetina;

        // if the last parameter is 'log', then activate log sampling (favour foveal vision and subsamples peripheral vision)
        if (useLogSampling)
        {
             myRetina = new cv::Retina(inputFrame.size(), true, cv::RETINA_COLOR_BAYER, true, 2.0, 10.0);
//            myRetina = &inputFrame.clone();
        }
        else// -> else allocate "classical" retina :
            myRetina = new cv::Retina(inputFrame.size());
//               myRetina = &inputFrame.clone();

        // save default retina parameters file in order to let you see this and maybe modify it and reload using method "setup"
        myRetina->write("RetinaDefaultParameters.xml");

        // load parameters if file exists
//        myRetina->setup("RetinaSpecificParameters.xml");
        myRetina->setup("RetinaDefaultParameters.xml");

//        // reset all retina buffers (imagine you close your eyes for a long time)
        myRetina->clearBuffers();

        // declare retina output buffers
        cv::Mat retinaOutput_parvo;
        cv::Mat retinaOutput_magno;

        // processing loop with no stop condition
        for(;;)
        {
            // if using video stream, then, grabbing a new frame, else, input remains the same
            if (videoCapture.isOpened())
                videoCapture>>inputFrame;

            // run retina filter on the loaded input frame
            myRetina->run(inputFrame);

            // Retrieve and display retina output
            myRetina->getParvo(retinaOutput_parvo);
            myRetina->getMagno(retinaOutput_magno);
            cv::imshow("retina input", inputFrame);
            cv::imshow("Retina Parvo", retinaOutput_parvo);
            cv::imshow("Retina Magno", retinaOutput_magno);
            cv::waitKey(10);
        }
    }catch(cv::Exception e)
    {
        std::cerr<<"Error using Retina : "<<e.what()<<std::endl;
    }

    // Program end message
    std::cout<<"Retina demo end"<<std::endl;

    return 0;
}

So my problem is segemntation fault. Nothing new, but my expectation was working tutorial code. I just changed argv[] to read it manually and argc (I use CodeBlocks) to run it without console and *.xml file name to read the existing one. I can display inputFrame with imshow, when comment line with myRetina->run(inputFrame) where segmentation fault is happening. I thought it is connected with non-8-bit format of input data (according to documentation of run()), but after converting it is still the same. Nor image, neither video works. And there is no other sample codes using OpenCV's Retina.

Any suggestions? I wanted to try it in my object recognition project, but I'm blocked to code further.

Cheers, Ola

Retina Demo - segmentation fault

Hello, I'm trying to run tutorial code from opencv 2.4.5 to retina model and after my little modification it goes like that:

#include <iostream>
#include <cstring>
 #include "opencv2/opencv.hpp"

using namespace cv;

static void help(std::string errorMessage)
{
    std::cout<<"Program init error : "<<errorMessage<<std::endl;
    std::cout<<"\nProgram call procedure : retinaDemo [processing mode] [Optional : media target] [Optional LAST parameter: \"log\" to activate retina log sampling]"<<std::endl;
    std::cout<<"\t[processing mode] :"<<std::endl;
    std::cout<<"\t -image : for still image processing"<<std::endl;
    std::cout<<"\t -video : for video stream processing"<<std::endl;
    std::cout<<"\t[Optional : media target] :"<<std::endl;
    std::cout<<"\t if processing an image or video file, then, specify the path and filename of the target to process"<<std::endl;
    std::cout<<"\t leave empty if processing video stream coming from a connected video device"<<std::endl;
    std::cout<<"\t[Optional : activate retina log sampling] : an optional last parameter can be specified for retina spatial log sampling"<<std::endl;
    std::cout<<"\t set \"log\" without quotes to activate this sampling, output frame size will be divided by 4"<<std::endl;
    std::cout<<"\nExamples:"<<std::endl;
    std::cout<<"\t-Image processing : ./retinaDemo -image lena.jpg"<<std::endl;
    std::cout<<"\t-Image processing with log sampling : ./retinaDemo -image lena.jpg log"<<std::endl;
    std::cout<<"\t-Video processing : ./retinaDemo -video myMovie.mp4"<<std::endl;
    std::cout<<"\t-Live video processing : ./retinaDemo -video"<<std::endl;
    std::cout<<"\nPlease start again with new parameters"<<std::endl;
    std::cout<<"****************************************************"<<std::endl;
    std::cout<<" NOTE : this program generates the default retina parameters file 'RetinaDefaultParameters.xml'"<<std::endl;
    std::cout<<" => you can use this to fine tune parameters and load them if you save to file 'RetinaSpecificParameters.xml'"<<std::endl;
}

int main(int argc, char* argv[]) {
    // welcome message
    std::cout<<"****************************************************"<<std::endl;
    std::cout<<"* Retina demonstration : demonstrates the use of is a wrapper class of the Gipsa/Listic Labs retina model."<<std::endl;
    std::cout<<"* This demo will try to load the file 'RetinaSpecificParameters.xml' (if exists).\nTo create it, copy the autogenerated template 'RetinaDefaultParameters.xml'.\nThen twaek it with your own retina parameters."<<std::endl;
    // basic input arguments checking
//    if (argc<2)
//    {
//        help("bad number of parameter");
//        return -1;
//    }
    argc = 3;
    bool useLogSampling = !strcmp(argv[argc-1], "log"); false; // "log" // check if user wants retina log sampling processing
    argv[1] std::string inputMediaType="-image"; // argv[1]
    string imageOrVideoName = "-image";
    argv[2] = "lenka.jpg";
//    argv[3] = "log";

    std::string inputMediaType=argv[1];
"lenka.jpg"; // argv[2]


    // declare the retina input buffer... that will be fed differently in regard of the input media
    Mat inputFrame, image;
    VideoCapture videoCapture; // in case a video media is used, its manager is declared here

    //////////////////////////////////////////////////////////////////////////////
    // checking input media type (still image, video file, live video acquisition)
    if (!strcmp(inputMediaType.c_str(), "-image") && argc >= 3)
)
    {
        std::cout<<"RetinaDemo: processing image "<<argv[2]<<std::endl;
        // image processing case
"<<imageOrVideoName<<std::endl;
        inputFrame = imread(std::string(argv[2]), imread(imageOrVideoName, 0); // load image in RGB mode
//        cvtColor(image, inputFrame, CV_BGR2GRAY);


    }else
    }else{
        if (!strcmp(inputMediaType.c_str(), "-video"))
        {
            if (argc == 2 || (argc == 3 && useLogSampling)) (useLogSampling) // attempt to grab images from a video capture device
            {
                videoCapture.open(0);
            }else// attempt to grab images from a video filestream
            {
                std::cout<<"RetinaDemo: processing video stream "<<argv[2]<<std::endl;
                videoCapture.open(argv[2]);
"<<imageOrVideoName<<std::endl;
                videoCapture.open(imageOrVideoName);
            }
             // grab a first frame to check if everything is ok
            videoCapture>>inputFrame;
        }else
}
    else
        {
            // bad command parameter
            help("bad command parameter");
            return -1;
        }
     }
    if (inputFrame.empty())
    {
        help("Input media could not be loaded, aborting");
        return -1;
    }


    //////////////////////////////////////////////////////////////////////////////
    // Program start in a try/catch safety context (Retina may throw errors)
    try
    {
        // create a retina instance with default parameters setup, uncomment the initialisation you wanna test
        Ptr<Retina> myRetina;

        // if the last parameter is 'log', then activate log sampling (favour foveal vision and subsamples peripheral vision)
        if (useLogSampling)
        {
             myRetina = new cv::Retina(inputFrame.size(), true, cv::RETINA_COLOR_BAYER, true, 2.0, 10.0);
//            myRetina = &inputFrame.clone();
        }
        else// -> else allocate "classical" retina :
            myRetina = new cv::Retina(inputFrame.size());
//               myRetina = &inputFrame.clone();

        // save default retina parameters file in order to let you see this and maybe modify it and reload using method "setup"
        myRetina->write("RetinaDefaultParameters.xml");

        // load parameters if file exists
//        myRetina->setup("RetinaSpecificParameters.xml");
        myRetina->setup("RetinaDefaultParameters.xml");

//        // reset all retina buffers (imagine you close your eyes for a long time)
        myRetina->clearBuffers();

        // declare retina output buffers
        cv::Mat retinaOutput_parvo;
        cv::Mat retinaOutput_magno;

        // processing loop with no stop condition
        for(;;)
        {
            // if using video stream, then, grabbing a new frame, else, input remains the same
            if (videoCapture.isOpened())
                videoCapture>>inputFrame;

            // run retina filter on the loaded input frame
            myRetina->run(inputFrame);

            // Retrieve and display retina output
            myRetina->getParvo(retinaOutput_parvo);
            myRetina->getMagno(retinaOutput_magno);
            cv::imshow("retina input", inputFrame);
            cv::imshow("Retina Parvo", retinaOutput_parvo);
            cv::imshow("Retina Magno", retinaOutput_magno);
            cv::waitKey(10);
        }
    }catch(cv::Exception e)
    {
        std::cerr<<"Error using Retina : "<<e.what()<<std::endl;
    }

    // Program end message
    std::cout<<"Retina demo end"<<std::endl;

    return 0;
}

So my problem is segemntation fault. Nothing new, but my expectation was working tutorial code. I just changed argv[] to read it manually and argc (I use CodeBlocks) to run it without console and *.xml file name to read the existing one. I can display inputFrame with imshow, when comment line with myRetina->run(inputFrame) where segmentation fault is happening. I thought it is connected with non-8-bit format of input data (according to documentation of run()), but after converting it is still the same. Nor image, neither video works. And there is no other sample codes using OpenCV's Retina.

Any suggestions? I wanted to try it in my object recognition project, but I'm blocked to code further.

Cheers, Ola