Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

accurate method to calculate framerate of input stream

Related to the latest questions of mine regarding kinect one and opencv I have a loop where I am transforming the images obtained from kinect to opencv matrices and then I am showing these matrices, the procedure is quite similar to the way we are using with the VideoCapture() class. A snippet code can be seen below:

    while( 1 ){
        // Frame

        // get color image
        IColorFrame* pColorFrame = nullptr;
        hResult = pColorReader->AcquireLatestFrame( &pColorFrame );
        if( SUCCEEDED( hResult ) ){
            hResult = pColorFrame->CopyConvertedFrameDataToArray( bufferSizeColor, reinterpret_cast<BYTE*>( colorBufferMat.data ), ColorImageFormat::ColorImageFormat_Bgra );
            // resize for visual reasons
            if( SUCCEEDED( hResult ) ){
                cv::resize( colorBufferMat, colorMat, cv::Size(), 0.5, 0.5 );
            }
        }

            // get depth image
//        // method 1
//        IDepthFrame* pDepthFrame = nullptr;
//        hResult = pDepthReader->AcquireLatestFrame( &pDepthFrame );
//        if( SUCCEEDED( hResult ) ){
//            hResult = pDepthFrame->AccessUnderlyingBuffer( &bufferSizeDepth, reinterpret_cast<UINT16**>( &depthBufferMat.data ) );
//            if( SUCCEEDED( hResult ) ){
//                depthBufferMat.convertTo( depthMat, CV_8U, 0.05f/*-255.0f / 8000.0f, 255.0f*/ );
//            }
//        }

        // method 2
        IDepthFrame* pDepthFrame = nullptr;
        hResult = pDepthReader->AcquireLatestFrame( &pDepthFrame );
        if( SUCCEEDED( hResult ) ){
            unsigned int bufferSize = 0;
            unsigned short* buffer = nullptr;
            hResult = pDepthFrame->AccessUnderlyingBuffer( &bufferSize, &buffer );
            if( SUCCEEDED( hResult ) ){
                for( int y = 0; y < depthHeight; y++ ){
                    for( int x = 0; x < depthWidth; x++ ){
                        unsigned int index = y * depthWidth + x;
                        BYTE intensity = buffer[ index ] == 0 || buffer[ index ] > 4095/*2047*/ ? 0 : 255 - (BYTE)(((float)buffer[ index ] / 4095.0f/*2047.0f*/) * 255.0f);
                        depthMat.at<unsigned char>( y, x ) = intensity;
                    }
                }
            }
//            memcpy(depth_im.data, buffer, depthHeight*depthWidth*sizeof(UINT16)); // creating an opencv image from the raw data in buffer above with the raw data
        }

        // get a colormap of the depth image
        applyColorMap(depthMat, colorDepthMat, COLORMAP_JET);

        // get infrared image
        IInfraredFrame* pInfraredFrame = nullptr;
        hResult = pInfraredReader->AcquireLatestFrame( &pInfraredFrame );
        if( SUCCEEDED( hResult ) ){
            unsigned int bufferSize = 0;
            unsigned short* buffer = nullptr;
            hResult/*IR*/ = pInfraredFrame->AccessUnderlyingBuffer( &bufferSize, &buffer );
            if( SUCCEEDED( hResult ) ){
                for( int y = 0; y < irHeight; y++ ){
                    for( int x = 0; x < irWidth; x++ ){
                        unsigned int index = y * irWidth + x;
                        infraredMat.at<unsigned char>( y, x ) = buffer[ index ] >> 8;
                    }
                }
            }
        }

        SafeRelease( pColorFrame );
        SafeRelease( pDepthFrame );
        SafeRelease( pInfraredFrame );

        cv::imshow( "Color", colorMat );
        // raw size
//        cv::imshow( "Color", colorBufferMat );
        cv::imshow( "Depth", depthMat );
        cv::imshow( "Color Depth", colorDepthMat );
        cv::imshow( "Infrared", infraredMat );

        int key = waitKey(1);    
        if( key == VK_ESCAPE ){
            break;
        }
    }

the question now is, is there any way to accurate measure the framerate that I get from kinect? I guess it does not have to do strictly related to kinect it could be any normal camera. Searching around I could something clear. Officially it says that kinect supports 30 fps so I would like to see if I get something close to that.

