opencv 4.5.4 with StreamCam Plus not getting expected FPS

33 Views Asked by At

I am using a MSI GS66 Stealth with a logitech StreamCam Plus and openCV_4.5.4. The code is below. For some reason I am only able to get a max of ~30 fps despite setting the fps to 60. It should be noted that I can slow the fps to say 5 fps as shown in Figure 2 below...

According to logitech the streamCam Plus supports 60fps while in MJPEG mode, see Figure 2... I believe I have set this properly in the code via a call to:

...
int fourcc = cv::VideoWriter::fourcc('M', 'J', 'P', 'G');
cap.set(CAP_PROP_FOURCC, fourcc);
...

Additionally, I have tried to change the cameras settings in both the Logitech Capture and Logi Tune software. Currently, the camera is set to completely manual via the Logi tune software

enter image description here

Figure 1: supports 60fps while in MJPEG format

enter image description here

Figure 2: has 5 fps and loops at 5 fps

enter image description here

Figure 3: has 60 fps however it loops at ~30fps

#include<opencv2/opencv.hpp>//OpenCV header to use VideoCapture class//
#include<iostream>

using timer = std::chrono::system_clock;
timer::time_point clock_cur;
timer::time_point clock_last;

using namespace std;
using namespace cv;

int main() {

    using fmilliseconds = chrono::duration<float, std::milli>;
    //Mat myImage(240, 320, CV_8UC1);//Declaring a matrix to load the frames//
    Mat myImage;
    namedWindow("Video Player");//Declaring the video to show the video//
    VideoCapture cap(1,CAP_DSHOW+0);//Declaring an object to capture stream of frames from default camera//
    if (!cap.isOpened()) { //This section prompt an error message if no video stream is found//
        cout << "No video stream detected" << endl;
        system("pause");
        return-1;
    }

    //setUseOptimized(true);

    float avgDur = 0;
    bool useShortWindow = true;
    int maxWindowSize = 10;
    int curWindowSize = 0;
    int loopCnt = curWindowSize;

    bool bVal = true;
    int fourcc = cv::VideoWriter::fourcc('M', 'J', 'P', 'G');
    cap.set(CAP_PROP_FOURCC, fourcc);

    int val = 0;
    int val2 = 0;

    cap.set(CAP_PROP_FRAME_HEIGHT, 240);
    cap.set(CAP_PROP_FRAME_WIDTH, 320);

    //cap.set(CAP_PROP_ZOOM, 125);
    //cap.set(CAP_PROP_FOCUS, 10);
    bVal = cap.set(CAP_PROP_FPS, 60);
    
    //cap.set(CAP_PROP_AUTOFOCUS, 2);


    clock_cur = timer::now();
    clock_last = clock_cur;

    while (true) { //Taking an everlasting loop to show the video//

        clock_cur = timer::now();

        cap >> myImage;
        //Mat imgBlur;
        //GaussianBlur(myImage, imgBlur, Size(7, 7), 0);
        //GaussianBlur(myImage, myImage, Size(7, 7), 0);
        //cvtColor(myImage, myImage, COLOR_BGR2GRAY);
        //cvtColor(imgBlur, imgBlur, COLOR_BGR2GRAY);
        
        //Mat image = myImage;
        //for (int y = 0; y < myImage.rows; y++)
        //{
        //    for (int x = 0; x < myImage.cols; x++)
        //    {
        //        //Get pixel value
        //        int pixelValue = (int)myImage.at<uchar>(y, x);
        //
        //        if (pixelValue < 60)
        //            pixelValue = 0;
        //        else if (pixelValue > 175)
        //            pixelValue = 255;
        //        else
        //            pixelValue = 128;
        //
        //        myImage.at<uchar>(y,x) = pixelValue;
        //
        //    }
        //}

        //if (myImage.empty()) { //Breaking the loop if no video frame is detected//
            //break;
        //}

        imshow("No Blur", myImage);//Showing the video//
        //imshow("Video Player", imgBlur);//Showing the video//

            fmilliseconds Duration = clock_cur - clock_last;

            val = cap.get(CAP_PROP_FRAME_WIDTH);
            val2 = cap.get(CAP_PROP_FRAME_HEIGHT);

            if (useShortWindow && loopCnt < maxWindowSize)
                curWindowSize = loopCnt;
            else
            {
                useShortWindow = false;
                curWindowSize = maxWindowSize;
            }

        if (curWindowSize > 1)
        {
            avgDur = avgDur / curWindowSize * (curWindowSize - 1) + Duration.count() / 1000 / curWindowSize;
            cout << to_string(val) << " : " << to_string(val2) << " : " << to_string(1 / avgDur) << "\n";
        }
        else
        {
            avgDur = Duration.count() / 1000;
            cout << to_string(val) << " : " << to_string(val2) << " : " << to_string(1 / Duration.count()) << "\n";
        }
            
        char c = (char)waitKey(1);//Allowing 25 milliseconds frame processing time and initiating break condition//
        //if (c == 27) { //If 'Esc' is entered break the loop//
        //    break;
        //}
        clock_last = clock_cur;
        loopCnt = useShortWindow ? loopCnt + 1 : maxWindowSize;
    }

    cap.release();//Releasing the buffer memory//
    return 0;
}

Figure 4: Code has been trimmed to just print size and fps.

Certainly, I am missing something. Any help would be greatly appreciated.

0

There are 0 best solutions below