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

asked 2017-07-19 12:06:51 -0600

gfx gravatar image

updated 2018-12-27 20:39:32 -0600

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

edit retag flag offensive close merge delete

Comments

UserRectMask.contains(cv::Point((bestMatch.x + bestMatch.height), (bestMatch.y + bestMatch.width) this line is wrong but you are lucky width=height

sholud be

UserRectMask.contains(cv::Point((bestMatch.x + bestMatch.width), (bestMatch.y + bestMatch.height)

Can you give an error message?

LBerger gravatar imageLBerger ( 2017-07-19 12:24:52 -0600 )edit

yes you are in right these is an error but not the important one ... sorry my mistache not show the result error ... here the messages:

OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in Mat, file /home/gfx/opencv-3.0.0/modules/core/src/matrix.cpp, line 495
    terminate called after throwing an instance of 'cv::Exception'
      what():  /home/gfx/opencv-3.0.0/modules/core/src/matrix.cpp:495: error: (-215) 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows in function Mat
my video is tranform from cv::getPerspectiveTransform command ... I don't know if these is a problem ... (more)

gfx gravatar imagegfx ( 2017-07-20 02:49:48 -0600 )edit

(more) ... for sure the Roi location error show when my hand "overlap" the rect that identify the targhet on video ... For sure the problem of Roi's incorrect location appears when my hand overlaps with the rectangles that identify the targhet ... I suppose I should remove the Roi under my hand before the error appears ... but maybe the right one Solution is jumping 50 frames or a number of frames = K ??

gfx gravatar imagegfx ( 2017-07-20 02:58:02 -0600 )edit

Is there a way that you can not use the iterator "K"? I have already seen the thresold makeup but I did not manage to make good results. My is not a picture defined as the picture of a super mario bros screen.

gfx gravatar imagegfx ( 2017-07-20 03:01:02 -0600 )edit

ok ... jumping a number of frame = K works .... REAL SORRY K IS MEANS "ì" iterator in code posted.

but there are a better solution?

gfx gravatar imagegfx ( 2017-07-20 03:15:21 -0600 )edit
1

May be bestMatch is outside of your image. Try :

Rect bestMatch = Rect(maxLoc, patchSize) & Rect(Point(0,0),carTmplt3.size());

and check bestMatch is not empty :

      if(bestMatch.area()>0 && (UserRectMask.contains(cv::Point(bestMatch.x, bestMatch.y))) && (UserRectMask.contains(cv::Point((bestMatch.x + bestMatch.height), (bestMatch.y + bestMatch.width))))){
LBerger gravatar imageLBerger ( 2017-07-20 03:16:26 -0600 )edit

you are in right ... Real Thanks. Nevere use Rect in these way ... you do an "and" of two rect in these way?

Ant how without using thresold trik or iterator (as my code do) these is an other way to identify multiple item of same object?

gfx gravatar imagegfx ( 2017-07-20 03:31:13 -0600 )edit

& is intersection You should use last opencv version too not 3.0

50 means 50 cars? if yes I don't think it's a good idea you should use a threshold but you kill signal with normin_max

LBerger gravatar imageLBerger ( 2017-07-20 03:38:14 -0600 )edit

yes is 50 car .... ok thanks I try. mmm ... about use 3.2 last ... may be next week .... on my program i have a lot of features to change if upload opencv to last one ....

gfx gravatar imagegfx ( 2017-07-20 17:10:54 -0600 )edit