OpenCV C++ ,Collision for Retangle and line -
please click! image pedestrians , line
hello, guys guys can see pedestrians rectangle , line middle on video. used cascade training detecting pedestrian.(cascadeclassifier).
i want detect collison rectangle(pedestrian) , line. when collision detected, want change rectangle's color. here code wrote. please check , let me know how detect collsion. i'm using c++ , opencv 2.4.9.
appreciate.
cv::size min_obj_sz(47,65); cv::size max_obj_sz(80,100); int width,height; cv::mat frame,gray_frame; __int64 freq,start,finish; ::queryperformancefrequency((_large_integer*)&freq); videocapture >> frame; frameimage = imageformat::mat2qimage(frame); origin_videoframelabel->setpixmap(qpixmap::fromimage(frameimage)); cvtcolor(frame, gray_frame, cv_bgr2gray); ::queryperformancecounter((_large_integer*)&start); cv::vector<cv::rect> found; detector.detectmultiscale( gray_frame, found, 1.1, 1, cv_haar_find_biggest_object | cv_haar_scale_image, min_obj_sz, max_obj_sz ); // processing time (fps) ::queryperformancecounter((_large_integer*)&finish); double fps = freq / double(finish - start + 1); char fps_str[20]; sprintf_s(fps_str, 20, "fps: %.1lf", fps); puttext(frame, fps_str, cv::point(5, 35), cv::font_hershey_simplex, 1., cv::scalar(0,255,0), 2); // draw results (bounding boxes) width=(frame.cols); height=frame.rows; line(frame, cv::point(0, width/2), cv::point(width,height/2),cv::scalar(94, 206, 165),3,2); for(int i=0; i<(int)found.size(); i++) rectangle(frame, found[i], cv::scalar(0,255,0), 2); cvtcolor(frame, gray_frame, cv_bgr2gray); if(gray_convert == true) frameimage = imageformat::mat2qimage(frame); else frameimage = imageformat::matgray2qimage(gray_frame); videoframelabel->setpixmap(qpixmap::fromimage(frameimage));
a simple solution try intersect line 4 edges of bounding box, if of them intersects line, there collision. how intersect line edge? here example might explain
in above figure, black box bounding box, if extend left edge (blue line), there 3 cases happen line in image blue line
1) line parallel blue line
2) line intersects blue line, intersection not on black edge (red line)
3) line intersects blue line , intersection on black edge (green line)
so let's have line represented 2 points p1
, p2
, can write collision test function this
//! test if line (with point p(x1, y2) , slope k) intersects line segment (x, min_y) -> (x, max_y) bool intersect(const double x1, const double y1, const double k, const double x, const double min_y, const double max_y) { double y = y1 + (x - x1) * k; return (y >= min_y && y <= max_y); } bool collision_detection(const cv::point& p1, const cv::point& p2, const cv::rect& r) { // vertical line if (p1.x == p2.x) { return (p1.x >= r.x && p1.x < r.x+r.width); } // horizontal line else if (p1.y == p2.y) { return (p1.y >= r.y && p1.y < r.y+r.height); } // general case double k = (p2.y - p1.y) / (p2.x - p1.x); // test left edge if (intersect(p1.x, p1.y, k, r.x, r.y, r.y+r.height-1)) { return true; } // test right edge if (intersect(p1.x, p1.y, k, r.x+r.width-1, r.y, r.y+r.height-1)) { return true; } // can reuse intersect function if swap x , y double k_inv = (p2.x - p1.x) / (p2.y - p1.y); // test top edge if (intersect(p1.y, p1.x, k_inv, r.y, r.x, r.x+r.width-1)) { return true; } // test bottom edge if (intersect(p1.y, p1.x, k_inv, r.y+r.height-1, r.x, r.x+r.width-1)) { return true; } // failed, no collision return false; }
Comments
Post a Comment