Ask Your Question

Revision history [back]

i modified the code of @Eduardo for this post. i tried to correct the FPS calculation not to exceed 30fps

#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

int main(int argc, char**argv)
{
    cv::VideoCapture cam;

    if (!cam.open(0))
    {
        std::cout << "Problem connecting to cam " << std::endl;
        return -1;
    }
    else
    {
        std::cout << "Successfuly connected to camera " << std::endl;
    }

    cv::Mat frame;

    double total_time = 0.0;
    int c,cpt = 30;

    double t = (double) cv::getTickCount();
    do
    {
        t = ((double) cv::getTickCount() - t) / cv::getTickFrequency();
        total_time += t;
        cpt++;
        t = (double) cv::getTickCount();

        cam.read(frame);

        cv::putText(frame, cv::format("Average FPS=%d",cvRound(cpt/total_time)), cv::Point(30, 30), cv::FONT_HERSHEY_SIMPLEX, 1.0, cv::Scalar(0,0,255));

        cv::imshow("test", frame);

        c = cv::waitKey(30);
    }
    while(c != 27);

    return 0;
}

i modified the code of @Eduardo for this post. i tried to correct the FPS calculation not to exceed 30fps

#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

int main(int argc, char**argv)
{
    cv::VideoCapture cam;

    if (!cam.open(0))
    {
        std::cout << "Problem connecting to cam " << std::endl;
        return -1;
    }
    else
    {
        std::cout << "Successfuly connected to camera " << std::endl;
    }

    cv::Mat frame;

    double total_time = 0.0;
    int c,cpt = 30;

    double t = (double) cv::getTickCount();
    do
    {
        t = ((double) cv::getTickCount() - t) / cv::getTickFrequency();
        total_time += t;
        cpt++;
        t = (double) cv::getTickCount();

        cam.read(frame);

        cv::putText(frame, cv::format("Average FPS=%d",cvRound(cpt/total_time)), cv::Point(30, 30), cv::FONT_HERSHEY_SIMPLEX, 1.0, cv::Scalar(0,0,255));

        cv::imshow("test", frame);

        c = cv::waitKey(30);
    }
    while(c != 27);

    return 0;
}

another way ( thanks to Pedro Batista )

#include <iostream>
#include <ctime>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/ocl.hpp>

using namespace std;

int main()
{
    cv::VideoCapture cam;

    if (!cam.open(0))
        std::cout << "Problem connecting to cam " << std::endl;
    else
        std::cout << "Successfuly connected to camera " << std::endl;

    long frameCounter = 0;

    std::time_t timeBegin = std::time(0);
    int tick = 0;

    cv::Mat frame;

    while (1)
    {
        cam.read(frame);

        cv::imshow("Sobel blurred Frame", frame);
        cv::waitKey(1);

        frameCounter++;

        std::time_t timeNow = std::time(0) - timeBegin;

        if (timeNow - tick >= 1)
        {
            tick++;
            cout << "Frames per second: " << frameCounter << endl;
            frameCounter = 0;
        }
    }

    return 0;
}

i modified the code of @Eduardo for this post. i tried to correct the FPS calculation not to exceed 30fps

#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

int main(int argc, char**argv)
{
    cv::VideoCapture cam;

    if (!cam.open(0))
    {
        std::cout << "Problem connecting to cam " << std::endl;
        return -1;
    }
    else
    {
        std::cout << "Successfuly connected to camera " << std::endl;
    }

    cv::Mat frame;

    double total_time = 0.0;
    int c,cpt = 30;

    double t = (double) cv::getTickCount();
    do
    {
        t = ((double) cv::getTickCount() - t) / cv::getTickFrequency();
        total_time += t;
        cpt++;
        t = (double) cv::getTickCount();

        cam.read(frame);

        cv::putText(frame, cv::format("Average FPS=%d",cvRound(cpt/total_time)), cv::Point(30, 30), cv::FONT_HERSHEY_SIMPLEX, 1.0, cv::Scalar(0,0,255));

        cv::imshow("test", frame);

        c = cv::waitKey(30);
    }
    while(c != 27);

    return 0;
}

another way ( thanks to Pedro Batista )

#include <iostream>
#include <ctime>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/ocl.hpp>

using namespace std;

