Click here to Skip to main content
15,889,879 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am new to programming as a whole, but have been able to pick things up fast thanks to you guys.
I have a couple of pictures and I would like to display each picture for as short a period of time as possible. I've been using Opencv so far and it works great. I've been able to figure out how to get the pictures i need and display them using cv::imshow and cv::waitKey(), but the waitKey as we all know it inaccurate in its timing and also doesn't allow me show the pictures for to as high a time resolution as I would like to go.
So far, I've tried using alternatives such as an event loop and limiting the run time of the imshow thread, but nothing has worked to my satisfaction.
First off, I would like to figure out how long it takes for imshow to display an image on the screen (I am guessing it has a lot to do with the CPU speed).

Lastly (which is my actual question), is there a way to do this (even if hypothetical and impractical) and if so how do I go about it (your answers are greatly valued).

What I have tried:

C++
#include <chrono>
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <cstdint>
#include <thread>
#include <opencv2 core="" core.hpp="">
#include <opencv2 highgui="" highgui.hpp="">

static std::condition_variable condvar;
static std::mutex mut;
static bool stop = false;
cv::String folder = "picture location...";
std::vector<cv::string> filenames;


void
loopFunction()
{
	using delta = std::chrono::duration<std::int64_t, std::ratio<1,="" 10000000="">>;
	auto next = std::chrono::steady_clock::now() + delta{ 1 };
	std::unique_lock<std::mutex> lk(mut);
	while (!stop)
	{
		mut.unlock();

		// Display pictures
		for (size_t i = 0; i < 5; ++i)
		{
			if (i > 1) {
				i = 0;
			}

			cv::Mat myImage = cv::imread(filenames[i]);
			cv::imshow("Boiii", myImage);

			// Wait for the next 1/10000000 sec
			mut.lock();
			condvar.wait_until(lk, next, [] {return false; });
			next += delta{ 1 };
		}
	}
}

int main()
{
	cv::glob(folder, filenames);
	using namespace std::chrono_literals;
	std::thread t{ loopFunction };

	std::this_thread::sleep_for(5s);
	{
		std::lock_guard<std::mutex> lk(mut);
		stop = true;
	}
	t.join();
}
Posted
Updated 23-Oct-16 21:25pm
v2
Comments
Mehdi Gholam 24-Oct-16 1:16am    
Why? even at 60fps that gives 16ms.
Dave Kreskowiak 24-Oct-16 1:28am    
You timing is subjective. What do you call "showing an image"? Is it the time it takes to move the image to the video buffer? The time it takes to load the image and display it? The monitor may not respond to the changes being made until the next refresh and the you even have to wait for liquid crystal to respond in the monitor for it to be actually "shown".

So what exactly are you timing? The best resolution timer you're going to get in Windows in the https://msdn.microsoft.com/en-us/library/windows/desktop/dn553408(v=vs.85).aspx. Even then, because Windows isn't a real-time operating system, you're not going to get the exact time down to the resolution and/or accuracy you want.

You cannot do that. Such a time resolution is not available on Windows.
 
Share this answer
 
You can't do that with a computer monitor. You need to invent your own HARDWARE for that purpose. But anyway, showing an image for less than one millionth of a second seems to be very useless...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900