static bool checkColision(const cv::Rect &a, const cv::Rect &b){ if (b.contains(a.tl())) return true; if (b.contains(a.br())) return true; if (b.contains(cv::Point(a.x+a.width,a.y))) return true; if (b.contains(cv::Point(a.x,a.y+a.height))) return true; return false; }
void drawQuadrants(cv::Point targetCoord) { cv::line(image, cv::Point(0, centerPoint.y), cv::Point(frameWidth, centerPoint.y), blackColor); cv::line(image, cv::Point(centerPoint.x, 0), cv::Point(centerPoint.x, frameHeight), blackColor); if (targetCoord != cv::Point(999,999)) { if (centerRectangle.contains(targetCoord)) { targetCentered = true; targetInQ1 = targetInQ2 = targetInQ3 = targetInQ4 = false; highlightAllQuadrants(); } else if (targetCoord.x >= centerPoint.x) { if (targetCoord.y <= centerPoint.y) { targetInQ1 = true; targetInQ2 = targetInQ3 = targetInQ4 = targetCentered = false; highlightQuadrant(1); } else if (targetCoord.y > centerPoint.y) { targetInQ4 = true; targetInQ1 = targetInQ2 = targetInQ3 = targetCentered = false; highlightQuadrant(4); } } else if (targetCoord.x < centerPoint.x) { if (targetCoord.y <= centerPoint.y) { targetInQ2 = true; targetInQ1 = targetInQ3 = targetInQ4 = targetCentered = false; highlightQuadrant(2); } else if (targetCoord.y > centerPoint.y) { targetInQ3 = true; targetInQ1 = targetInQ2 = targetInQ4 = targetCentered = false; highlightQuadrant(3); } } } }
Point computeIntersect(cv::Vec4i a, cv::Vec4i b, cv::Rect ROI) { int x1 = a[0], y1 = a[1], x2 = a[2], y2 = a[3]; int x3 = b[0], y3 = b[1], x4 = b[2], y4 = b[3]; Point p1 = Point(x1,y1); Point p2 = Point(x2,y2); Point p3 = Point(x3,y3); Point p4 = Point(x4,y4); if( !ROI.contains(p1) || !ROI.contains(p2) || !ROI.contains(p3) || !ROI.contains(p4) ) return Point(-1,-1); Point vec1 = p1-p2; Point vec2 = p3-p4; float vec1_norm2 = vec1.x*vec1.x + vec1.y*vec1.y; float vec2_norm2 = vec2.x*vec2.x + vec2.y*vec2.y; float cosTheta = (vec1.dot(vec2))/sqrt(vec1_norm2*vec2_norm2); float den = ((float)(x1-x2) * (y3-y4)) - ((y1-y2) * (x3-x4)); if(den != 0) { cv::Point2f pt; pt.x = ((x1*y2 - y1*x2) * (x3-x4) - (x1-x2) * (x3*y4 - y3*x4)) / den; pt.y = ((x1*y2 - y1*x2) * (y3-y4) - (y1-y2) * (x3*y4 - y3*x4)) / den; if( !ROI.contains(pt) ) return Point(-1,-1); // no-confidence metric float d1 = MIN( dist2(p1,pt), dist2(p2,pt) )/vec1_norm2; float d2 = MIN( dist2(p3,pt), dist2(p4,pt) )/vec2_norm2; float no_confidence_metric = MAX(sqrt(d1),sqrt(d2)); if( no_confidence_metric < 0.5 && cosTheta < 0.707 ) return Point(int(pt.x+0.5), int(pt.y+0.5)); } return cv::Point(-1, -1); }
bool ImageSegmentation::toSegment(const std::vector<std::vector<cv::Point>> &contours, const std::vector<cv::Vec4i> &hierarchy, const size_t index, Segment &seg, const size_t minSize, const size_t minHoleSize, const cv::Rect &roi) { seg.rect = cv::boundingRect(contours[index]); if(!roi.contains(seg.rect.tl()) || !roi.contains(seg.rect.br())) { // outside ROI return false; } seg.area = cv::contourArea(contours[index]); seg.childrenArea = 0; seg.holes = 0; if(seg.area < minSize) { return false; } Segment child; for(int32_t childIndex = hierarchy[index][2]; childIndex >= 0; childIndex = hierarchy[childIndex][0]) { if(toSegment(contours, hierarchy, childIndex, child, minHoleSize, minHoleSize, roi)) { seg.children.push_back(child); seg.area -= child.area; seg.childrenArea += child.area; ++seg.holes; } } if(seg.area < minSize) { return false; } seg.contour = contours[index]; computeMoments(contours, hierarchy, index, seg); compute2DAttributes(seg); return true; }
bool MultiObjectTracker::sharesBoundingRect(size_t i, cv::Rect boundingRect) { for (size_t j = 0; j < this->kalmanTrackers.size(); j++) { if (i == j) { continue; } if (boundingRect.contains(this->kalmanTrackers[i].latestPrediction())) { return true; } } return false; }