#ifndef __IMAGE_ANALYSIS_ROI_OBJECT_BOX_UTIL_H__ #define __IMAGE_ANALYSIS_ROI_OBJECT_BOX_UTIL_H__ #include #include #include #include using namespace std; namespace trt_vehicle{ struct GridCoord { int x; int y; int i; }; struct ObjBox { int x; int y; int width; int height; float score; int class_; std::string label; std::vector gridCoords; }; struct ObjCls { int class_; std::string label; float score; }; float overlap( float x1, float w1, float x2, float w2); //calculation intersection area float boxIntersection(ObjBox& a, ObjBox& b); //calculation union area float boxUnion(ObjBox& a, ObjBox& b); //calculate iou of tow boxes float rectIou(ObjBox& a, ObjBox& b); float rectIouRegion(ObjBox& a, ObjBox& b); float rectIouDivOne(ObjBox& a, ObjBox& b); //coords change : from xyxy to objBox void xyxy2objBox(int x1,int y1,int x2,int y2, ObjBox& b); //Sort from large to small , return indeices std::vector getSortIndex(std::vector& scores); //nms for bounding boxes int nonMaximumSuppression(vector& boxes, vector& scores, float overlapThreshold,vector& outBoxes); //difference map pf two pictures cv::Mat diffImgs(cv::Mat bgImg,cv::Mat frontImg); //sort void sort(std::vector& scores,std::vector& coordXs,std::vector& coordYs); void sort(std::vector& scores); //draw boxes to img , and return the drawed img cv::Mat drawRectangle(cv::Mat img,std::vector& boxes,cv::Scalar color); //Calculate the Euclidean distance between two vectors double calVectorDistance(std::vector f1,std::vector f2); //Batch calculation of Euclidean distance between two vectors std::vector calVectorDistances(std::vector> fs1,std::vector> fs2); //Generate binary vector from distance value(score) void genBinaryVectorfromScore(vector binaryScores,vector scores,int imgW,int imgH,int downScale); //Generate gray image from distance value(score) void genImgfromScore(cv::Mat& img,vector scores,int imgW,int imgH,int downScale); //filter rectangles through regions of interest region std::vector selectRectByInterestMask(std::vector& rects,cv::Mat interestedMask); //find adjacent grids int disInVector(GridCoord gridCoord,std::vector>& indicesAll); //generate rectangles using distance vector void genBoxesFromVector(std::vector& dis,int gridW,int downScale,std::vector* rects,std::vector>* indicesAll); //generate rectangles using binary std::vector genBoxesFromBinary(cv::Mat& binary); //compare the current image with the background image to find different areas bool compareAlarmRectToBg(ObjBox rectCurr,std::vector& rects,ObjBox& rect); //filter rectangular boxes by scale / aspect ratio / region of interest / iou , etc class RectSelector{ private: std::vector m_rects; int m_cursor; ObjBox next(); bool hasNext(); void remove(); void remove_inside(ObjBox rect); void remove_overlop(ObjBox rect,float iouThres); public: void init(std::vector rects); void rmInside(); void rmOverlop(float iouThres); void rmLittle(int minW,int minH); void rmBig(int maxW,int maxH); void rmBigRatio(int maxW,int maxH,int imgH, float ratio); void rmBigWithRoadMask(cv::Mat& mask,std::vector& roadNumsList,float carLeastWidth); void rmRatio(); std::vector getRect(); }; } #endif