Ask Your Question
2

Need good method for grabbing image from webcam every 10 seconds

asked 2012-11-28 01:08:43 -0600

Scott Bell gravatar image

updated 2012-11-28 17:45:02 -0600

SR gravatar image

Hi there, Can anyone suggest a good way to grab an image from a webcam every 10 seconds and then display the timestamp on the image? I was able to get the following to work, but it only displays the timestamp the very first time it is called and does not update the timestamp, so looking for a better method. Thanks in advance -Scott

#include "stdafx.h"
#include <ctime>
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <stdio.h>
#include <stdlib.h>

using namespace cv;
using namespace std;

Mat base_mat;
time_t time_current;
time_t time_future;

int time_zero = 0;

const int NUM_SECONDS = 10;

///Declare Functions Header
void Text_On_Image( Mat image_name, String win_name, String img_text);

int _tmain(int argc, _TCHAR* argv[])
{
        time_t t = time(0);   // get time now
    struct tm * now = localtime( & t );
    time_current = now->tm_min;
    now->tm_year + 1900;
    now->tm_mon + 1;
    now->tm_mday;
     Mat base_mat(200,200, CV_8UC3, Scalar(0,0,0)); //RGB

     Mat src= cv::imread("C:\\Users\\Public\\Pictures\\Opencv_images\\video.jpg");
     cv::imshow("my_meter", src);

    int waitKeyValue = 1000;

        int count = 1;

    double time_counter = 0;

    clock_t this_time = clock();
    clock_t last_time = this_time;
    //Try to grab single frames at time stamps
    /*
        while(true)
    {
        this_time = clock();

        time_counter += (double)(this_time - last_time);

        last_time = this_time;

        if(time_counter > (double)(NUM_SECONDS * CLOCKS_PER_SEC))
        {

            time_counter -= (double)(NUM_SECONDS * CLOCKS_PER_SEC);
            printf("%d\n", count);
            count++;
        }

        printf("DebugTime = %f\n", time_counter);
    }
    */
    cv::Mat san;

    cv::VideoCapture san_cap(0);
    if (san_cap.isOpened()) {
        while (1) {
            san_cap.read(san);
            string time_hours = static_cast<ostringstream*>( &(ostringstream() << now->tm_hour) )->str(); //convert contour count to a string
            string time_minutes = static_cast<ostringstream*>( &(ostringstream() << now->tm_min) )->str(); //convert contour count to a string
            string time_seconds = static_cast<ostringstream*>( &(ostringstream() << now->tm_sec) )->str(); //convert contour count to a string

             cv::putText(san, time_hours + ":" + time_minutes + ":" + time_seconds, cvPoint(30,150), FONT_HERSHEY_COMPLEX_SMALL, 0.8, cvScalar(200,200,250), 1, CV_AA);
            cv::imshow("gp", san);
            int key = cv::waitKey(waitKeyValue);
            if(key!=-1)cout<<key<<endl;
            if (key == 27 || key == 1048586) {
                if (waitKeyValue == 10)waitKeyValue = 0;
                else waitKeyValue = 10;
            }
        }
    } else cout << "videoCapture not working" << endl;



    return 0;
}

void Text_On_Image( Mat image_name, String win_name, String img_text)
{
    cv::putText(image_name, img_text, cvPoint(1,30), FONT_HERSHEY_COMPLEX_SMALL, 0.6, cvScalar(255,255,255), 1, CV_AA);
    imshow( win_name, image_name );

}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2012-11-28 01:43:38 -0600

Mahdi gravatar image

You only need to put a waitKey(10000) before san_cap.read(san);

edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-11-28 01:08:43 -0600

Seen: 2,062 times

Last updated: Nov 28 '12