accurate method to calculate framerate of input stream

Related to the latest questions of mine regarding kinect one and opencv I have a loop where I am transforming the images obtained from kinect to opencv matrices and then I am showing these matrices, the procedure is quite similar to the way we are using with the VideoCapture() class. A snippet code can be seen below:

    while( 1 ){
        // Frame

        // get color image
        IColorFrame* pColorFrame = nullptr;
        hResult = pColorReader->AcquireLatestFrame( &pColorFrame );
        if( SUCCEEDED( hResult ) ){
            hResult = pColorFrame->CopyConvertedFrameDataToArray( bufferSizeColor, reinterpret_cast<BYTE*>( colorBufferMat.data ), ColorImageFormat::ColorImageFormat_Bgra );
            // resize for visual reasons
            if( SUCCEEDED( hResult ) ){
                cv::resize( colorBufferMat, colorMat, cv::Size(), 0.5, 0.5 );
            }
        }

            // get depth image
//        // method 1
//        IDepthFrame* pDepthFrame = nullptr;
//        hResult = pDepthReader->AcquireLatestFrame( &pDepthFrame );
//        if( SUCCEEDED( hResult ) ){
//            hResult = pDepthFrame->AccessUnderlyingBuffer( &bufferSizeDepth, reinterpret_cast<UINT16**>( &depthBufferMat.data ) );
//            if( SUCCEEDED( hResult ) ){
//                depthBufferMat.convertTo( depthMat, CV_8U, 0.05f/*-255.0f / 8000.0f, 255.0f*/ );
//            }
//        }

        // method 2
        IDepthFrame* pDepthFrame = nullptr;
        hResult = pDepthReader->AcquireLatestFrame( &pDepthFrame );
        if( SUCCEEDED( hResult ) ){
            unsigned int bufferSize = 0;
            unsigned short* buffer = nullptr;
            hResult = pDepthFrame->AccessUnderlyingBuffer( &bufferSize, &buffer );
            if( SUCCEEDED( hResult ) ){
                for( int y = 0; y < depthHeight; y++ ){
                    for( int x = 0; x < depthWidth; x++ ){
                        unsigned int index = y * depthWidth + x;
                        BYTE intensity = buffer[ index ] == 0 || buffer[ index ] > 4095/*2047*/ ? 0 : 255 - (BYTE)(((float)buffer[ index ] / 4095.0f/*2047.0f*/) * 255.0f);
                        depthMat.at<unsigned char>( y, x ) = intensity;
                    }
                }
            }
//            memcpy(depth_im.data, buffer, depthHeight*depthWidth*sizeof(UINT16)); // creating an opencv image from the raw data in buffer above with the raw data
        }

        // get a colormap of the depth image
        applyColorMap(depthMat, colorDepthMat, COLORMAP_JET);

        // get infrared image
        IInfraredFrame* pInfraredFrame = nullptr;
        hResult = pInfraredReader->AcquireLatestFrame( &pInfraredFrame );
        if( SUCCEEDED( hResult ) ){
            unsigned int bufferSize = 0;
            unsigned short* buffer = nullptr;
            hResult/*IR*/ = pInfraredFrame->AccessUnderlyingBuffer( &bufferSize, &buffer );
            if( SUCCEEDED( hResult ) ){
                for( int y = 0; y < irHeight; y++ ){
                    for( int x = 0; x < irWidth; x++ ){
                        unsigned int index = y * irWidth + x;
                        infraredMat.at<unsigned char>( y, x ) = buffer[ index ] >> 8;
                    }
                }
            }
        }

        SafeRelease( pColorFrame );
        SafeRelease( pDepthFrame );
        SafeRelease( pInfraredFrame );

        cv::imshow( "Color", colorMat );
        // raw size
//        cv::imshow( "Color", colorBufferMat );
        cv::imshow( "Depth", depthMat );
        cv::imshow( "Color Depth", colorDepthMat );
        cv::imshow( "Infrared", infraredMat );

        int key = waitKey(1);    
        if( key == VK_ESCAPE ){
            break;
        }
    }

the question now is, is there any way to accurate measure the framerate that I get from kinect? I guess it does not have to do strictly related to kinect it could be any normal camera. Searching around I could something clear. Officially it says that kinect supports 30 fps so I would like to see if I get something close to that.

