Use setMouseCallBack in class and show image in a loop

Hello,

The link is the way I use setMouseCallBack in my class. Before this, I show only images without problem. Showing image no longer work after I added waitKey(0) into the code, see the one behind setMouseCallBack. Mouse action was not captured without using this waitkey(0).

	cv::namedWindow("L");
	cv::setMouseCallback("L", onMouse, this);
	cv::waitKey(0);
	for (size_t i = 0; i < corners.size(); i++)
	{
		circle(copy, corners[i], radius, cv::Scalar(moRNG.uniform(0, 255), moRNG.uniform(0, 256), moRNG.uniform(0, 256)), cv::FILLED);
		cv::imshow("L", copy);
		cv::waitKey(1);
	} // end for

imshow windows only work while waitKey is executing.

waitKey(0) waits (forever) until a key is pressed.

waitKey(1) waits at most a millisecond or so, then it’s done and returning.

there must not be any waitKey calls inside of event handlers. they don’t belong there. they belong in a single thread, the main thread (other threads sometimes allowed, but always exactly one).

please post complete but minimal code to reproduce your issue. the code you posted here lacks the mouse callback function. the code on stackoverflow contains a bunch of stuff that is clearly irrelevant to the issue, hence a distraction.

2 Likes

2 posts were split to a new topic: waitKey without delay

Hello, @crackwitz .
The codes in my post is the main part of a member function of my class. The name could be classA::init, for example.

In main function, we simply declare an object of classA and call init() with the object.

solved with replacing waitkey(0) by waitkey(1)