Beispiel #1
0
//==============================================================
void VideoTesting::run(int Type){
    //for (int i = 0; i < 2*33; i++) getNextFrame();
    cv::namedWindow("Orginal",CV_WINDOW_AUTOSIZE);
    cv::namedWindow("HSV Filtered",CV_WINDOW_AUTOSIZE);
    cv::namedWindow("Line Filtered",CV_WINDOW_AUTOSIZE);
    cv::namedWindow("Canny", CV_WINDOW_AUTOSIZE);

    if (Type == 2){
    cv::moveWindow("Orginal", 1400, 50);           //reading from photo
    cv::moveWindow("HSV Filtered", 1000, 50);
    cv::moveWindow("Line Filtered", 600, 50);
    cv::moveWindow("Canny", 100, 50);}
    else if (Type == 1){
    cv::moveWindow("Orginal", 1200, 50);           //reading from camera
    cv::moveWindow("HSV Filtered", 1200, 500);
    cv::moveWindow("Line Filtered", 600, 50);
    cv::moveWindow("Canny", 600, 500);}
    else if (Type == 0){
    cv::moveWindow("Orginal", 1400, 100);         //reading from video
    cv::moveWindow("HSV Filtered", 1400, 500);
    cv::moveWindow("Line Filtered", 800, 100);
    cv::moveWindow("Canny", 800, 500);}

    cv::Mat frame;
    cv::Mat filtered;
    cv::Mat filtered2;
    cv::Mat lineFiltered;
    cv::Mat contour;

    cv::namedWindow("Control", CV_WINDOW_AUTOSIZE); //create a window called "Control"
    int iLowH = 5;
    int iHighH = 105;
    int iLowS = 10;
    int iHighS = 59;
    int iLowV = 0;
    int iHighV = 237;
    int max = 1;
    //Create trackbars in "Control" window
    cvCreateTrackbar("LowH", "Control", &iLowH, 179); //Hue (0 - 179)
    cvCreateTrackbar("HighH", "Control", &iHighH, 179);
    cvCreateTrackbar("LowS", "Control", &iLowS, 255); //Saturation (0 - 255)
    cvCreateTrackbar("HighS", "Control", &iHighS, 255);
    cvCreateTrackbar("LowV", "Control", &iLowV, 255); //Value (0 - 255)
    cvCreateTrackbar("HighV", "Control", &iHighV, 255);
    cvCreateTrackbar("max", "Control", &max, 130);

    //HSVFilter hf(25, 179, 0, 255, 0,255);
    LineFilter lf;
    ShapeFilter sf(1, 1);
    BlurFilter bf(2, 0.2f);
    BlurFilter bf2(1, 0.4f);
    if (Type == 0)
        frame = cv::imread("test_path.png");       //img
    cv::Scalar color = cv::Scalar(255, 0, 0);

    while (1){
        //contour = cv::Mat::zeros(frame.size(), CV_8UC3);
        if (Type == 2)
            frame = this->getNextFrame(); //video
        if (Type == 1)
            frame = getNextCameraFrame(); //webcam
        contour = frame.clone();
        if(frame.cols == 0)break;       //exit when there is no next fraame

        //filtered = hf.filter(&frame);
        filtered2 = HSVFilter(frame, iLowH, iHighH, iLowS, iHighS, iLowV, iHighV);
        filtered = HSVFilter(frame, iLowH, iHighH, iLowS, iHighS, iLowV, iHighV);
        //filtered = blur(filtered,max);
        //filtered = bf.filter(filtered2);
        //filtered = bf2.filter(filtered);
        lineFiltered = lf.filter(filtered, 0);
        //lineFiltered = Moments(filtered);

        //draw rectangle
        if (sf.findRect(filtered)){
            std::vector<cv::RotatedRect> rect = sf.getRect();
            for (cv::RotatedRect rekt: rect){
                cv::Point2f rect_points[4]; rekt.points( rect_points );
                for( int j = 0; j < 4; j++ )
                   line( contour, rect_points[j], rect_points[(j+1)%4], color, 1, 8 );
                //delete rect;
                std::cout<<rekt.angle<<std::endl;
                cv::Point2f ps[4];
                rekt.points(ps);
                std::cout << ps[0] << " " << ps[1] << " " << ps[2] << " " << ps[3] << std::endl;
            }
        }

        //draw circle
        if (sf.findCirc(filtered)){
            auto rad = sf.getRad();
            auto cent = sf.getCenter();
            //radius
            for (unsigned int i = 0; i < rad.size(); i++){
                cv::circle(lineFiltered, cent[i], rad[i], cv::Scalar(255,0,0) );
            }
            //center
            for (unsigned int i = 0; i < rad.size(); i++){
                cv::circle(lineFiltered, cent[i], 2, cv::Scalar(0,255,0));
            }
        }

        //gets the mass center; testing purposes only
        auto t = sf.findMassCenter(filtered);
        for (cv::Point2f p: t){
            cv::circle(lineFiltered, p, 2, cv::Scalar(0,0,255));
        }

        imshow("Orginal", frame);
        imshow("HSV Filtered", filtered);
        imshow("Line Filtered", lineFiltered);
        imshow("Canny", contour);
        int key = cv::waitKey((33));        //wait for 33ms, ~= 30fps;
        //std::cout<<key<<std::endl;
        if (key == 27) break;               //if user press esc, break the loop
        else if(key == 98) cv::waitKey(0);  //"b" key, "pasues" play

        /*delete filtered;
        delete filtered2;
        delete lineFiltered;*/
    }
    cvReleaseCapture(&capture);
    //cap.release();
    std::cout << "End of video feed" << std::endl;
}
Beispiel #2
0
int main( int argc, char** argv ) {
    unsigned long nframes = 0, t_start, t_end;
    
    if (argc == 1) 
        printf ("For options use --help\n\n");

    for (int i = 1; i < argc; i++) {
        if (!strcmp(argv[i],"1") || !strcmp(argv[i],"2"))
            CAM_NUMBER = atoi (argv[i]);
        else if (!strcmp (argv[i], "--test"))
            TEST = 1;
        else if (!strcmp (argv[i], "--grad"))
            GRAD = 1;
        else if (!strcmp (argv[i], "--watershed"))
            WATERSHED = 1;
        else if (!strcmp (argv[i], "--write"))
            WRITE = 1;
        else if (!strcmp (argv[i], "--break"))
            BREAK = 1;
        else if (!strcmp (argv[i], "--line"))
            LINE = 1;
        else if (!strcmp (argv[i], "--circle"))
            CIRCLE = 1;
        else if (!strcmp (argv[i], "--rect"))
            RECT = 1;
        else if (!strcmp (argv[i], "--gate"))
            GATE = 1;
        else if (!strcmp (argv[i], "--path"))
            PATH = 1;
        else if (!strcmp (argv[i], "--buoy"))
            BUOY = 1;
        else if (!strcmp (argv[i], "--frame"))
            FRAME = 1;
        else if (!strcmp (argv[i], "--timeout"))
            TIMEOUT = atoi(argv[++i]);
        else if (!strcmp (argv[i], "--pos"))
            POS = atoi(argv[++i]); 
        else if (!strcmp (argv[i], "--load"))
            LOAD = ++i; // put the next argument index into LOAD
        else if (!strcmp (argv[i], "--help")) {
            printf ("OpenCV based webcam program. Hit 'q' to exit. Defaults to cam0, writes to \"webcam.avi\"\n");
            printf ("Put any integer as an argument (without --) to use that as camera number\n\n");
            printf ("  --write\n    Write captured video to file.\n\n");
            printf ("  --load\n    Use a video file (following --load) instead of a webcam.\n\n");
            printf ("  --line\n    Run line finding code.\n\n");
            printf ("  --circle\n    Run circle finding code.\n\n");
            printf ("  --break\n    Pause the input (press any key to go to the next frame).\n\n");
            printf ("  Example: `./webcam 1 --write` will use cam1, and will write to disk\n\n");
            return 0;
        }
    }

    /// initialization
    // init camera
    mvCamera* camera = NULL;
    if (LOAD == 0) {
        camera = new mvCamera (CAM_NUMBER);
    }
    else {
        camera = new mvCamera (argv[LOAD]);
    }
    if (camera != 0 && POS > 0) {
        camera->set_relative_position(static_cast<double>(POS)/1000);
    }

    mvVideoWriter* frame_writer = NULL;
    mvVideoWriter* filter_writer_1 = NULL;
    mvVideoWriter* filter_writer_2 = NULL;
    if (WRITE) {
        frame_writer = new mvVideoWriter ("frames.avi");
        filter_writer_1 = new mvVideoWriter ("filtered_1.avi");
        filter_writer_2 = new mvVideoWriter ("filtered_2.avi");
    }

    // init windows
    mvWindow* win1 = new mvWindow ("webcam");
    mvWindow* win2 = new mvWindow ("win2");
    mvWindow* win3 = new mvWindow ("win3");

    // declare filters we need
    mvHSVFilter HSVFilter ("test_settings.csv"); // color filter
    mvBinaryMorphology Morphology7 (9,9, MV_KERN_ELLIPSE);
    mvBinaryMorphology Morphology5 (5,5, MV_KERN_ELLIPSE);
    mvHoughLines HoughLines ("test_settings.csv");
    mvLines lines; // data struct to store lines
    mvKMeans kmeans;
    mvWatershedFilter watershed_filter;
    mvContours contour_filter;

    MDA_VISION_MODULE_GATE* gate=GATE? new MDA_VISION_MODULE_GATE : 0;
    MDA_VISION_MODULE_PATH* path=PATH? new MDA_VISION_MODULE_PATH : 0;
    MDA_VISION_MODULE_BUOY* buoy=BUOY? new MDA_VISION_MODULE_BUOY : 0;
    MDA_VISION_MODULE_FRAME* frame_task=FRAME? new MDA_VISION_MODULE_FRAME : 0;

    // declare images we need
    IplImage* scratch_color = mvCreateImage_Color();
    IplImage* scratch_color_2 = mvCreateImage_Color();
    IplImage* filter_img = mvCreateImage ();
    IplImage* filter_img_2 = mvCreateImage ();
 
    /// execution
    char c = 0;
    IplImage* frame;

    t_start = clock();

    for (;;) {
        frame = camera->getFrameResized(); // read frame from cam
        if (LOAD) {
            usleep(17000);
        }
	if (TIMEOUT > 0) {
	    if ((clock() - t_start)/CLOCKS_PER_SEC > TIMEOUT) {
	         break;
            }
	}

        if (!frame) {
            printf ("Video Finished.\n");
            break;
        }
        
        if (nframes < 10) {
            nframes++;
            continue;
        }
 
        cvCopy (frame, scratch_color);
        win1->showImage (scratch_color);
        
        if (TEST) {             
        }
        else if (GATE) {
            if (gate->filter (frame) == FULL_DETECT) {
                cvWaitKey(200);
            }
        }
        else if (PATH) {
            if (path->filter (frame) == FULL_DETECT) {
                cvWaitKey(00);
            }
        }
        else if (BUOY) {
            if (buoy->filter (frame) == FULL_DETECT) {
                cvWaitKey(200);
            }
        }
        else if (FRAME) {
            if (frame_task->filter (frame) == FULL_DETECT) {
                cvWaitKey(200);
            }
        }
        else if (WATERSHED) {
            watershed_filter.watershed(frame, filter_img);
            win1->showImage (frame);
            win2->showImage (filter_img);
            
            COLOR_TRIPLE color;
            MvCircle circle;
            MvCircleVector circle_vector;
            MvRotatedBox rbox;
            MvRBoxVector rbox_vector;
            
            while ( watershed_filter.get_next_watershed_segment(filter_img_2, color) ) {
                if (CIRCLE) {
                    contour_filter.match_circle(filter_img_2, &circle_vector, color);
                }
                else if (RECT) {
                    contour_filter.match_rectangle(filter_img_2, &rbox_vector, color, 2, 6);
                }
/*
                win3->showImage(filter_img_2);
                cvWaitKey(200);
*/              
            }

            if (CIRCLE && circle_vector.size() > 0) {
                MvCircleVector::iterator iter = circle_vector.begin();
                MvCircleVector::iterator iter_end = circle_vector.end();
                int index = 0;
                printf ("%d Circles Detected:\n", static_cast<int>(circle_vector.size()));
                for (; iter != iter_end; ++iter) {
                    printf ("\tCircle #%d: (%3d,%3d), Rad=%5.1f, <%3d,%3d,%3d>\n", ++index,
                        iter->center.x, iter->center.y, iter->radius, iter->m1, iter->m2, iter->m3);
                    iter->drawOntoImage(filter_img_2);
                }
                
                win3->showImage (filter_img_2);
                cvWaitKey(200);
            }
            else if (RECT && rbox_vector.size() > 0) {
                MvRBoxVector::iterator iter = rbox_vector.begin();
                MvRBoxVector::iterator iter_end = rbox_vector.end();
                int index = 0;
                printf ("%d Rectangles Detected:\n", static_cast<int>(rbox_vector.size()));
                for (; iter != iter_end; ++iter) {
                    printf ("\tRect #%d: (%3d,%3d), Len=%5.1f, Width=%5.1f, Angle=%5.1f <%3d,%3d,%3d>\n", ++index, 
                        iter->center.x, iter->center.y, iter->length, iter->width, iter->angle, iter->m1, iter->m2, iter->m3);
                    iter->drawOntoImage(filter_img_2);
                }
                
                win3->showImage (filter_img_2);
                cvWaitKey(200);
            }
        }

        if (GRAD) {
            Morphology5.gradient(filter_img, filter_img);
        }

        if (LINE) {
            HoughLines.findLines (filter_img, &lines);
            kmeans.cluster_auto (1, 8, &lines, 1);
        
            //lines.drawOntoImage (filter_img);
            kmeans.drawOntoImage (filter_img);
            lines.clearData(); // erase line data and reuse allocated mem
            //kmeans.clearData();
            
            win3->showImage (filter_img);
        }
        /*
        else if (CIRCLE) {
            CvPoint centroid;
            float radius;
            contour_filter.match_circle(filter_img, centroid, radius);
            contour_filter.drawOntoImage(filter_img);
            win3->showImage (filter_img);
        }
        else if (RECT) {
            CvPoint centroid;
            float length, angle;
            contour_filter.match_rectangle(filter_img, centroid, length, angle);
            contour_filter.drawOntoImage(filter_img);
            win3->showImage (filter_img);
        }
        */
        if (WRITE) {
            frame_writer->writeFrame (frame);
            cvCvtColor (filter_img, scratch_color, CV_GRAY2BGR);
            filter_writer_1->writeFrame (scratch_color);
            cvCvtColor (filter_img_2, scratch_color, CV_GRAY2BGR);
            filter_writer_2->writeFrame (scratch_color);
        }

        nframes++;
        if (BREAK)
            c = cvWaitKey(0);
        else if (LOAD)
            c = cvWaitKey(3); // go for about 15 frames per sec
        else
            c = cvWaitKey(5);

        if (c == 'q')
            break;
        else if (c == 'w') {
            mvDumpPixels (frame, "webcam_img_pixel_dump.csv");
            mvDumpHistogram (frame, "webcam_img_histogram_dump.csv");
        }
    }
    
    t_end = clock ();
    printf ("\nAverage Framerate = %f\n", (float)nframes/(t_end - t_start)*CLOCKS_PER_SEC);
    
    cvReleaseImage (&scratch_color);
    cvReleaseImage (&scratch_color_2);
    cvReleaseImage (&filter_img);
    cvReleaseImage (&filter_img_2);
    delete camera;
    delete frame_writer;
    delete filter_writer_1;
    delete filter_writer_2;
    delete win1;
    delete win2;
    delete win3;
    return 0;
}