Ask Your Question
0

imshow() very slow

asked 2016-06-16 00:27:36 -0600

bob_oi gravatar image

updated 2016-06-16 09:16:13 -0600

Xcode, Mac OS X 10.11.5, OpenCV 2.4.13, C++

I am showing images from vector, with the aim to flip through them as fast as possible:

for (int i = 0; i < imgs.size(); i++) {
    imshow( wndName, imgs[i]);
    waitKey(1);
}

The images are 900x900 pixels.

The rate I am achieving this way is 15 frames per second - which is of course very slow. The bottleneck is in imshow(). The rate does increase and approaches 60 fps if Image size is reduced significantly (like 1/10)

Is there a way to make imshow() faster? if not - are there alternative ways to show images that are faster on my platform?

Thanks

edit retag flag offensive close merge delete

Comments

2

cv::imshow is a convenience function more suited to prototyping and simple applications. You can try alternatives such as Qt and glfw. Another idea would be to display the images in a separate thread, and maybe even intentionally reduce the frame rate, skip every other image, for example. This would free your capture and processing threads to continue to hum along.

Der Luftmensch gravatar imageDer Luftmensch ( 2016-06-16 09:21:23 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2018-12-23 23:07:59 -0600

lbsweek gravatar image

https://stackoverflow.com/questions/2...

OpenCV 4 have resolved the issue, please update to new version.

edit flag offensive delete link more
0

answered 2016-06-16 10:34:06 -0600

i can't say anything about performance on Mac OS X but i tried with the code below on Windows.

the result FPS is 30 with reading from webcam.

and without reading a new frame the result FPS is 64

( note: when testing the code, you can press Space key to toggle reading new frame )

#include <iostream>
#include <ctime>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.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;
    int fps = 0;
    cam.read(frame);
    int key;
    bool read_frame = true;

    while (1)
    {
        if (read_frame)
        {
            cam.read(frame);
        }

        cv::resize(frame, frame, cv::Size(900, 900));
        cv::rectangle(frame, cv::Rect(0, 0, 900, 40), cv::Scalar(0, 0, 0), -1);
        cv::putText(frame, cv::format("Frames per second: %d - %d", fps, frameCounter), cv::Point(30, 30), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(255, 0, 0));

        cv::imshow("FPS test", frame);
        key = cv::waitKey(1);

        if (key == 32) // press Space key to toggle reading new frame
        {
            read_frame = !read_frame;
        }

        frameCounter++;

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

        if (timeNow - tick >= 1)
        {
            tick++;
            fps = frameCounter;
            frameCounter = 0;
        }
    }

    return 0;
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-06-16 00:26:12 -0600

Seen: 16,255 times

Last updated: Jun 16 '16