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
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?
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:
(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 ??
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.
ok ... jumping a number of frame = K works .... REAL SORRY K IS MEANS "ì" iterator in code posted.
but there are a better solution?
May be bestMatch is outside of your image. Try :
and check bestMatch is not empty :
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?
& 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
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 ....