int main()
{
    cv::VideoCapture cam;

    if (!cam.open(0))
        std::cout << "Problem connecting to cam " << std::endl;
    else
        std::cout << "Successfuly connected to camera " << std::endl;

    long frameCounter = 0;

    std::time_t timeBegin = std::time(0);
    int tick = 0;

    cv::Mat frame;

    while (1)
    {
        cam.read(frame);

        cv::imshow("Sobel blurred Frame", cv::imshow("FPS test", frame);
        cv::waitKey(1);

        frameCounter++;

        std::time_t timeNow = std::time(0) - timeBegin;

        if (timeNow - tick >= 1)
        {
            tick++;
            cout << "Frames per second: " << frameCounter << endl;
            frameCounter = 0;
        }
    }

    return 0;
}

i while trying TickMeter class,i modified the @Eduardo 's and @Pedro' s code of @Eduardo for from thisthis post. post. i tried to correct the FPS calculation not to exceed 30fps

both code gives almost same results

#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
<opencv2/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
<opencv2/highgui.hpp>

class TickMeter
{
public:
    TickMeter();
    void start();
    void stop();

    int64 getTimeTicks() const;
    double getTimeMicro() const;
    double getTimeMilli() const;
    double getTimeSec()   const;
    int64 getCounter() const;

    void reset();
private:
    int64 counter;
    int64 sumTime;
    int64 startTime;
};

TickMeter::TickMeter()
{
    reset();
}
int64 TickMeter::getTimeTicks() const
{
    return sumTime;
}
double TickMeter::getTimeMicro() const
{
    return  getTimeMilli()*1e3;
}
double TickMeter::getTimeMilli() const
{
    return getTimeSec()*1e3;
}
double TickMeter::getTimeSec() const
{
    return (double)getTimeTicks()/cv::getTickFrequency();
}
int64 TickMeter::getCounter() const
{
    return counter;
}
void TickMeter::reset()
{
    startTime = 0;
    sumTime = 0;
    counter = 0;
}

void TickMeter::start()
{
    startTime = cv::getTickCount();
}
void TickMeter::stop()
{
    int64 time = cv::getTickCount();
    if ( startTime == 0 )
        return;
    ++counter;
    sumTime += ( time - startTime );
    startTime = 0;
}

int main(int argc, char**argv)
{
    cv::VideoCapture cam;
capture;

    // Read the video stream or open the web cam
    if( argc > 1)
        capture.open( argv[1] );
    else
        capture.open( 0 );

    if (!cam.open(0))
(!capture.isOpened())
    {
        std::cout << "Problem connecting to cam " << std::endl;
        return -1;
    }
    else
    if( argc == 1 )
    {
        std::cout << "Successfuly connected to camera " << std::endl;
        //capture.set(cv::CAP_PROP_FRAME_WIDTH,320);
        //capture.set(cv::CAP_PROP_FRAME_HEIGHT,240);
    }

    cv::Mat frame;

    double total_time = 0.0;
    int c,cpt = 30;

    double t = (double) cv::getTickCount();
c;
    TickMeter tm;

    do
    {
        t = ((double) cv::getTickCount() - t) / cv::getTickFrequency();
        total_time += t;
        cpt++;
        t = (double) cv::getTickCount();

        cam.read(frame);

tm.stop();
        tm.start();
        capture.read(frame);

        if( frame.empty() )
        {
            break;
        }

        if(tm.getCounter() > 0)
        {
            cv::putText(frame, cv::format("Average FPS=%d",cvRound(cpt/total_time)), FPS=%d",cvRound( tm.getCounter()/tm.getTimeSec())), cv::Point(30, 30), cv::FONT_HERSHEY_SIMPLEX, 1.0, 0.8, cv::Scalar(0,0,255));

        cv::imshow("test",         }

        cv::imshow("Video Capture Test", frame);

        c = cv::waitKey(30);
cv::waitKey(1);
    }
    while(c != 27);

    return 0;
}

another way way

( thanks to Pedro Batista )

#include <iostream>
#include <ctime>
#include <opencv2/imgproc/imgproc.hpp>
<opencv2/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/ocl.hpp>

using namespace std;
<opencv2/highgui.hpp>

int main()
main(int argc, char**argv)
{
    cv::VideoCapture cam;
capture;

    // Read the video stream or open the web cam
    if( argc > 1)
        capture.open( argv[1] );
    else
        capture.open( 0 );

    if (!cam.open(0))
(!capture.isOpened())
    {
        std::cout << "Problem connecting to cam " << std::endl;
        return -1;
    }
    else
    if( argc == 1 )
    {
        std::cout << "Successfuly connected to camera " << std::endl;

    long         //capture.set(cv::CAP_PROP_FRAME_WIDTH,320);
        //capture.set(cv::CAP_PROP_FRAME_HEIGHT,240);
    }

    int frameCounter = 0;
     int tick = 0;
    int fps;
    std::time_t timeBegin = std::time(0);
    int tick = 0;

    cv::Mat frame;

    while (1)
    {
        cam.read(frame);

        cv::imshow("FPS test", frame);
        cv::waitKey(1);
capture.read(frame);

        if( frame.empty() )
        {
            break;
        }

        frameCounter++;

        std::time_t timeNow = std::time(0) - timeBegin;

        if (timeNow - tick >= 1)
        {
            tick++;
            cout << "Frames per second: " << fps = frameCounter;
            frameCounter << endl;
            frameCounter = 0;
        }

        cv::putText(frame, cv::format("Average FPS=%d", fps ), cv::Point(30, 30), cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(0,0,255));
        cv::imshow("FPS test", frame);
        cv::waitKey(1);
    }

    return 0;
}