void BallMovementCalculator::PrintCalculationData(Mat& image, vector<TimedDetectedBall> relevantSamples) { char message[256]; for (int i = 0 ; i < NUM_OF_SAMPLES ; i++) { TimedDetectedBall currentBall = m_SamplesArray[i]; circle(image, currentBall.Location, 5, Scalar(255,0,0), 2); sprintf(message, "%d", i); PrintMessageOnImage(image, message, Point(currentBall.Location.x,currentBall.Location.y - 10), 0.9); sprintf(message, "%d - (%.2f,%.2f) - %f", i, currentBall.Location.x, currentBall.Location.y, GetRelativeTimeToFirstSample(currentBall.DetectionTime)); PrintMessageOnImage(image, message, Point(0,(i+1)*20), 0.9); } ImageShowOnDebug("BallMove", "Vision", image); }
/***************************************************************************************** * Method Name: FindGate * Description: The function search for a couple of white vertical rectangles, whose buttom points * are on green && upper part is not on green. * This rectangles are marked as gate posts. * If only one post is present in the frame, the function tries to find the direction * of the second vertical post, using the upper post direction. * Arguments: BWImage, Output - DetectedGate * Return Values: DetectedGate object * Owner: Hen Shoob, Assaf Rabinowitz *****************************************************************************************/ DetectedObject* GateDetector::FindGate(Mat& inputImage) { Mat onlyWhiteImage; GetWhiteImage(inputImage, onlyWhiteImage); Mat whiteVertical = onlyWhiteImage.clone(); GetVerticalObjects(whiteVertical); //creating Mat thr with contours, using canny Mat src; cvtColor(whiteVertical, src, CV_GRAY2BGR); Mat canny; Canny(whiteVertical, canny, 50, 190, 3, false); ImageShowOnDebug("canny", canny); //crating contours vector vector<vector<Point> > contours; vector<Vec4i> hierarchy; findContours(canny, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); cvtColor(canny, canny, CV_GRAY2BGR); //finding the blocking rectangles of the contours vector<RotatedRect> minRect(contours.size()); for (size_t i = 0; i < contours.size(); i++) { minRect[i] = minAreaRect(Mat(contours[i])); DrawRectangle(canny, minRect[i]); } ImageShowOnDebug("contours", canny); Mat field; FindField(inputImage, field); //Showing the contours + field in one image cvtColor(canny, canny, CV_BGR2GRAY); // Mat or; // bitwise_or(field, canny, or); // ImageShowOnDebug("convex", or); vector<RotatedRect> post1Candidates; FindPostCandidates(field, minRect, post1Candidates); RotatedRect post1 = FindLargestCandidate(post1Candidates); DrawRectangle(src, post1); ImageShowOnDebug("post1", src); vector<RotatedRect> post2Candidates; for (size_t i = 0; i < post1Candidates.size(); i++) { if (post1Candidates[i].center.x != post1.center.x) { post2Candidates.push_back(post1Candidates[i]); } } RotatedRect post2 = FindLargestCandidate(post2Candidates); DrawRectangle(src, post2); ImageShowOnDebug("post2", src); ////////////////////////////////////////////////////////////////////////////////////////// if (post1.size.area() > 0 && post2.size.area() > 0) { return new DetectedGate(post1, post2); } if (post1.size.area() == 0 && post2.size.area() == 0) { return new DetectedGate(); } // One post - detect which. float longSide = (post1.size.height > post1.size.width) ? post1.size.height : post1.size.width; float shortSide = (post1.size.height < post1.size.width) ? post1.size.height : post1.size.width; Mat whiteHorizontal = onlyWhiteImage.clone(); GetHorizontalObjects(whiteHorizontal); ImageShowOnDebug("whiteHorizontal", whiteHorizontal); //counting white pixels near the top of the post on both sides // We set the range to look for white pixels in the upper post. float upperPostTop = MAX(post1.center.y - longSide / 2 - 10, 0); float upperPostButtom = upperPostTop + shortSide + 5; //Counting near the left post float upperPostLeft = MAX(post1.center.x - shortSide * 3, 0); float upperPostRight = MAX(post1.center.x - shortSide, 0); unsigned int leftCounter = CountAndDrawWhitePixels(whiteHorizontal, src, upperPostTop, upperPostButtom, upperPostLeft, upperPostRight); //finding right check rectangle, left and right side upperPostRight = MIN(post1.center.x + shortSide * 3, FRAME_WIDTH - 1); upperPostLeft = MIN(post1.center.x + shortSide, FRAME_WIDTH - 1); unsigned int rightCounter = CountAndDrawWhitePixels(whiteHorizontal, src, upperPostTop, upperPostButtom, upperPostLeft, upperPostRight); if (rightCounter > leftCounter && rightCounter - leftCounter > 5) { return new DetectedGate(post1, E_DetectedPost_LEFT); } else if (rightCounter < leftCounter && leftCounter - rightCounter > 5) { return new DetectedGate(post1, E_DetectedPost_RIGHT); } else { PrintStringOnImage(src, "Move camera up"); return new DetectedGate(); } }