Update

#include <iostream>
#include <time.h>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main()
{    
    VideoCapture cap1(0);
    VideoCapture cap2(1);

    // check if video is loaded successfuly
    if(!cap1.isOpened() || !cap2.isOpened())
    {
        cout << "Problem opening streams!" << endl;
        return -1;
    }

    Mat frame1, frame2;

    VideoWriter rec1("test.avi", -1/*CV_FOURCC('M','J','P','G')*/, 25, Size(640, 480), true); // setting cv_fourcc manually does not work in windows
    VideoWriter rec2("test2.avi", -1/*CV_FOURCC('D','I','V','X')*/, 25, Size(640, 480), true);

    if ( !rec1.isOpened() ) //if not initialize the VideoWriter successfully, exit the program
   {
        cout << "ERROR: Failed to write the rgb video" << endl;
        return -1;
   }
    if ( !rec2.isOpened() ) //if not initialize the VideoWriter successfully, exit the program
    {
        cout << "ERROR: Failed to write the depth video" << endl;
        return -1;
    }

    bool flag = false;

    double time = 0;
    int numberOfFrames = 0;

    int64 t0 = cv::getTickCount();

    while(1)
    {

        cap1 >> frame1;
        cap2 >> frame2;

        numberOfFrames++;

        imshow("frame1", frame1);
        imshow("frame2", frame2);


        int key = waitKey(1);

        if(key == ' ')
        {
            flag = true;
            t0 = cv::getTickCount();
            numberOfFrames = 0;
            cout << "Started Recording!" << endl;
        }

        if(flag && !frame1.empty())
        {
            rec1 << frame1;
            rec2 << frame2;
        }

        if( key == 'q' ){
            break;
        }

        time = (cv::getTickCount() - t0) / cv::getTickFrequency(); // seconds
        double averageTime = time / numberOfFrames; // seconds
        cout << "Measured time: "
             << averageTime * 1000 << "ms"
             << " ~= " << cvRound(1 / averageTime) << "fps" << endl;
    }

    rec1.release();
    rec2.release();

    cout << "Hello World!" << endl;
    return 0;
}

accurate method to calculate framerate of input stream

Related to the latest questions of mine regarding kinect one and opencv I have a loop where I am transforming the images obtained from kinect to opencv matrices and then I am showing these matrices, the procedure is quite similar to the way we are using with the VideoCapture() class. A snippet code can be seen below:

    while( 1 ){
        // Frame

        // get color image
        IColorFrame* pColorFrame = nullptr;
        hResult = pColorReader->AcquireLatestFrame( &pColorFrame );
        if( SUCCEEDED( hResult ) ){
            hResult = pColorFrame->CopyConvertedFrameDataToArray( bufferSizeColor, reinterpret_cast<BYTE*>( colorBufferMat.data ), ColorImageFormat::ColorImageFormat_Bgra );
            // resize for visual reasons
            if( SUCCEEDED( hResult ) ){
                cv::resize( colorBufferMat, colorMat, cv::Size(), 0.5, 0.5 );
            }
        }

            // get depth image
//        // method 1
//        IDepthFrame* pDepthFrame = nullptr;
//        hResult = pDepthReader->AcquireLatestFrame( &pDepthFrame );
//        if( SUCCEEDED( hResult ) ){
//            hResult = pDepthFrame->AccessUnderlyingBuffer( &bufferSizeDepth, reinterpret_cast<UINT16**>( &depthBufferMat.data ) );
//            if( SUCCEEDED( hResult ) ){
//                depthBufferMat.convertTo( depthMat, CV_8U, 0.05f/*-255.0f / 8000.0f, 255.0f*/ );
//            }
//        }

        // method 2
        IDepthFrame* pDepthFrame = nullptr;
        hResult = pDepthReader->AcquireLatestFrame( &pDepthFrame );
        if( SUCCEEDED( hResult ) ){
            unsigned int bufferSize = 0;
            unsigned short* buffer = nullptr;
            hResult = pDepthFrame->AccessUnderlyingBuffer( &bufferSize, &buffer );
            if( SUCCEEDED( hResult ) ){
                for( int y = 0; y < depthHeight; y++ ){
                    for( int x = 0; x < depthWidth; x++ ){
                        unsigned int index = y * depthWidth + x;
                        BYTE intensity = buffer[ index ] == 0 || buffer[ index ] > 4095/*2047*/ ? 0 : 255 - (BYTE)(((float)buffer[ index ] / 4095.0f/*2047.0f*/) * 255.0f);
                        depthMat.at<unsigned char>( y, x ) = intensity;
                    }
                }
            }
//            memcpy(depth_im.data, buffer, depthHeight*depthWidth*sizeof(UINT16)); // creating an opencv image from the raw data in buffer above with the raw data
        }

        // get a colormap of the depth image
        applyColorMap(depthMat, colorDepthMat, COLORMAP_JET);

        // get infrared image
        IInfraredFrame* pInfraredFrame = nullptr;
        hResult = pInfraredReader->AcquireLatestFrame( &pInfraredFrame );
        if( SUCCEEDED( hResult ) ){
            unsigned int bufferSize = 0;
            unsigned short* buffer = nullptr;
            hResult/*IR*/ = pInfraredFrame->AccessUnderlyingBuffer( &bufferSize, &buffer );
            if( SUCCEEDED( hResult ) ){
                for( int y = 0; y < irHeight; y++ ){
                    for( int x = 0; x < irWidth; x++ ){
                        unsigned int index = y * irWidth + x;
                        infraredMat.at<unsigned char>( y, x ) = buffer[ index ] >> 8;
                    }
                }
            }
        }

        SafeRelease( pColorFrame );
        SafeRelease( pDepthFrame );
        SafeRelease( pInfraredFrame );

        cv::imshow( "Color", colorMat );
        // raw size
//        cv::imshow( "Color", colorBufferMat );
        cv::imshow( "Depth", depthMat );
        cv::imshow( "Color Depth", colorDepthMat );
        cv::imshow( "Infrared", infraredMat );

        int key = waitKey(1);    
        if( key == VK_ESCAPE ){
            break;
        }
    }

