Ask Your Question
0

matchTemplate c++ exception first time only

asked 2016-05-23 11:26:02 -0600

JMorgie gravatar image

updated 2016-05-24 17:33:33 -0600

LorenaGdL gravatar image

running video file with several thousand frames. matchTemplate crashes only on the first time i call it, then runs fine every time afterwards. first time called, Live and template are two different frames. attached screenshots of input images, error messages. have had this behavior for months -- sometimes shows up sometimes program runs without the crash. suggestions what i can look at, tweak, etc.

C:\fakepath\Capture2.GIF(screenshot failed to capture green arrow showing matchTemplate was the culprit)

C:\fakepath\Capture1.GIF

bool E::runNewScan()
{    
    bool rc = false;
    double avgMatch;
    cv::Mat Redd;
    cv::extractChannel( cFrame, Redd, 0);
    if (firstBatch) {   // save template
        firstBatch = false;
        Redd (sectorRect).copyTo(scanTplate);
        if (Debug)
        { cv::imshow("ScanTemplate", scanTplate);   waitKey(keyWait); }
        rc = true;
    }  // first batch only
    else
    {
        //   test for new scan
        try {
                matchTemplate ( Redd, scanTplate, Cresult, CV_TM_CCORR_NORMED);  // correlation
        }
        catch (cv::Exception& e)
        {
            const char* err_what = e.what();
            std::cout << e.file << e.func << e.err << std::endl;
            std::cout << "exception caught: " << err_what << std::endl;
        }

        minMaxLoc(Cresult, &minVal, &maxVal, &minLoc, &maxLoc, Mat());
        avgMatch = maxVal;  // minVal / tmplSize;   // is this a match ?        
    } // else not first batch

and gets called from:

                cv::Mat cFrame, Result;  // in scope of Object

                           ....

          while (!stop) {
            if (!capture.read(inFrame))
                break;
            FrameNo = static_cast<int> (capture.get(CAP_PROP_POS_FRAMES));
            if (FrameNo >= FrameCount)
                stop = true;
            else  // not stop
            {
                if (FrameNo >= rp.firstFrame ) 
                {   // process this one
                    cFrame = inFrame;
                    runNewScan();
                ...
edit retag flag offensive close merge delete

Comments

is your frame not empty ? sometime 1st frame isn't available immediately than resulting Mat is empty. BTW provide a bit of your code

pklab gravatar imagepklab ( 2016-05-23 13:43:23 -0600 )edit

The relevant code is in the screenshot -- matchTemplate only relies on the arguements passed, right?. both of the images also shown, so its not that the images are empty.. btw, the 'live' is the second frame -- the first provided the reference [template] image.

JMorgie gravatar imageJMorgie ( 2016-05-23 13:59:41 -0600 )edit

maybe you are doing some mistake with frames or something else ... a bit of code would help us to help you

pklab gravatar imagepklab ( 2016-05-24 02:02:11 -0600 )edit

i do not understand why the 2nd gif is not showing ... i can show more code but i have 8,000 lines of code scattered over 5 modules and i cant post it all.

JMorgie gravatar imageJMorgie ( 2016-05-24 07:29:24 -0600 )edit

a) how does one post code ? i tried using the 'preformatted text' button but the code got inserted without <cr> or formatting. Also why does the 2nd gif not get linked ?

JMorgie gravatar imageJMorgie ( 2016-05-24 07:47:50 -0600 )edit

If you use opencl try to disable it cv::ocl::setUseOpenCL(false);

LBerger gravatar imageLBerger ( 2016-05-24 07:51:48 -0600 )edit

@JMorgie 1) you have to be careful with extra () and [] when posting links (the comment was ruining everything); 2) use the 10101 button

LorenaGdL gravatar imageLorenaGdL ( 2016-05-24 08:18:20 -0600 )edit

no opencl anywhere.

JMorgie gravatar imageJMorgie ( 2016-05-25 11:29:09 -0600 )edit

my guess is that there is some var in matchTemplate or its subsidiarys that is not properly initialized which is why the first time use fails but after that it proceeds fine. i posted this question to see if one of the developers wanted to try and trace this.

JMorgie gravatar imageJMorgie ( 2016-05-25 11:30:22 -0600 )edit

Without a small example it is difficult to reproduce your problem. Can you post an example with a main reading template and reading many images and tryingto use matchtemplate function?

LBerger gravatar imageLBerger ( 2016-05-25 11:50:02 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-05-26 23:44:51 -0600

JMorgie gravatar image

More Data: I spent the day compiling debug version of openCL [ v3.1 ] into static libraries. Here is what i have learned: * the problem i was stuck with -- matchTemplate crashing -- now works fine, no crash * but there is now an earlier crash long before any call to match Template. * I have tracked this thru to line 274 of opencl_core.cpp; * my program is calling cv::extractChannel to get channel zero of an RGB image [which looks just fine in ImageWatch]. this goes to useOpenCL which then calls HaveOpenCL which then calls opencl_check_fn which as you can see in the gifs declares the function clGetPlatformDs as not present and throws the memory fault.

Here are the gifs: C:\fakepath\useOpenCL_1.GIF OpenCL C:\fakepath\haveOpenCL.GIF haveOpenCL C:\fakepath\opencl_check_fn.GIF check function A C:\fakepath\check_fn_B.GIF check function B C:\fakepath\check_fn_C_smoking_gun.GIF smoking gun

I leave it to the opencl code gurus to figure out what this is crashing here, especially as it wasnt crashing using the non-debug libraries.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-05-23 11:26:02 -0600

Seen: 1,272 times

Last updated: May 26 '16