void testBS(string inputpath, string maskpath, string inputPrefix, string extinput, int firstFileP, int lastfile, int step) { int currFrame=-1; int lastFrame=lastfile; string maskprefix("/bin"); string extmask("png"); std::ofstream ofs; UtilCpp u; string str; if(firstFileP>=0) currFrame=firstFileP; else currFrame=1; if(lastfile>0) lastFrame=lastfile; while( currFrame <= lastFrame ) { Mat input = u.getFrame(false, inputpath+inputPrefix, currFrame, 6, extinput); Mat resultFg = processImages(input,currFrame); string rname = string(maskpath)+"/bin"; string bgname = string(maskpath)+"/bg/bg"; u.writeImg(rname, resultFg, currFrame, string("jpg")); Mat bg; pMOG.getBackgroundImage(bg); u.writeImg(bgname,bg,currFrame,string("bmp")); input.release(); currFrame+=step; } }
int main(int argc, char *argv[]) { Mat frame; Mat back; Mat fore; VideoCapture cap(0); int i=0; BackgroundSubtractorMOG2 bg; bg.set("nmixtures",3); bg.set("detectShadows",false); namedWindow("Frame"); namedWindow("Background"); int backgroundFrame=500; for(;;) { //Get the frame cap >> frame; //Update the current background model and get the foreground if(backgroundFrame>0) {bg.operator ()(frame,fore);backgroundFrame--;} else {bg.operator()(frame,fore,0);} //Get background image to display it bg.getBackgroundImage(back); imshow("Frame",frame); imshow("Background",back); if(waitKey(10) >= 0) break; } return 0; }
void background(Mat &img, Mat &fore, Mat &back) { bg.operator()(img, fore); bg.getBackgroundImage(back); erode(fore, fore, Mat()); return ; }
int main(int argc, char *argv[]) { /* @frame - raw frame * @back - background image * @fore - foreground mask */ Mat frame, back, fore; VideoCapture cap(0); // Caputure input from camera // Read above description if(cap.isOpened() == false){ cout << " Failed to access video camera."; } BackgroundSubtractorMOG2 bg; bg.set("detectShadows", false); bg.set("nmixtures", 3); bg.set("history", 30); // vector of vector of points // We connect set of detected points to contours // 1 vector of points = 1 contour // No of contours detected on fly :-) std::vector< std::vector<cv::Point> > contours; namedWindow("Frame"); for( ; ;) { // Grab frame from video at store it into frame cap >> frame; // Detect foreground objects // and write to fore matrix bg.operator()(frame, fore); // Detect background // and store in back matrix bg.getBackgroundImage(back); // Noise removal, we perform two operations // 1. erode // 2. dilate erode(fore, fore, Mat()); dilate(fore, fore, Mat()); // findcountours maps vector of points to contours // as described above findContours(fore, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE ); // Now draw those contours on frame with red color vector<Point2f>center( contours.size() ); vector<float>radius( contours.size() ); drawContours(frame, contours, -1, Scalar(0, 0, 255), 2); imshow("Frame", frame); if(waitKey(30) >= 0) break; } return 0; }
void ballDetect :: initDetect(char *videoInput){ VideoCapture capture; Mat src, src_HSV, processed; int x=0; int y=0; Mat currentFrame, back, fore; BackgroundSubtractorMOG2 bg; std::vector<std::vector<cv::Point> > contours; capture.open(videoInput); capture.set(CV_CAP_PROP_FRAME_WIDTH, FRAME_WIDTH); capture.set(CV_CAP_PROP_FRAME_HEIGHT, FRAME_HEIGHT); // int xyz=1; while(1){ // cout<<xyz++<<endl; capture.read(src); cvtColor(src, src_HSV, COLOR_BGR2HSV); bg.operator ()(src, fore); bg.getBackgroundImage(back); erode(fore, fore, Mat()); dilate(fore, fore, Mat()); findContours(fore, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); // drawContours(src, contours, -1, Scalar(0, 0, 255), 2); contourCount=contours.size(); if(white_collide.x != -1 && white_collide.y!=-1){ circle(src, white_collide, 2, Scalar(0, 0, 0), 2); circle(src, white_initial, 2, Scalar(0, 0, 0), 2); line(src, white_initial, white_collide, Scalar(255, 255, 255), 1, CV_AA); } inRange(src_HSV, *minval, *maxval, processed); morphOps(processed); trackFilteredObject(x, y, processed, src); for(int i=0;i<(int)white_position.size()-1;++i){ line(src, white_position[i], white_position[i+1], Scalar(255, 255, 255), 1, CV_AA); } while(white_collide.x == -1 && white_collide.y==-1){ setMouseCallback("source", onMouseClick, &src); putText(src, "Specify Point", Point(750, 40), 1, 1, Scalar(255, 0, 0), 2); imshow("source", src); waitKey(5); } imshow("source", src); waitKey(5); } }
int main () { Mat frame; Mat back; Mat fore; VideoCapture cap1(1);/*to capture from camera*/ BackgroundSubtractorMOG2 bg;//works on GMM bg.set ("nmixtures", 10); vector < vector < Point > >contours; namedWindow ("Frame"); int i=0; for (;;) { cap1 >> frame; bg.operator()(frame, fore); bg.getBackgroundImage (back); erode (fore, fore, cv::Mat ()); erode (fore, fore, cv::Mat ()); dilate (fore, fore, cv::Mat ()); dilate (fore, fore, cv::Mat ()); dilate (fore, fore, cv::Mat ()); findContours (fore, contours, CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE); drawContours (frame, contours, -1, Scalar (255, 255, 255), 1); Scalar color = Scalar(200,200,200); int a=0; vector<Rect> boundRect( contours.size() ); for( int i = 0; i < contours.size(); i++ ) { boundRect[i] = boundingRect( contours[i] ); } for( i = 0; i< contours.size(); i++ ) { if(boundRect[i].width>=40 || boundRect[i].height>=40)//eliminates small boxes { a=a+(boundRect[i].height)*(boundRect[i].width); } // cout<<"Net contour area is "<<a<<"\n"; if(a>=int(frame.rows)*int(frame.cols)/2)//change denominator as per convenience { putText(frame,"Tampering",Point(5,30),FONT_HERSHEY_SIMPLEX,1,Scalar(0,255,255),2); cout<<"\a"; } } imshow ("Frame", frame); waitKey(10); } return 1; }
int main(int argc, char *argv[]) { Mat frame; Mat back; Mat fore; Mat frame_roi; Rect roi_rect; VideoCapture cap(path); BackgroundSubtractorMOG2 bg; //bg.set("bShadowDetection", false); //bg.bShadowDetection = false; vector<vector<Point> > contours; namedWindow("Frame"); namedWindow("Background"); cap >> frame; Rect rect_roi = getSelected(frame); for(;;) { cap >> frame; // capture frame from video frame_roi = frame(rect_roi); bg.operator ()(frame_roi,fore); bg.getBackgroundImage(back); // get background image erode(fore,fore,Mat()); dilate(fore,fore,Mat()); findContours(fore,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE); // find contours vector<vector<Point> > contours_poly( contours.size() ); vector<Rect> boundRect( contours.size() ); vector<Point2f>center( contours.size() ); vector<float>radius( contours.size() ); for( int i = 0; i < contours.size(); i++ ) { approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true ); boundRect[i] = boundingRect( Mat(contours_poly[i]) ); minEnclosingCircle( (Mat)contours_poly[i], center[i], radius[i] ); } /// Draw polygonal contour + bonding rects + circles Mat drawing = Mat::zeros( fore.size(), CV_8UC3 ); for( int i = 0; i< contours.size(); i++ ) { if(boundRect[i].area() <min_size || boundRect[i].area() >max_size){ continue; } Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) ); //drawContours( frame, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point() ); rectangle( frame_roi, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 ); circle( frame_roi, center[i], (int)radius[i], color, 2, 8, 0 ); } ///// imshow("Frame",frame); imshow("Background",fore); if(waitKey(1) >= 0) break; } return 0; }
int main(int argc, char* argv[]) { CvCapture* capture = 0; Mat frame, frameCopy, image; char c; vector <Rect> roi; VideoCapture cap(0); // open the default camera if (!cap.isOpened()) { cout << "Error:" << endl; } // check if we succeeded namedWindow("result"); char TrackbarName[50]; sprintf( TrackbarName, "Red max %d", red_t_max ); createTrackbar( TrackbarName, "result", &red_t_max, 70, on_trackbar ); sprintf( TrackbarName, "Red min %d", red_t_min ); createTrackbar( TrackbarName, "result", &red_t_min, 70, on_trackbar ); sprintf( TrackbarName, "Blue max %d", blue_t_max ); createTrackbar( TrackbarName, "result", &blue_t_max, 70, on_trackbar ); sprintf( TrackbarName, "Blue min %d", blue_t_min ); createTrackbar( TrackbarName, "result", &blue_t_min, 70, on_trackbar ); sprintf( TrackbarName, "Green max %d", green_t_max ); createTrackbar( TrackbarName, "result", &green_t_max, 70, on_trackbar ); sprintf( TrackbarName, "Green min %d", green_t_min ); createTrackbar( TrackbarName, "result", &green_t_min, 70, on_trackbar ); int count = 0; bool filter = false; cv::Scalar total_mean[POINTS]; namedWindow("Frame"); namedWindow("Background"); BackgroundSubtractorMOG2 bg; bg.set("nmixtures",3); bg.set("detectShadows",false); // namedWindow("FG Mask MOG 2"); //cap >> frame; //namedWindow("filtered"); int background = 500; Mat fore; Mat back; for (;;) { cap >> frame; if(background>0) { bg.operator ()(frame,fore);background--; } else { putText(frame,"Ready",Point(10,20),FONT_HERSHEY_PLAIN,1,Scalar(0,0,0)); filter = true; bg.operator()(frame,fore,0); } bg.getBackgroundImage(back); erode(fore,fore,Mat()); dilate(fore,fore,Mat()); imshow("Frame",frame); imshow("Background",back); imshow("Forground",fore); //cout<< background << endl; /* pMOG->operator()(frame, fgMaskMOG); pMOG2->operator()(frame, fgMaskMOG2); imshow("Frame", frame); imshow("FG Mask MOG", fgMaskMOG); imshow("FG Mask MOG 2", fgMaskMOG2);*/ // flip(frame,frameCopy,1); //imshow("result", frame); // imshow("result2", frameCopy); if (filter) { //Mat dst; /* Scalar lowerBound=Scalar( total_mean[0][0] -red_t_min , total_mean[0][1] -green_t_min, total_mean[0][2] - blue_t_min ); Scalar upperBound=Scalar( total_mean[0][0] + red_t_max , total_mean[0][1] + green_t_max, total_mean[0][2] + blue_t_max ); Mat dst; inRange(frame, lowerBound, upperBound, dst); for(int i =1; i < POINTS; i ++) { Scalar lowerBound=Scalar( total_mean[i][0] - red_t_min , total_mean[i][1] - green_t_min, total_mean[i][2] - blue_t_min ); Scalar upperBound=Scalar( total_mean[i][0] + red_t_max , total_mean[i][1] + green_t_max, total_mean[i][2] + blue_t_max ); Mat tmp; inRange(frame, lowerBound, upperBound, tmp); dst+=tmp; } medianBlur(dst, dst,7); imshow("result2", dst); pyrUp(dst,dst); */ findContours(fore,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE); hullI=vector<vector<int> >(contours.size()); hullP=vector<vector<Point> >(contours.size()); defects=vector<vector<Vec4i> > (contours.size()); //cout << " We found: " << contours.size() << endl; int indexOfBiggestContour = -1; int sizeOfBiggestContour = 0; for (int i = 0; i < contours.size(); i++){ if(contours[i].size() > sizeOfBiggestContour){ sizeOfBiggestContour = contours[i].size(); indexOfBiggestContour = i; } } if(sizeOfBiggestContour < 300) continue; cout << "Size: " << sizeOfBiggestContour << endl; cIdx = indexOfBiggestContour; bRect = boundingRect(Mat(contours[indexOfBiggestContour])); bRect_height=bRect.height; bRect_width=bRect.width; convexHull(Mat(contours[indexOfBiggestContour]),hullP[indexOfBiggestContour],false,true); convexHull(Mat(contours[indexOfBiggestContour]),hullI[indexOfBiggestContour],false,false); approxPolyDP( Mat(hullP[indexOfBiggestContour]), hullP[indexOfBiggestContour], 18, true ); if(contours[indexOfBiggestContour].size()>3 ){ convexityDefects(contours[indexOfBiggestContour],hullI[indexOfBiggestContour],defects[indexOfBiggestContour]); eleminateDefects(&fore); } getFingerTips(frame); bool isHand=detectIfHand(); rectangle(frame,bRect,cv::Scalar(200,0,0)); drawContours(frame,hullP,cIdx,cv::Scalar(200,0,0),2, 8, vector<Vec4i>(), 0, Point()); if(isHand){ // getFingerTips(frame); drawFingerTips(frame); // myDrawContours(frame); } } /*Scalar color = Scalar(0, 255, 0); roi.push_back(Rect(Point(frame.cols / 3, frame.rows / 6), Point(frame.cols / 3 + 20, frame.rows / 6 + 20))); roi.push_back(Rect(Point(frame.cols / 4, frame.rows / 2), Point(frame.cols / 4 + 20, frame.rows / 2 + 20))); roi.push_back(Rect(Point(frame.cols / 3, frame.rows / 1.5), Point(frame.cols / 3 + 20, frame.rows / 1.5 + 20))); roi.push_back(Rect(Point(frame.cols / 2, frame.rows / 2), Point(frame.cols / 2 + 20, frame.rows / 2 + 20))); roi.push_back(Rect(Point(frame.cols / 2.5, frame.rows / 2.5), Point(frame.cols / 2.5 + 20, frame.rows / 2.5 + 20))); roi.push_back(Rect(Point(frame.cols / 2, frame.rows / 1.5), Point(frame.cols / 2 + 20, frame.rows / 1.5 + 20))); roi.push_back(Rect(Point(frame.cols / 2.5, frame.rows / 1.8), Point(frame.cols / 2.5 + 20, frame.rows / 1.8 + 20))); rectangle(frame, roi[0], color, 2); rectangle(frame,roi[1] , color, 2); rectangle(frame, roi[2], color, 2); rectangle(frame, roi[3], color, 2); rectangle(frame, roi[4], color, 2); rectangle(frame, roi[5], color, 2); rectangle(frame, roi[6], color, 2);*/ std::string win = "majd"; imshow(win, frame); //imshow("faaa",frame); /*roi.push_back(My_ROI(Point(m->src.cols / 3, m->src.rows / 6), Point(m->src.cols / 3 + square_len, m->src.rows / 6 + square_len), m->src)); roi.push_back(My_ROI(Point(m->src.cols / 4, m->src.rows / 2), Point(m->src.cols / 4 + square_len, m->src.rows / 2 + square_len), m->src)); roi.push_back(My_ROI(Point(m->src.cols / 3, m->src.rows / 1.5), Point(m->src.cols / 3 + square_len, m->src.rows / 1.5 + square_len), m->src)); roi.push_back(My_ROI(Point(m->src.cols / 2, m->src.rows / 2), Point(m->src.cols / 2 + square_len, m->src.rows / 2 + square_len), m->src)); roi.push_back(My_ROI(Point(m->src.cols / 2.5, m->src.rows / 2.5), Point(m->src.cols / 2.5 + square_len, m->src.rows / 2.5 + square_len), m->src)); roi.push_back(My_ROI(Point(m->src.cols / 2, m->src.rows / 1.5), Point(m->src.cols / 2 + square_len, m->src.rows / 1.5 + square_len), m->src)); roi.push_back(My_ROI(Point(m->src.cols / 2.5, m->src.rows / 1.8), Point(m->src.cols / 2.5 + square_len, m->src.rows / 1.8 + square_len), m->src)); */ c = waitKey(10); if (c == 'q') { return 0; } // c = 'a'; if (c == 'a') { //Mat tst = frame(roi[0]); //total_mean = cv::mean(tst);; filter = true; } } return 0; }
int main(int argc, char *argv[]) { RNG rng(12345); Mat frame; Mat back; Mat fore; VideoCapture cap(0); const int nmixtures =3; const bool bShadowDetection = false; const int history = 4; double dist2Center; BackgroundSubtractorMOG2 bg (history,nmixtures,bShadowDetection); vector<vector<Point> > contours; namedWindow("Frame"); namedWindow("Background"); setMouseCallback( "Frame", onMouse, 0 ); //createTrackbar( "Smin", "CamShift Demo", &smin, 256, 0 ); createTrackbar( "Vmin", "Frame", &vmin, 256, 0 ); createTrackbar( "Vmax", "Frame", &vmax, 256, 0 ); createTrackbar( "Smin", "Frame", &smin, 256, 0 ); for(;;) { cap >> frame; bg.operator ()(frame,fore); bg.getBackgroundImage(back); erode(fore,fore,Mat()); dilate(fore,fore,Mat()); findContours(fore,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE); // CV_RETR_EXTERNAL retrieves only the extreme outer contours // vectors to hold contours infos vector<vector<Point> > contours_poly( contours.size() ); vector<Rect> boundRect( contours.size() ); vector<Point2f>center( contours.size() ); vector<float>radius( contours.size() ); for( unsigned int i = 0; i< contours.size(); i++ ) { Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) ); approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true); boundRect[i] = boundingRect ( Mat(contours_poly[i]) ); double area = boundRect[i].area(); if (area > 10000) { Point2f center(boundRect[i].x + boundRect[i].width/2.0, boundRect[i].y + boundRect[i].height/2.0); // Test if the center of a contour has crossed ROI (direction: going in or out) if (parking.size() > 3) { dist2Center = pointPolygonTest(parking, center, true); } cout << center << "is " << dist2Center << " distance from the contour. \n"; putText(frame, "I", center, FONT_HERSHEY_COMPLEX_SMALL, 1.5, color, 1); rectangle(frame, boundRect[i].tl(), boundRect[i].br(), Scalar(100, 100, 200), 2, CV_AA); // Tracking object with camShift if (trackObject) { CamShiftTracker CSTracker; CSTracker.track(frame, boundRect[i], vmin, vmax, smin); if( backProjMode ) cvtColor(CSTracker.getBackProjection(), frame, CV_GRAY2BGR ); ellipse(frame, CSTracker.getTrackBox(), Scalar(0,0,255), 3, CV_AA ); } } } /* * Draw parking zone */ for (unsigned int j = 0; j < parking.size(); j++) { circle(frame, parking[j], 5, Scalar(0,0,255), -1); } drawPaking(frame); imshow("Frame",frame); imshow("Background",back); char c = (char)waitKey(30); if( c == 27 || c == 'q') break; switch(c) { case 's': traceParking = !traceParking; break; case 't': trackObject = !trackObject; break; case 'b': backProjMode = !backProjMode; break; case 'c': parking.clear(); break; default: ; } } return 0; }
int main(int argc, const char *argv[]) { VideoCapture cap("/home/mac/Documents/PROJECT/Training_Sets/Father/father_divya (1).mp4"); noframes=1; Mat3b frame; cv::Mat frame1; cv::Mat back; cv::Mat fore; std::vector<std::vector<cv::Point> > contours; BackgroundSubtractorMOG2 bg; // bg.nmixtures = 3; //bg.bShadowDetection = false; int options=1,hands_count=0; double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight)); //VideoWriter oVideoWriter ("/home/mac/Documents/PROJECT/Output/Friend_t.avi", CV_FOURCC('P','I','M','1'), 20, frameSize, true); //initialize the VideoWriter object //VideoWriter oVideoWriter1 ("/home/mac/Documents/PROJECT/Output/HSV2_s.avi", CV_FOURCC('P','I','M','1'), 20, frameSize, true); //initialize the VideoWriter object Mat trace = Mat::zeros( Size(dWidth,dHeight), CV_8UC3 ); bool flag=true; while(cap.read(frame ) and (options==1 or options==2)) { noframes+=1; //if(noframes<5) continue; skin = GetSkin(frame); cvtColor(skin,skin,CV_RGB2GRAY); skin1 = skin> 50; blur( skin1, skin1, Size(3,3) ); char* source_window = "Source"; src_gray=skin1; Mat output; Point array[3];int sz=0; frame1=frame; skin1=draw_contour(src_gray,array,sz,frame1); bg.operator()(frame1,fore); bg.getBackgroundImage(back); cv::erode(fore,fore,cv::Mat()); cv::dilate(fore,fore,cv::Mat()); cv::findContours(fore,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE); cv::drawContours(frame1,contours,-1,cv::Scalar(0,0,255),2); cv::imshow("Frame",frame1); cv::imshow("Background",back); if(sz and flag) { flag=false; face=array[0]; } //imshow("image",human); face_map(array,sz); if(sz>1) { MyCircle(trace,array[0],0); // imshow("draw",trace); // oVideoWriter.write(trace); } //Mat aux=thresh_callback(0,0); //drawKeypoints(skin1, keypoints, output); skin2=frame; blur( skin1, skin1, Size( 5, 5 ) ); imshow(source_window, skin1); // oVideoWriter1.write(skin1); if(sz>2) hands_count++; if(sz>1) { } waitKey(50); } destroyAllWindows(); int ch=0,single=1; if(hands_count>=5) single++; return 0; }