the question now is, is there any way to accurate measure the framerate that I get from kinect? I guess it does not have to do strictly related to kinect it could be any normal camera. Searching around I could something clear. Officially it says that kinect supports 30 fps so I would like to see if I get something close to that.

Update

#include <iostream>
#include <time.h>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main()
{    
    VideoCapture cap1(0);
    VideoCapture cap2(1);

    // check if video is loaded successfuly
    if(!cap1.isOpened() || !cap2.isOpened())
    {
        cout << "Problem opening streams!" << endl;
        return -1;
    }

    Mat frame1, frame2;

    VideoWriter rec1("test.avi", -1/*CV_FOURCC('M','J','P','G')*/, 25, Size(640, 480), true); // setting cv_fourcc manually does not work in windows
windows, also do not know why
    VideoWriter rec2("test2.avi", -1/*CV_FOURCC('D','I','V','X')*/, 25, Size(640, 480), true);

    if ( !rec1.isOpened() ) //if not initialize the VideoWriter successfully, exit the program
   {
        cout << "ERROR: Failed to write the rgb video" << endl;
        return -1;
   }
    if ( !rec2.isOpened() ) //if not initialize the VideoWriter successfully, exit the program
    {
        cout << "ERROR: Failed to write the depth video" << endl;
        return -1;
    }

    bool flag = false;

    double time = 0;
    int numberOfFrames = 0;

    int64 t0 = cv::getTickCount();

    while(1)
    {

        cap1 >> frame1;
        cap2 >> frame2;

        numberOfFrames++;

        imshow("frame1", frame1);
        imshow("frame2", frame2);


        int key = waitKey(1);

        if(key == ' ')
        {
            flag = true;
            t0 = cv::getTickCount();
            numberOfFrames = 0;
            cout << "Started Recording!" << endl;
        }

        if(flag && !frame1.empty())
        {
            rec1 << frame1;
            rec2 << frame2;
        }

        if( key == 'q' ){
            break;
        }

        time = (cv::getTickCount() - t0) / cv::getTickFrequency(); // seconds
        double averageTime = time / numberOfFrames; // seconds
        cout << "Measured time: "
             << averageTime * 1000 << "ms"
             << " ~= " << cvRound(1 / averageTime) << "fps" << endl;
    }

    rec1.release();
    rec2.release();

    cout << "Hello World!" << endl;
    return 0;
}