matchTemplate c++ exception first time only
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)
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();
...
is your frame not empty ? sometime 1st frame isn't available immediately than resulting Mat is empty. BTW provide a bit of your code
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.
maybe you are doing some mistake with frames or something else ... a bit of code would help us to help you
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.
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 ?
If you use opencl try to disable it cv::ocl::setUseOpenCL(false);
@JMorgie 1) you have to be careful with extra () and [] when posting links (the comment was ruining everything); 2) use the 10101 button
no opencl anywhere.
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.
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?