vector<Rect> detect(InputArray img) { // Run the detector with default parameters. to get a higher hit-rate // (and more false alarms, respectively), decrease the hitThreshold and // groupThreshold (set groupThreshold to 0 to turn off the grouping completely). vector<Rect> found; if (m == Default) hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2, false); else if (m == Daimler) hog_d.detectMultiScale(img, found, 0.5, Size(8,8), Size(32,32), 1.05, 2, true); return found; }
/** * Test detection with custom HOG description vector * @param hog * @param hitThreshold threshold value for detection * @param imageData */ static void detectTest(const HOGDescriptor& hog, const double hitThreshold, Mat& imageData) { vector<Rect> found; Size padding(Size(8, 8)); Size winStride(Size(8, 8)); hog.detectMultiScale(imageData, found, hitThreshold, winStride, padding); showDetections(found, imageData); }
PERF_TEST(HOGFixture, HOG) { Mat src = imread(getDataPath("gpu/hog/road.png"), cv::IMREAD_GRAYSCALE); ASSERT_TRUE(!src.empty()) << "can't open input image road.png"; vector<cv::Rect> found_locations; declare.in(src).time(5); if (RUN_PLAIN_IMPL) { HOGDescriptor hog; hog.setSVMDetector(hog.getDefaultPeopleDetector()); TEST_CYCLE() hog.detectMultiScale(src, found_locations); std::sort(found_locations.begin(), found_locations.end(), RectLess()); SANITY_CHECK(found_locations, 1 + DBL_EPSILON); } else if (RUN_OCL_IMPL) { ocl::HOGDescriptor ocl_hog; ocl_hog.setSVMDetector(ocl_hog.getDefaultPeopleDetector()); ocl::oclMat oclSrc(src); OCL_TEST_CYCLE() ocl_hog.detectMultiScale(oclSrc, found_locations); std::sort(found_locations.begin(), found_locations.end(), RectLess()); SANITY_CHECK(found_locations, 1 + DBL_EPSILON); } else OCL_PERF_ELSE }
/** * Test detection with custom HOG description vector * @param hog * @param hitThreshold threshold value for detection * @param imageData */ static void detectTest(const HOGDescriptor& hog, const double hitThreshold, Mat& imageData, vector<Rect>& found, vector<double>& weights) { //vector<Rect> found; Size padding(Size(32, 32)); Size winStride(Size(8, 8)); hog.detectMultiScale(imageData, found, weights, hitThreshold, winStride, padding, 1.05, 1); showDetections(found, imageData); }
/* * === FUNCTION ====================================================================== * Name: consumer * Description: 处理图像线程,计算hog和显示 * ===================================================================================== */ void consumer(void) { while (true){ vector<Rect> found, found_filtered; spsc_queue.pop(showimg); hog.detectMultiScale(showimg, found, 0, Size(4,4), Size(0,0), 1.05, 2); for (i=0; i<found.size(); i++) { Rect r = found[i]; for (j=0; j<found.size(); j++) if (j!=i && (r & found[j])==r) break; if (j==found.size()) found_filtered.push_back(r); } for (i=0; i<found_filtered.size(); i++) { Rect r = found_filtered[i]; r.x += cvRound(r.width*0.1); r.width = cvRound(r.width*0.8); r.y += cvRound(r.height*0.06); r.height = cvRound(r.height*0.9); rectangle(showimg, r.tl(), r.br(), cv::Scalar(0,255,0), 1); } imshow("1",showimg); waitKey(5); } }
void detectPeople(Mat frame, bool isFlip) { vector<Rect> found, found_filtered; // we shouldn't need to flip anything - if we always use landscape mode if (isFlip) { Mat flippedFrame; flip(frame, flippedFrame, 1); flippedFrame.copyTo(frame); } hog.detectMultiScale(frame, found, 0, Size(8,8), Size(32,32), 1.05, 2); LOGD("found %d", found.size()); for (int i = 0; i < found.size(); ++i) { Rect r = found[i]; int j = 0; for (; j < found.size(); ++j) { // what does & mean for Rect? if (j != i && (r & found[j]) == r) { break; } } if (j == found.size()) { found_filtered.push_back(r); } } for (int i = 0; i < found_filtered.size(); ++i) { Rect r = found_filtered[i]; rectangle(frame, r.tl(), r.br(), Scalar(255,0,0), 3); } }
void detectSat(HOGDescriptor& hog, const double hitThreshold, Mat& imageData, CircleData& cercle) { vector<Rect> found; Size padding(Size(4, 4)); Size winStride(Size(2, 2)); hog.detectMultiScale(imageData, found, hitThreshold, winStride, padding); showDetections(found, imageData, cercle); }
/** * Test detection with custom HOG description vector * @param hog * @param imageData */ static void detectTest(const HOGDescriptor& hog, Mat& imageData) { vector<Rect> found; int groupThreshold = 2; Size padding(Size(32, 32)); Size winStride(Size(8, 8)); double hitThreshold = 0.; // tolerance hog.detectMultiScale(imageData, found, hitThreshold, winStride, padding, 1.05, groupThreshold); showDetections(found, imageData); }
OCL_TEST_P(HOG, Detect) { HOGDescriptor hog; hog.winSize = winSize; hog.gammaCorrection = true; if (winSize.width == 48 && winSize.height == 96) hog.setSVMDetector(hog.getDaimlerPeopleDetector()); else hog.setSVMDetector(hog.getDefaultPeopleDetector()); std::vector<Rect> cpu_found; std::vector<Rect> gpu_found; OCL_OFF(hog.detectMultiScale(img, cpu_found, 0, Size(8, 8), Size(0, 0), 1.05, 6)); OCL_ON(hog.detectMultiScale(uimg, gpu_found, 0, Size(8, 8), Size(0, 0), 1.05, 6)); EXPECT_LT(checkRectSimilarity(img.size(), cpu_found, gpu_found), 0.05); }
void test_it( const Size & size ) { char key = 27; Scalar reference( 0, 255, 0 ); Scalar trained( 0, 0, 255 ); Mat img, draw; Ptr<SVM> svm; HOGDescriptor hog; HOGDescriptor my_hog; my_hog.winSize = size; VideoCapture video; vector< Rect > locations; // Load the trained SVM. svm = StatModel::load<SVM>( "my_people_detector.yml" ); // Set the trained svm to my_hog vector< float > hog_detector; get_svm_detector( svm, hog_detector ); my_hog.setSVMDetector( hog_detector ); // Set the people detector. hog.setSVMDetector( hog.getDefaultPeopleDetector() ); // Open the camera. video.open(0); if( !video.isOpened() ) { cerr << "Unable to open the device 0" << endl; exit( -1 ); } bool end_of_process = false; while( !end_of_process ) { video >> img; if( img.empty() ) break; draw = img.clone(); locations.clear(); hog.detectMultiScale( img, locations ); draw_locations( draw, locations, reference ); locations.clear(); my_hog.detectMultiScale( img, locations ); draw_locations( draw, locations, trained ); imshow( "Video", draw ); key = (char)waitKey( 10 ); if( 27 == key ) end_of_process = true; } }
vector<Rect> HogDetectPeople(Mat img) { HOGDescriptor hog; hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector()); fflush(stdout); vector<Rect> found, found_filtered; double t = (double)getTickCount(); // run the detector with default parameters. to get a higher hit-rate // (and more false alarms, respectively), decrease the hitThreshold and // groupThreshold (set groupThreshold to 0 to turn off the grouping completely). hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2); t = (double)getTickCount() - t; printf("tdetection time = %gms\n", t*1000./cv::getTickFrequency()); size_t i, j; for( i = 0; i < found.size(); i++ ) { Rect r = found[i]; for( j = 0; j < found.size(); j++ ) if( j != i && (r & found[j]) == r) break; if( j == found.size() ) found_filtered.push_back(r); } for( i = 0; i < found_filtered.size(); i++ ) { Rect r = found_filtered[i]; // the HOG detector returns slightly larger rectangles than the real objects. // so we slightly shrink the rectangles to get a nicer output. r.x += cvRound(r.width*0.1); r.width = cvRound(r.width*0.8); r.y += cvRound(r.height*0.07); r.height = cvRound(r.height*0.8); if(r.x+r.width>img.cols-1) { r.x=img.cols-1-r.width;} if(r.x<0) r.x=0; if(r.y+r.height>img.rows-1) r.y=img.rows-1-r.height; if(r.y<0) r.y=0; found_filtered[i].x=r.x; found_filtered[i].y=r.y; found_filtered[i].width=r.width; found_filtered[i].height=r.height; // rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 3); } return found_filtered; }
int main (int argc, const char * argv[]) { VideoCapture cap(CV_CAP_ANY); cap.set(CV_CAP_PROP_FRAME_WIDTH, 640); cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480); if (!cap.isOpened()) return -1; Mat img; HOGDescriptor hog; hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector()); namedWindow("video capture", CV_WINDOW_AUTOSIZE); while (true) { cap >> img; if (!img.data) continue; vector<Rect> found, found_filtered; hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2); //should be able to utilize found.size() as the person count //eliminating the graphics display and plotting should //speed things up. size_t i, j; for (i=0; i<found.size(); i++) { Rect r = found[i]; for (j=0; j<found.size(); j++) if (j!=i && (r & found[j])==r) break; if (j==found.size()) found_filtered.push_back(r); } for (i=0; i<found_filtered.size(); i++) { Rect r = found_filtered[i]; r.x += cvRound(r.width*0.1); r.width = cvRound(r.width*0.8); r.y += cvRound(r.height*0.06); r.height = cvRound(r.height*0.9); rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 2); } imshow("video capture", img); if (waitKey(20) >= 0) break; } return 0; }
void PeopleDetectByHOG( Mat frame , Mat mask, HOGDescriptor hog) { Mat frame_copy = frame.clone(); if(Body_scale_factor != 1.0f) { resize(frame_copy, frame_copy, Size(), Body_scale_factor, Body_scale_factor, INTER_AREA); // resize(mask, mask, Size(), Body_scale_factor, Body_scale_factor, INTER_AREA); } // imshow("test1",frameCopy); vector<Rect> found, found_filtered; //do detection //if the size of src image is too small, the program will be error as assertion fault //the parameters here should be adjusted hog.detectMultiScale(frame_copy, found, 0, Size(8,8), Size(32,32), 1.05, 2); // hog.detectMultiScale(frameCopy, found); //remove nested rectangle size_t i, j; for( i = 0; i < found.size(); i++ ) { Rect r = found[i]; for( j = 0; j < found.size(); j++ ) if( j != i && (r & found[j]) == r) break; if( j == found.size() ) found_filtered.push_back(r); } Mat drawing = Mat::zeros(frame_copy.size(),CV_8UC1); for( i = 0; i < found_filtered.size(); i++ ) { Rect r = found_filtered[i]; // the HOG detector returns slightly larger rectangles than the real objects. // so we slightly shrink the rectangles to get a nicer output. r = Rect_AdjustSizeAroundCenter(r,0.55,0.8); rectangle( drawing, r.tl(), r.br(), Scalar(255,255,255), -1, 8, 0); // rectangle( mask, r.tl(), r.br(), Scalar(255,255,255), -1, 8, 0); } if(Body_scale_factor != 1.0f) { resize(drawing, drawing, Size(video_size.width,video_size.height), 0,0, INTER_NEAREST); // resize(mask, mask, Size(), 1/Body_scale_factor, 1/Body_scale_factor, INTER_NEAREST); } for(int i = 0; i < mask.cols; i++) { for(int j = 0; j < mask.rows; j++) { Point p = Point(i,j); if(drawing.at<uchar>(p) == 255) mask.at<uchar>(p) += mask_add_step; } } }
void HOGTrainer::testIt(const string fileName) { if (trained != "") { char key = 27; Scalar sReference(0, 255, 0); Scalar sTrained(0, 0, 255); Mat img, draw; Ptr<SVM> svm; HOGDescriptor hog; HOGDescriptor my_hog; my_hog.winSize = size; VideoCapture *video; vector<Rect> locations; // Load the sTrained SVM. svm = StatModel::load<SVM>(trained); // Set the sTrained svm to my_hog vector<float> hog_detector; getSVMDetector(svm, hog_detector); my_hog.setSVMDetector(hog_detector); // Set the people detector. hog.setSVMDetector(hog.getDefaultPeopleDetector()); // Open the camera. video = new VideoCapture(fileName); if (!video->isOpened()) { cerr << "Unable to open the device 0" << endl; exit(-1); } bool end_of_process = false; while (!end_of_process) { video->read(img); if (img.empty()) break; draw = img.clone(); locations.clear(); hog.detectMultiScale(img, locations); drawLocations(draw, locations, sReference); locations.clear(); my_hog.detectMultiScale(img, locations); drawLocations(draw, locations, sTrained); imshow("Video", draw); key = (char) waitKey(10); if (27 == key) end_of_process = true; } } }
void CvPeopleDetector::Run() { HOGDescriptor hog; hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector()); // Get the new frame m_img = m_pImgProcessor->GetRGBImage(); // Clear the previously detected people m_found_filtered.clear(); std::vector<Rect> found; cv::Size winStride(8, 8); if (m_params.type == Params::SMALL_WIN) { winStride.width = 8; winStride.height = 8; } else if (m_params.type == Params::MEDIUM_WIN) { winStride.width = 16; winStride.height = 16; } else if (m_params.type == Params::LARGE_WIN) { winStride.width = 32; winStride.height = 32; } hog.detectMultiScale(m_img, found, m_params.hitThreshold, winStride, Size(32,32), m_params.scaleFactor, m_params.groupThreshold); //hog.detectMultiScale(m_img, found, 0, Size(8,8), Size(32,32), 1.05, 2); size_t i, j; for( i = 0; i < found.size(); i++ ) { Rect r = found[i]; for( j = 0; j < found.size(); j++ ) if( j != i && (r & found[j]) == r) break; if( j == found.size() ) m_found_filtered.push_back(r); } }
void testInVideo() { char key = 27; Scalar reference(0, 255, 0); Scalar trained(0, 0, 255); Mat img, draw; HOGDescriptor hog; hog.winSize = size; VideoCapture video; vector<Rect> locations; Ptr<SVM> svm = StatModel::load<SVM>("coral-detector.yml"); vector<float> hog_detector; get_svm_detector(svm, hog_detector); hog.setSVMDetector(hog_detector); string filename = "/home/modasshir/Documents/1080.mp4"; video.open(filename); if (!video.isOpened()) { cerr << "Unable to open the device 0" << endl; exit(-1); } int i = 2500; namedWindow("Video",WINDOW_OPENGL); bool end_of_process = false; while (!end_of_process) { video.set(CV_CAP_PROP_POS_FRAMES, i); video >> img; if (img.empty()) break; Size s = img.size(); resize(img,img,Size(s.width/4,s.height/4)); draw = img.clone(); hog.detectMultiScale(img, locations); draw_locations(draw, locations, reference); locations.clear(); imshow("Video", draw); key = (char) waitKey(50); if (27 == key) end_of_process = true; i = i + 15; } }
void detect_hog_inria(VideoCapture *vc) { // detector (64x128 template) HOGDescriptor hog; hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector()); // parameters double hit_thr = 0; double gr_thr = 2; Mat frame; __int64 freq,start,finish; ::QueryPerformanceFrequency((_LARGE_INTEGER*)&freq); while(1) { // input image *vc >> frame; if(frame.empty()) break; ::QueryPerformanceCounter((_LARGE_INTEGER*)&start); // detect vector<Rect> found; hog.detectMultiScale(frame, found, hit_thr, Size(8,8), Size(32,32), 1.05, gr_thr); // 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, Point(5, 35), FONT_HERSHEY_SIMPLEX, 1., Scalar(0,255,0), 2); // draw results (bounding boxes) for(int i=0; i<(int)found.size(); i++) rectangle(frame, found[i], Scalar(0,255,0), 2); // display imshow("darkpgmr", frame); char ch = waitKey(10); if( ch == 27 ) break; // ESC Key else if(ch == 32 ) // SPACE Key { while((ch = waitKey(10)) != 32 && ch != 27); if(ch == 27) break; } } }
OCL_PERF_TEST(HOGFixture, HOG) { UMat src; imread(getDataPath("gpu/hog/road.png"), cv::IMREAD_GRAYSCALE).copyTo(src); ASSERT_FALSE(src.empty()); vector<cv::Rect> found_locations; declare.in(src); HOGDescriptor hog; hog.setSVMDetector(hog.getDefaultPeopleDetector()); OCL_TEST_CYCLE() hog.detectMultiScale(src, found_locations); std::sort(found_locations.begin(), found_locations.end(), RectLess()); SANITY_CHECK(found_locations, 3); }
vector<Rect> detectPeople(Mat frame) { HOGDescriptor hog; vector<Rect> found, filtered; size_t i, j; hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector()); hog.detectMultiScale( frame, found, 0, Size(8,8), Size(32,32), 1.05, 2 ); for(i = 0; i < found.size(); i++){ Rect r = found[i]; for(j = 0; j < found.size(); j++) if(j != i && (r & found[j]) == r) break; if(j == found.size()) filtered.push_back(r); } for(i = 0; i < filtered.size(); i++){ Rect r = filtered[i]; r.x += cvRound(r.width*0.1); r.width = cvRound(r.width*0.8); r.y += cvRound(r.height*0.07); r.height = cvRound(r.height*0.8); rectangle(frame, r.tl(), r.br(), Scalar(0,255,0), 1); } cout << "--------------------------------------------" << endl; cout << "function: detectPeople" << endl; cout << "--------------------------------------------" << endl; return filtered; }
JNIEXPORT void JNICALL Java_org_opencv_objdetect_HOGDescriptor_detectMultiScale_11 (JNIEnv* env, jclass , jlong self, jlong img_nativeObj, jlong foundLocations_mat_nativeObj, jlong foundWeights_mat_nativeObj) { static const char method_name[] = "objdetect::detectMultiScale_11()"; try { LOGD("%s", method_name); vector<Rect> foundLocations; Mat& foundLocations_mat = *((Mat*)foundLocations_mat_nativeObj); vector<double> foundWeights; Mat& foundWeights_mat = *((Mat*)foundWeights_mat_nativeObj); HOGDescriptor* me = (HOGDescriptor*) self; //TODO: check for NULL Mat& img = *((Mat*)img_nativeObj); me->detectMultiScale( img, foundLocations, foundWeights ); vector_Rect_to_Mat( foundLocations, foundLocations_mat ); vector_double_to_Mat( foundWeights, foundWeights_mat ); return; } catch(const std::exception &e) { throwJavaException(env, &e, method_name); } catch (...) { throwJavaException(env, 0, method_name); } return; }
GaussianMixture* HogPeopleDetector::process(int offsetX=0, int offsetY=0, double scale=1) { detectedROI.clear(); detectedCENTER.clear(); detectedSCALES.clear(); detectedIDS.clear(); //gets the detections vector<cv::Rect> rois; vector<cv::Point> points; HOGDescriptor hogDesc;//the detector hogDesc.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());//we use a people detector //cv::Mat m (this->imgBank->imgSRC); cv::gpu::GpuMat gpuMat(this->imgBank->imgSRC); hogDesc.detectMultiScale(gpuMat, rois, 0, Size(8, 8), Size(32,32), 1.05, 2.5); //fills the gaussian mixture with detections for(int i = 0 ; i < rois.size() ; i++) { cv::Rect r = rois[i]; detectedROI.push_back(r); cv::Point p; p.x = r.x + r.width*0.5; p.y = r.y + r.height*0.5; detectedCENTER.push_back(p); this->gm->glist[i]->mean[0] = p.x; this->gm->glist[i]->mean[1] = p.y; } this->nbdetected = rois.size(); gm->curnb = rois.size(); return gm; }
JNIEXPORT void JNICALL Java_org_opencv_objdetect_HOGDescriptor_detectMultiScale_10 (JNIEnv* env, jclass , jlong self, jlong img_nativeObj, jlong foundLocations_mat_nativeObj, jlong foundWeights_mat_nativeObj, jdouble hitThreshold, jdouble winStride_width, jdouble winStride_height, jdouble padding_width, jdouble padding_height, jdouble scale, jdouble finalThreshold, jboolean useMeanshiftGrouping) { static const char method_name[] = "objdetect::detectMultiScale_10()"; try { LOGD("%s", method_name); vector<Rect> foundLocations; Mat& foundLocations_mat = *((Mat*)foundLocations_mat_nativeObj); vector<double> foundWeights; Mat& foundWeights_mat = *((Mat*)foundWeights_mat_nativeObj); HOGDescriptor* me = (HOGDescriptor*) self; //TODO: check for NULL Mat& img = *((Mat*)img_nativeObj); Size winStride((int)winStride_width, (int)winStride_height); Size padding((int)padding_width, (int)padding_height); me->detectMultiScale( img, foundLocations, foundWeights, (double)hitThreshold, winStride, padding, (double)scale, (double)finalThreshold, (bool)useMeanshiftGrouping ); vector_Rect_to_Mat( foundLocations, foundLocations_mat ); vector_double_to_Mat( foundWeights, foundWeights_mat ); return; } catch(const std::exception &e) { throwJavaException(env, &e, method_name); } catch (...) { throwJavaException(env, 0, method_name); } return; }
int main (int argc, const char * argv[]) { VideoCapture cap2(0); cap2.set(CV_CAP_PROP_FRAME_WIDTH, 320); cap2.set(CV_CAP_PROP_FRAME_HEIGHT, 240); bool isOnline=true; if(isOnline) argv[1]="test.avi"; cv::VideoCapture cap(argv[1]); if(!isOnline) cap=cap2; if (!cap.isOpened()) {printf("Failed to open file....");return -1;} Mat img; namedWindow("opencv", CV_WINDOW_AUTOSIZE); HOGDescriptor hog; hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector()); int frameskip=0; int detection=0; while (true) { frameskip+=10; int people_in_the_back=frameskip%35; cap.set(1,frameskip); cap >> img; if (img.empty()) continue; vector<Rect> found, found_filtered; hog.detectMultiScale(img, found, 0, Size(4,4), Size(16,16), 1.05, 3); size_t i, j; if(frameskip<40) detection =found.size()*4+7; else detection=found.size()*4+people_in_the_back; printf("\n Frame Number =%d People= %d",frameskip,detection); for (i=0; i<found.size(); i++) { Rect r = found[i]; for (j=0; j<found.size(); j++) if (j!=i && (r & found[j]) == r) break; if (j== found.size()) found_filtered.push_back(r); } for (i=0; i<found_filtered.size(); i++) { Rect r = found_filtered[i]; r.x += cvRound(r.width*0.1); r.width = cvRound(r.width*0.8); r.y += cvRound(r.height*0.07); r.height = cvRound(r.height*0.8); rectangle(img, r.tl(), r.br(), Scalar(0,255,0), 3); } imshow("opencv", img); if (waitKey(5)>=0) break; } return 0; }
void test_video(const Size & size) { char key = 27; Mat img, draw; Ptr<SVM> svm; HOGDescriptor hog; hog.winSize = size; vector< Rect > locations; vector< Rect > found_filtered; // Load the trained SVM. svm = StatModel::load<SVM>(TRAINED_SVM); // Set the trained svm to my_hog vector< float > hog_detector; get_svm_detector(svm, hog_detector); hog.setSVMDetector(hog_detector); printHOGParams(hog); VideoCapture video; // Open the video file. video.open(TRAFFIC_VIDEO_FILE); if (!video.isOpened()) { cerr << "Unable to open the device" << endl; exit(-1); } // Get the frame rate double rate = video.get(CV_CAP_PROP_FPS); cout << " Frame rate : " << rate << endl; cout << " Input video codec :" << video.get(CV_CAP_PROP_FOURCC); // initilaize the video writer object to write the video output std::string outputFile(OUT_Video_File); VideoWriter writer; int codec = static_cast<int>(video.get(CV_CAP_PROP_FOURCC)); //int codec = CV_FOURCC('M', 'J', 'P', 'G'); bool isWriterInitialized = false; int num_of_vehicles = 0; bool end_of_process = false; while (!end_of_process) { video >> img; if (img.empty()) break; draw = img.clone(); Mat cropped; cv::resize(draw, cropped, Size(720, 560)); Mat temp, temp3; cvtColor(cropped, temp, COLOR_BGR2GRAY); /*Mat bgr[3]; //destination array split(temp3,bgr);//split source temp = bgr[0]+bgr[2]; */ if (isWriterInitialized) { //execute only once isWriterInitialized = true; /*writer.open(outputFile, capture.get(CV_CAP_PROP_FOURCC), capture.get(CV_CAP_PROP_FPS), Size(capture.get(CV_CAP_PROP_FRAME_WIDTH),capture.get(CV_CAP_PROP_FRAME_HEIGHT)), true);*/ writer.open(outputFile, codec, rate, cropped.size(), true); } locations.clear(); // Rect(x,y,w,h) w->width=cols;h->rows // first remove the upper 50% from height Original Cropped =size(720,560)=(cols,rows) Mat roi = temp(Rect(0, temp.rows*0.5, temp.cols, temp.rows - temp.rows*0.5)); //size(roi) = size(720,280) //cout<<"roi.size() = "<<roi.size()<<endl; int y_offset = temp.rows*0.5; //again crop the lower 10 % to remove the images near dashboard-> remove false positives roi = roi(Rect(0, 0, roi.cols, roi.rows - 100)); //cout<<"roi.size() = "<<roi.size()<<endl; //no offset required as this is the lower row colums. //hog.detectMultiScale(roi, locations); //hog.detectMultiScale(roi, locations, 1, Size(50, 50), Size(32, 32), 1, 2);//对图片进行多尺度行人检测 hog.detectMultiScale(roi, locations, 0.00, Size(4, 8), Size(0, 0), 1.05, 2);//less false positive //hog.detectMultiScale(roi, locations, 0.00, Size(8,8), Size(0,0), 1.05, 2);// less true negative(missed) // add the offset std::vector<Rect>::iterator it = locations.begin(); std::vector<Rect>::iterator itend = locations.end(); vector<Rect> actuallocations; bool isVehicle = false; for (; it != itend; it++) { Rect current = *it; //cout<<" Rect current = "<< current<<endl; //cout<<" roi size= "<<roi.size()<<endl; Mat roi2Check = roi(Rect(current.x, current.y, current.width, current.height));//define a roi of 50x50 //cout<<" roi2Check size= "<<roi2Check.size()<<endl; isVehicle = checkIfpatchIsVehicle(roi2Check); if (isVehicle) actuallocations.push_back(Rect(current.x, current.y + y_offset, current.width, current.height)); } if (0 != actuallocations.size()) draw_locations(cropped, actuallocations, Scalar(0, 255, 0)); imshow(WINDOW_NAME, cropped); if (save_video) writer.write(cropped); //wait infinite fro keypress key = (char)waitKey(3); if (27 == key) end_of_process = true; } // Close the video file. // Not required since called by destructor writer.release(); video.release(); }
int main(int argc, char** argv) { VideoCapture cap(argv[1]); // open the default camera if(!cap.isOpened()) // check if we succeeded { return -1; } HOGDescriptor hog; Mat mono_img; hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector()); namedWindow("test",1); std::vector<Rect> found; std::vector<Rect> foundsave; std::vector<std::vector<Rect> > found_filtered(3,std::vector<Rect>(0)); int k = 0; Mat frame; Mat rW; Rect save(Point_<int>(0,0), Point_<int>(0,0)); Point2d prevPos; std::vector<Point2d> src(4); src[0] = Point2d(263,281); src[1] = Point2d(7,265); src[2] = Point2d(357,147); src[3] = Point2d(161,147); std::vector<Point2d> dest(4); dest[0] = Point2d(3,0); dest[1] = Point2d(0,0); dest[2] = Point2d(3,4.5); dest[3] = Point2d(0,3); Mat H; H = findHomography(src,dest,H); for(;;) { foundsave = found_filtered[k%3]; found_filtered[k%3] = std::vector<Rect>(0); cap >> frame; // get a new frame from camera if(k==0) { rW = frame; warpPerspective(frame,rW,H,Size(1000,1000)); imwrite("/home/benjamin.michelland/calibrated.png",rW); } cvtColor(frame, mono_img, CV_BGR2GRAY); hog.detectMultiScale(mono_img, found,-0.5); size_t i, j; for (i=0; i<found.size(); i++) { Rect r = found[i]; for (j=0; j<found.size(); j++) if (j!=i && (r & found[j])==r) break; if (j==found.size()) found_filtered[k%3].push_back(r); } if(k>=3) { if(found_filtered[k%3].empty()) { found_filtered[k%3] = foundsave; } if(!found_filtered[0].empty() && !found_filtered[1].empty() && !found_filtered[2].empty()) { std::vector<int> list = threeNearest(save,found_filtered); for(unsigned i = 0; i < found_filtered[0].size(); i++) { if(list[0] != -1 && list[1] != -1 && list[2] !=-1) { Rect r1 = found_filtered[0][list[0]]; Rect r2 = found_filtered[1][list[1]]; Rect r3 = found_filtered[2][list[2]]; Point_<int> c1 = r1.tl() + Point_<int>(r1.width/2,r1.height-10); c1 = c1 + r2.tl() + Point_<int>(r2.width/2,r2.height-10); c1 = c1 + r3.tl() + Point_<int>(r3.width/2,r3.height-10); c1 = Point_<int>(c1.x/3.0,c1.y/3.0); rectangle(frame, c1, c1, Scalar(0,255,0), 2); save = Rect(c1,c1); } } float speed = 0; if(k%25==0) { std::vector<Point2d> pix(1); pix[0] = save.tl(); std::vector<Point2d> pos(1); perspectiveTransform(pix,pos,H); if(init) { speed = sqrt((pos[0]-prevPos).dot(pos[0]-prevPos)); std::cout << std::setprecision(3) << speed << " m/s" << std::endl; } prevPos=pos[0]; init = true; } } } imshow("test", frame); if(waitKey(30) >= 0) break; k++; } // the camera will be deinitialized automatically in VideoCapture destructor return 0; }
int main() { int x, y , z; int leftX = 0; int leftY = 0; int rightX = 0; int rightY = 0; Mat cam0P, cam1P; Mat *Q = new Mat(); Mat *T = new Mat(); Calibration calibration; Triangulate triangulation; if (true) { vector<Mat> PandP; string filename = "stereo.xml"; PandP = calibration.Calibrate(filename, 5, 4, T, Q); cam0P = PandP.at(0); cam1P = PandP.at(1); } VideoCapture cap("/home/artyom/Dropbox/BigData/workspace/TriangulateHuman/Debug/out.avi"); /* cap.set(CV_CAP_PROP_FRAME_WIDTH, 320); cap.set(CV_CAP_PROP_FRAME_HEIGHT, 240); */ if (!cap.isOpened()) return -1; Mat img; VideoCapture cap1("/home/artyom/Dropbox/BigData/workspace/TriangulateHuman/Debug/out1.avi"); //cap.set(CV_CAP_PROP_FRAME_WIDTH, 320); //cap.set(CV_CAP_PROP_FRAME_HEIGHT, 240); if (!cap1.isOpened()) return -1; Mat img1; string Pos = ""; HOGDescriptor hog; hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector()); //string posPoint = ""; string posRect =""; string posRect2 =""; bool onceOnly = true; while (true) { cap >> img; cap1 >> img1; /* Mat tempFrame = img; Mat tempFrame1 = img1; cv::transpose(img, tempFrame); cv::flip(tempFrame, tempFrame, 0); cv::transpose(tempFrame, img); cv::flip(img, img, 0); cv::transpose(img1, tempFrame1); cv::flip(tempFrame1, tempFrame1, 0); cv::transpose(tempFrame1, img1); cv::flip(img1, img1, 0); */ if (!img.data) continue; vector<Rect> found, found_filtered; vector<Rect> found1, found_filtered1; hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2); hog.detectMultiScale(img1, found1, 0, Size(8,8), Size(32,32), 1.05, 2); //hog.detect(img, found1, 0, Size(8,8), Size(0,0)); size_t i, j; for (i=0; i<found.size(); i++) { Rect r = found[i]; for (j=0; j<found.size(); j++) if (j!=i && (r & found[j])==r) break; if (j==found.size()) found_filtered.push_back(r); } for (i=0; i<found_filtered.size(); i++) { Rect r = found_filtered[i]; if (r.x > 0 && r.x < 500){ r.x += cvRound(r.width*0.1); r.width = cvRound(r.width*0.8); r.y += cvRound(r.height*0.06); r.height = cvRound(r.height*0.9); leftX = r.x; leftY = r.y; string x = to_string(r.x); string y = to_string(r.y); posRect = to_string(global) + " Pos: x:" + x+ " y: " + y; rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 2); } } for (i=0; i<found1.size(); i++) { Rect r = found1[i]; for (j=0; j<found1.size(); j++) if (j!=i && (r & found1[j])==r) break; if (j==found1.size()) found_filtered1.push_back(r); } for (i=0; i<found_filtered1.size(); i++) { Rect r = found_filtered1[i]; if (r.x > 0 && r.x < 500){ r.x += cvRound(r.width*0.1); r.width = cvRound(r.width*0.8); r.y += cvRound(r.height*0.06); r.height = cvRound(r.height*0.9); rightX = r.x; rightY = r.y; string x = to_string(r.x); string y = to_string(r.y); posRect2 = to_string(global) + " Pos: x:" + x+ " y: " + y; global++; rectangle(img1, r.tl(), r.br(), cv::Scalar(0,255,0), 2); } } int number = 5; char text[255]; sprintf(text, "Score %d", (int)number); CvFont font; double hScale=1.0; double vScale=1.0; int lineWidth=1; cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX|CV_FONT_ITALIC, hScale,vScale,0,lineWidth); IplImage* img00 = new IplImage(img); IplImage* img11 = new IplImage(img1); char* p = new char[posRect.length()+1]; memcpy(p, posRect.c_str(), posRect.length()+1); cvPutText(img00, p, cvPoint(200,400), &font, cvScalar(0,255,0)); char* p2 = new char[posRect2.length()+1]; memcpy(p2, posRect2.c_str(), posRect2.length()+1); cvPutText(img11, p2, cvPoint(200,300), &font, cvScalar(255,255,255)); double realXYZ[3]; triangulation.triangulate(cam0P, cam1P, leftX, leftY, rightX, rightY, *T, *Q, realXYZ); /*Debuggining purpose */ //printToFile(leftX, leftY,rightX, rightY, realXYZ.at(0)[0], realXYZ.at(0)[1], realXYZ.at(0)[2] ); printToFile(leftX, leftY,rightX, rightY, realXYZ[0], realXYZ[1], realXYZ[2]); imshow("video capture", img); imshow("video capture2", img1); if (waitKey(1) >= 0) break; } namedWindow("video capture", CV_WINDOW_AUTOSIZE); cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!! return 0; }
int main (int arg, char * argv[]) { char * val = ""; if (arg > 1) { val = argv[1]; } else { //char szFile[100]; //OPENFILENAME ofn; //ZeroMemory( &ofn , sizeof( ofn)); //ofn.lStructSize = sizeof ( ofn ); //ofn.hwndOwner = NULL ; //wchar_t wFile[100]; //mbstowcs(wFile, szFile, strlen(szFile) + 1); //ofn.lpstrFile = wFile; //ofn.lpstrFile[0] = L'\0'; //ofn.nMaxFile = sizeof( szFile ); //ofn.lpstrFilter = L"All\0*.*\0Text\0*.TXT\0"; //ofn.nFilterIndex =1; //ofn.lpstrFileTitle = NULL ; //ofn.nMaxFileTitle = 0 ; //ofn.lpstrInitialDir=NULL ; //ofn.Flags = OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST ; //GetOpenFileName( &ofn ); //val = new char[wcslen(ofn.lpstrFile) + 1]; //wsprintfA(val, "%S", ofn.lpstrFile); } try { IplImage *image = cvLoadImage(val); //"f:\\test\\11.jpg" Mat img(image); HOGDescriptor hog; hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector()); vector<Rect> found, found_filtered; hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2); //hog.detectMultiScale(img, found, 0, Size(6,6), Size(32,32), 1.05, 0, true); size_t i, j; for (i=0; i<found.size(); i++) { Rect r = found[i]; for (j=0; j<found.size(); j++) if (j!=i && (r & found[j])==r) break; if (j==found.size()) found_filtered.push_back(r); } for (i=0; i<found_filtered.size(); i++) { Rect r = found_filtered[i]; r.x += cvRound(r.width*0.1); r.width = cvRound(r.width*0.8); //r.y += cvRound(r.height*0.06); r.height = cvRound(r.height*0.9); rectangle(img, r.tl(), r.br(), cv::Scalar(0,0,255), 1); } namedWindow("Image Viewer", CV_WINDOW_AUTOSIZE); imshow("Image Viewer", img); waitKey(0); } catch (exception e) { } return 0; }
int CVHOGDetector::main(int argc, char** argv) { string filename; string savefilename; if( argc == 1 ) { printf("Usage: peopledetect video_filename\n"); // filename="/mnt/hgfs/I/Win7/dataset/大王卡口全景东向西_大王卡口全景东向西/大王卡口全景东向西_大王卡口全景东向西_20150318090325.mp4"; filename="/media/yzbx/15311446439/测试视频/12.wmv"; } else if(argc ==2){ filename=argv[1]; } else{ filename=argv[1]; savefilename=argv[2]; } VideoCapture cap(filename); if(!cap.isOpened()){ std::cout<<"cannot open video file "<<filename<<endl; } HOGDescriptor hog; hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector()); namedWindow("people detector", 1); cv::VideoWriter videoWriter; bool firstTimeToOpen=true; for(;;) { Mat img; cap>>img; if(!img.data){ std::cout<<"empty image"<<endl; break; } // cv::resize(img,img,Size(0,0),0.5,0.5); vector<Rect> found, found_filtered; double t = (double)getTickCount(); // run the detector with default parameters. to get a higher hit-rate // (and more false alarms, respectively), decrease the hitThreshold and // groupThreshold (set groupThreshold to 0 to turn off the grouping completely). hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2); t = (double)getTickCount() - t; printf("tdetection time = %gms\n", t*1000./cv::getTickFrequency()); size_t i, j; for( i = 0; i < found.size(); i++ ) { Rect r = found[i]; for( j = 0; j < found.size(); j++ ) if( j != i && (r & found[j]) == r) break; if( j == found.size() ) found_filtered.push_back(r); } for( i = 0; i < found_filtered.size(); i++ ) { Rect r = found_filtered[i]; // the HOG detector returns slightly larger rectangles than the real objects. // so we slightly shrink the rectangles to get a nicer output. r.x += cvRound(r.width*0.1); r.width = cvRound(r.width*0.8); r.y += cvRound(r.height*0.07); r.height = cvRound(r.height*0.8); rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 3); } imshow("people detector", img); int c = waitKey(30) & 255; if(argc>2){ if(firstTimeToOpen){ cv::Size s=img.size(); assert(videoWriter.open(savefilename,CV_FOURCC('D', 'I', 'V', 'X'),50,s,true)); firstTimeToOpen=false; } assert(videoWriter.isOpened()); videoWriter<<img; } else{ if( c == 'q' || c == 'Q') break; } } return 0; }
int main(int argc, char ** argv) { VideoCapture cap(0); //set the width and the height of frame captured from this camera //cap.set(CV_CAP_PROP_FRAME_WIDTH,320); //cap.set(CV_CAP_PROP_FRAME_HEIGHT,240); if(!cap.isOpened()) { cout<<"Error: Failed to open camera"<<endl; return -1; } Mat img; HOGDescriptor hog; //use the default People Detector cout<<"Setting the default people detector"<<endl; hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector()); cout<<"Setting done"<<endl; cout<<"Creatting a Window"<<endl; namedWindow("Video Capture",CV_WINDOW_AUTOSIZE); cout<<"Creation done"<<endl; while(true) { printf("Getting an image\n"); for(unsigned char count = 0; count < 10;count++) { //printf("Grabbing Frame from file\n"); if(!cap.grab()) { printf("Error: Cannot grab frame from file\n"); count--; continue; } //printf("Retrieving Frame into Mat\n"); if(!cap.retrieve(img)) { printf("Error: Cannot retrieve frame into Mat\n"); count--; continue; } } printf("Image got\n"); if(!img.data) { cout<<"Currently no valid data"<<endl; continue; } cout<<"Capture valid frame"<<endl; vector<Rect> found, found_filtered; /* * *void HOGDescriptor::detectMultiScale( * const Mat& img, vector<Rect>& foundLocations, * double hitThreshold, Size winStride, Size padding, * double scale0, double finalThreshold, bool useMeanshiftGrouping) const * */ hog.detectMultiScale(img,found,0,Size(8,8),Size(32,32),1.05,2); size_t i,j; //first for loop cout<<"Entering first for loop"<<endl; for(i = 0; i < found.size();i++) { Rect r = found[i]; for (j=0; j<found.size(); j++) if (j!=i && (r & found[j])==r) break; if (j==found.size()) found_filtered.push_back(r); } cout<<"Entering second for loop"<<endl; //second for loop for(i = 0; i < found_filtered.size();i++) { Rect r = found_filtered[i]; r.x += cvRound(r.width * 0.1); r.width = cvRound(r.width * 0.8); r.y += cvRound(r.height * 0.06); r.height = cvRound(r.height * 0.9); rectangle(img,r.tl(),r.br(),cv::Scalar(0,255,0),2); } imshow("Video Capture",img); if(waitKey(20) >= 0) break; } return 0; }
int main (int argc, const char * argv[]) { printf("Hello\n"); cout << "Hello Artyom" << endl << flush ; VideoCapture cap; cap.open("http://192.168.1.105:8080/?action=stream"); /* cap.set(CV_CAP_PROP_FRAME_WIDTH, 320); cap.set(CV_CAP_PROP_FRAME_HEIGHT, 240); */ if (!cap.isOpened()) return -1; Mat img; /* VideoCapture cap1(1); cap.set(CV_CAP_PROP_FRAME_WIDTH, 320); cap.set(CV_CAP_PROP_FRAME_HEIGHT, 240); if (!cap1.isOpened()) return -1; Mat img2; */ string Pos = ""; HOGDescriptor hog; hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector()); string posPoint = ""; string posRect =""; while (true) { cap >> img; // cap1 >> img2; if (!img.data) continue; vector<Rect> found, found_filtered; vector<Point> found1, found_filtered1; hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2); hog.detect(img, found1, 0, Size(8,8), Size(0,0)); size_t i, j; for (i = 0 ; i < found1.size(); i++){ Point tempPoint = found1[i]; Rect r ; if (tempPoint.x > 0 && tempPoint.y > 0) { r.x += tempPoint.x; r.y += tempPoint.y; r.width = 10; r.height = 10; rectangle(img, r.tl(), r.br(), cv::Scalar(255,0,0), 2); string x = to_string(r.x); string y = to_string(r.y); posPoint = "Pos: x:" + x+ " y: " + y; } } for (i=0; i<found.size(); i++) { Rect r = found[i]; for (j=0; j<found.size(); j++) if (j!=i && (r & found[j])==r) break; if (j==found.size()) found_filtered.push_back(r); } for (i=0; i<found_filtered.size(); i++) { Rect r = found_filtered[i]; r.x += cvRound(r.width*0.1); r.width = cvRound(r.width*0.8); r.y += cvRound(r.height*0.06); r.height = cvRound(r.height*0.9); // string x = to_string(r.x); string y = to_string(r.y); // posRect = "Pos: x:" + x+ " y: " + y; rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 2); } int number = 5; char text[255]; sprintf(text, "Score %d", (int)number); CvFont font; double hScale=1.0; double vScale=1.0; int lineWidth=1; cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX|CV_FONT_ITALIC, hScale,vScale,0,lineWidth); IplImage* img1 = new IplImage(img); char* p = new char[posRect.length()+1]; memcpy(p, posRect.c_str(), posRect.length()+1); cvPutText(img1, p, cvPoint(200,400), &font, cvScalar(0,255,0)); char* p2 = new char[posPoint.length()+1]; memcpy(p2, posPoint.c_str(), posPoint.length()+1); cvPutText(img1, p2, cvPoint(200,430), &font, cvScalar(255,255,255)); imshow("video capture", img); // imshow("video capture2", img2); if (waitKey(1) >= 0) break; } //namedWindow("video capture", CV_WINDOW_AUTOSIZE); return 0; }