Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

opencv3.0 C++ Match Template mltiple item of same object crash after introduce new one

case 16:{

dest_image.copyTo(carTmplt1);
dest_image.copyTo(carTmplt4);
double minVal; double maxVal, thresholdM = 0.9;;
cv::Point minLoc, maxLoc, matchLoc;
int match_method = CV_TM_CCORR_NORMED;
Size patchSize = Size(50, 50);

UserRectMask = Rect(DataRoiX1, DataRoiY1, DataRoiX4, DataRoiY4);

cvtColor(dest_image, imgHSV, CV_RGB2HSV);

inRange(imgHSV, Scalar(a,b,c), Scalar(a1,b1,c1), imgThresh);
imgThresh.copyTo(desTresh4);

cvtColor(carTmplt1, carTmplt2, CV_RGB2GRAY);

matchTemplate(carTmplt2, carTmplt, carTmplt3, match_method);
normalize(carTmplt3, carTmplt3, 0, 1, cv::NORM_MINMAX, -1, cv::Mat());

maskM = Mat::ones(carTmplt3.size(), CV_8UC1);

       for(int i=0; i<50; i++)
       {
           minMaxLoc(carTmplt3, NULL, NULL, NULL, &maxLoc, maskM);
           Rect bestMatch = Rect(maxLoc, patchSize);
           if((UserRectMask.contains(cv::Point(bestMatch.x, bestMatch.y))) && (UserRectMask.contains(cv::Point((bestMatch.x + bestMatch.height), (bestMatch.y + bestMatch.width))))){
               Mat roiM = maskM(bestMatch);
               roiM.setTo(0);


               if( match_method  == CV_TM_SQDIFF || match_method == CV_TM_SQDIFF_NORMED ) { matchLoc = minLoc;}
               else{ matchLoc = maxLoc;}

               cv::rectangle(carTmplt4, matchLoc, cv::Point(matchLoc.x + carTmplt.cols , matchLoc.y + carTmplt.rows), CV_RGB(255,0,0), 3);
               cv::floodFill(carTmplt4, matchLoc, cv::Scalar(0), 0, cv::Scalar(.1), cv::Scalar(1.));
           }


       }

These code works well ... and detect all 10-15 car in my video .... but if I introduce my hand in front of my camera the program crash ... if introduce my hand I obscured some car ... so I think the previous Roi was loose and maskM too .... there are a system to prevent these? or the only way is use one only frame every time the case:16 become true? Where is my error?

regards gfx

opencv3.0 C++ Match Template mltiple item of same object crash after introduce new one

case 16:{

dest_image.copyTo(carTmplt1);
dest_image.copyTo(carTmplt4);
double minVal; double maxVal, thresholdM = 0.9;;
cv::Point minLoc, maxLoc, matchLoc;
int match_method = CV_TM_CCORR_NORMED;
Size patchSize = Size(50, 50);

UserRectMask = Rect(DataRoiX1, DataRoiY1, DataRoiX4, DataRoiY4);

cvtColor(dest_image, imgHSV, CV_RGB2HSV);

inRange(imgHSV, Scalar(a,b,c), Scalar(a1,b1,c1), imgThresh);
imgThresh.copyTo(desTresh4);

cvtColor(carTmplt1, carTmplt2, CV_RGB2GRAY);

matchTemplate(carTmplt2, carTmplt, carTmplt3, match_method);
normalize(carTmplt3, carTmplt3, 0, 1, cv::NORM_MINMAX, -1, cv::Mat());

maskM = Mat::ones(carTmplt3.size(), CV_8UC1);

       for(int i=0; i<50; i++)
       {
           minMaxLoc(carTmplt3, NULL, NULL, NULL, &maxLoc, maskM);
           Rect bestMatch = Rect(maxLoc, patchSize);
           if((UserRectMask.contains(cv::Point(bestMatch.x, bestMatch.y))) && (UserRectMask.contains(cv::Point((bestMatch.x + bestMatch.height), (bestMatch.y + bestMatch.width))))){
               Mat roiM = maskM(bestMatch);
               roiM.setTo(0);


               if( match_method  == CV_TM_SQDIFF || match_method == CV_TM_SQDIFF_NORMED ) { matchLoc = minLoc;}
               else{ matchLoc = maxLoc;}

               cv::rectangle(carTmplt4, matchLoc, cv::Point(matchLoc.x + carTmplt.cols , matchLoc.y + carTmplt.rows), CV_RGB(255,0,0), 3);
               cv::floodFill(carTmplt4, matchLoc, cv::Scalar(0), 0, cv::Scalar(.1), cv::Scalar(1.));
           }


       }

These code works well ... and detect all 10-15 car in my video .... but if I introduce my hand in front of my camera the program crash ... if introduce my hand I obscured some car ... so I think the previous Roi was loose and maskM too .... there are a system to prevent these? or the only way is use one only frame every time the case:16 become true? Where is my error?

regards gfx