Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing waitKey commands to be universal #7105

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions apps/createsamples/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1270,7 +1270,7 @@ void cvCreateTrainingSamples( const char* filename,
if( showsamples )
{
cvShowImage( "Sample", &sample );
if( cvWaitKey( 0 ) == 27 )
if( (cvWaitKey( 0 ) & 0xFF) == 27 )
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In cases like here, I would also like to replace 27 by 'ESC' because it simply is more understandable that we are looking the value of the ESC key. If in some future those backend values would change, the code itself would not need updating.

What you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure whether there is a dedicated escape sequence or standard constant for ESC value. If we replace it with 'ESC' it will be converted to (int)4543299 at least for VStudio.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checked, it seems that on Ubuntu calling the code

using namespace std;

int main( int argc, const char** argv )
{
    int key = 0;
    key = 'ESC';
    cerr << key << endl;

    return 0;
}

yields the same output as you!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One way could be to define a global KEYSTROKE_ESC and use that ... but that might be a bit overkill?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some more investigation shows that ESC is not a printable char, and thus there is no sequence assigned to that. We will have to stick to 27 or define an enum for non printable ASCI values, which could be interesting to do so.

Copy link
Contributor

@terfendail terfendail Aug 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could still use 27 for ESC value. Enum definition for ESC and a few other useful values only will be inconsistent while all other(and IMO most of) values included to the enum will be never used.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Lets keep it at 27 then!

{
showsamples = 0;
}
Expand Down Expand Up @@ -1402,7 +1402,7 @@ void cvCreateTestSamples( const char* infoname,
if( showsamples )
{
cvShowImage( "Image", &cvbgreader->src );
if( cvWaitKey( 0 ) == 27 )
if( (cvWaitKey( 0 ) & 0xFF) == 27 )
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@terfendail Decided to leave this here. Because the sample is mainly using C functionality, and since waitChar is not available as C version.

{
showsamples = 0;
}
Expand Down Expand Up @@ -1525,7 +1525,7 @@ int cvCreateTrainingSamplesFromInfo( const char* infoname, const char* vecfilena
if( showsamples )
{
cvShowImage( "Sample", sample );
if( cvWaitKey( 0 ) == 27 )
if( (cvWaitKey( 0 ) & 0xFF) == 27 )
{
showsamples = 0;
}
Expand Down Expand Up @@ -1672,12 +1672,12 @@ void cvShowVecSamples( const char* filename, int winwidth, int winheight,
icvGetTraininDataFromVec( sample, &file );
if( scale != 1.0 ) cvResize( sample, scaled_sample, CV_INTER_LINEAR);
cvShowImage( "Sample", scaled_sample );
if( cvWaitKey( 0 ) == 27 ) break;
if( (cvWaitKey( 0 ) & 0xFF) == 27 ) break;
}
if( scaled_sample && scaled_sample != sample ) cvReleaseMat( &scaled_sample );
cvReleaseMat( &sample );
cvFree( &file.vector );
}
fclose( file.input );
}
}
}
2 changes: 1 addition & 1 deletion apps/interactive-calibration/calibPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ PipelineExitStatus CalibPipeline::start(std::vector<cv::Ptr<FrameProcessor> > pr
for (std::vector<cv::Ptr<FrameProcessor> >::iterator it = processors.begin(); it != processors.end(); ++it)
processedFrame = (*it)->processFrame(processedFrame);
cv::imshow(mainWindowName, processedFrame);
int key = cv::waitKey(CAP_DELAY);
char key = cv::waitChar(CAP_DELAY);

if(key == 27) // esc
return Finished;
Expand Down
4 changes: 2 additions & 2 deletions doc/tutorials/imgproc/imgtrans/remap/remap.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ Explanation
while( true )
{
/// Each 1 sec. Press ESC to exit the program
int c = waitKey( 1000 );
char c = waitChar( 1000 );

if( (char)c == 27 )
if( c == 27 )
{ break; }

/// Update map_x & map_y. Then apply remap
Expand Down
1 change: 1 addition & 0 deletions doc/tutorials/imgproc/pyramids/pyramids.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Let's check the general structure of the program:

- Perform an infinite loop waiting for user input.
@snippet cpp/tutorial_code/ImgProc/Pyramids.cpp infinite_loop

Our program exits if the user presses *ESC*. Besides, it has two options:

- **Perform upsampling (after pressing 'u')**
Expand Down
22 changes: 21 additions & 1 deletion modules/highgui/include/opencv2/highgui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ It provides easy interface to:

Mat img2,img3;

while( waitKey(33) != 27 )
while( waitChar(33) != 27 )
{
img1.convertTo(img2,-1,1,value);
video >> img3;
Expand Down Expand Up @@ -338,6 +338,26 @@ If there are several HighGUI windows, any of them can be active.
*/
CV_EXPORTS_W int waitKey(int delay = 0);

/** @brief Waits for a pressed key and returns the corresponding char value (ASCII) assigned.

The function waitChar waits for a key event infinitely (when \f$\texttt{delay}\leq 0\f$ ) or for delay
milliseconds, when it is positive. It returns the ASCII code of the pressed key as a char value. If no key was pressed before
the specified time has elapsed, -1 (most systems) or 255 (if char == unsigned char) is returned depending on the system available.

@note

There is a minor issue for `char == unsigned char systems`. Return value or timeout could be mixed up with value of valid `'\0xFF'` symbol.
However this is an extremely rare usecase, and is thus ignored for the moment.

@note

The function only works if there is at least one HighGUI window created and the window is active.
If there are several HighGUI windows, any of them can be active.

@param delay Delay in milliseconds. 0 is the special value that means "forever".
*/
CV_EXPORTS_W char waitChar(int delay = 0);

/** @brief Displays an image in the specified window.

The function imshow displays an image in the specified window. If the window was created with the
Expand Down
9 changes: 9 additions & 0 deletions modules/highgui/src/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,15 @@ int cv::waitKey(int delay)
return cvWaitKey(delay);
}

char cv::waitChar(int delay){
#if CHAR_MIN==SCHAR_MIN
int intKey = cv::waitKey(delay) & 0xFF;
return (char)(intKey & 0x80 ? intKey - 0x100 : intKey);
#else
return (char)(cv::waitKey(delay) & 0xff);
#endif
}

int cv::createTrackbar(const String& trackbarName, const String& winName,
int* value, int count, TrackbarCallback callback,
void* userdata)
Expand Down
6 changes: 6 additions & 0 deletions modules/python/src2/cv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,12 @@ bool pyopencv_to(PyObject* obj, double& value, const char* name)
return !PyErr_Occurred();
}

template<>
PyObject* pyopencv_from(const char& value)
{
return PyInt_FromLong(value);
}

template<>
PyObject* pyopencv_from(const float& value)
{
Expand Down
4 changes: 2 additions & 2 deletions modules/python/test/tst_scene_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def resetTime(self):
img = render.getNextFrame()
cv2.imshow('img', img)

ch = 0xFF & cv2.waitKey(3)
ch = cv2.waitChar(3)
if ch == 27:
break
cv2.destroyAllWindows()
cv2.destroyAllWindows()
2 changes: 1 addition & 1 deletion samples/cpp/3calibration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ int main( int argc, char** argv )
for( k = 0; k < small_canvas.rows; k += 16 )
line(small_canvas, Point(0, k), Point(small_canvas.cols, k), Scalar(0,255,0), 1);
imshow("rectified", small_canvas);
int c = waitKey(0);
char c = waitChar(0);
if( c == 27 || c == 'q' || c == 'Q' )
break;
}
Expand Down
2 changes: 1 addition & 1 deletion samples/cpp/autofocus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ int main(int argc, char ** argv)
}

imshow(windowOriginal, frame);
switch (key = static_cast<char>(waitKey(30)))
switch (key = waitChar(30))
{
case 'k': // focus out
cap.set(CAP_PROP_ZOOM, 100);
Expand Down
2 changes: 1 addition & 1 deletion samples/cpp/bgfg_segm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ int main(int argc, const char** argv)
if(!bgimg.empty())
imshow("mean background image", bgimg );

char k = (char)waitKey(30);
char k = waitChar(30);
if( k == 27 ) break;
if( k == ' ' )
{
Expand Down
8 changes: 4 additions & 4 deletions samples/cpp/calibration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,9 +493,9 @@ int main( int argc, char** argv )
}

imshow("Image View", view);
int key = 0xff & waitKey(capture.isOpened() ? 50 : 500);
char key = waitChar(capture.isOpened() ? 50 : 500);

if( (key & 255) == 27 )
if( key == 27 )
break;

if( key == 'u' && mode == CALIBRATED )
Expand Down Expand Up @@ -536,8 +536,8 @@ int main( int argc, char** argv )
//undistort( view, rview, cameraMatrix, distCoeffs, cameraMatrix );
remap(view, rview, map1, map2, INTER_LINEAR);
imshow("Image View", rview);
int c = 0xff & waitKey();
if( (c & 255) == 27 || c == 'q' || c == 'Q' )
char c = waitChar();
if( c == 27 || c == 'q' || c == 'Q' )
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion samples/cpp/camshiftdemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ int main( int argc, const char** argv )
imshow( "CamShift Demo", image );
imshow( "Histogram", histimg );

char c = (char)waitKey(10);
char c = waitChar(10);
if( c == 27 )
break;
switch(c)
Expand Down
2 changes: 1 addition & 1 deletion samples/cpp/cloning_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ int main()

for(;;)
{
char key = (char) waitKey(0);
char key = waitChar(0);

if(key == 'd' && flag3 == 0)
Copy link
Contributor

@apavlenko apavlenko Aug 18, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@StevenPuttemans , @alalek don't you think that after making c of type int this comparison can be true or false depending on the signed or unsigned char type default? (I mean the case when the symbol code is more than 127? for 'd' it will work anyway!)
I would prefer leaving char type for c...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @apavlenko, I guess I am missing what you are trying to say here. Could you explain where you would like in INT cast to ensure that we have no loss of values above 127? Or am I completely missing your point?

Copy link
Contributor

@apavlenko apavlenko Aug 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean that the int key = waitKey(0) & 0xFF; is always non-negative, while char constant ('*') can be negative when the char type is signed and the symbol ASCII code is more than 127 - do you agree?
But in reality people will unlikely use regional-specific symbols is such case, so the issue is minor.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I get your point and agree, but isn't it best to serve a solution that tends to mass?
Maybe we should add a notice somewhere covering this special case?

{
Expand Down
3 changes: 1 addition & 2 deletions samples/cpp/convexhull.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ int main( int argc, char** argv )

for(;;)
{
char key;
int i, count = (unsigned)rng%100 + 1;

vector<Point> points;
Expand Down Expand Up @@ -58,7 +57,7 @@ int main( int argc, char** argv )

imshow("hull", img);

key = (char)waitKey();
char key = waitChar();
if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC'
break;
}
Expand Down
4 changes: 2 additions & 2 deletions samples/cpp/detect_mser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,8 @@ static void DrawOpenGLMSER(Mat img, Mat result)
for (;;)
{
updateWindow("OpenGL");
int key = waitKey(40);
if ((key & 0xff) == 27)
char key = waitChar(40);
if (key == 27)
break;
if (key == 0x20)
rotateEnable = !rotateEnable;
Expand Down
2 changes: 1 addition & 1 deletion samples/cpp/distrans.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ int main( int argc, const char** argv )
// Call to update the view
onTrackbar(0, 0);

int c = waitKey(0) & 255;
char c = waitChar(0);

if( c == 27 )
break;
Expand Down
12 changes: 6 additions & 6 deletions samples/cpp/facedetect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ int main( int argc, const char** argv )
}
if( inputName.empty() || (isdigit(inputName[0]) && inputName.size() == 1) )
{
int c = inputName.empty() ? 0 : inputName[0] - '0';
if(!capture.open(c))
cout << "Capture from camera #" << c << " didn't work" << endl;
int camera = inputName.empty() ? 0 : inputName[0] - '0';
if(!capture.open(camera))
cout << "Capture from camera #" << camera << " didn't work" << endl;
}
else if( inputName.size() )
{
Expand Down Expand Up @@ -104,7 +104,7 @@ int main( int argc, const char** argv )
Mat frame1 = frame.clone();
detectAndDraw( frame1, cascade, nestedCascade, scale, tryflip );

int c = waitKey(10);
char c = waitChar(10);
if( c == 27 || c == 'q' || c == 'Q' )
break;
}
Expand All @@ -127,7 +127,7 @@ int main( int argc, const char** argv )
char buf[1000+1];
while( fgets( buf, 1000, f ) )
{
int len = (int)strlen(buf), c;
int len = (int)strlen(buf);
while( len > 0 && isspace(buf[len-1]) )
len--;
buf[len] = '\0';
Expand All @@ -136,7 +136,7 @@ int main( int argc, const char** argv )
if( !image.empty() )
{
detectAndDraw( image, cascade, nestedCascade, scale, tryflip );
c = waitKey(0);
char c = waitChar(0);
if( c == 27 || c == 'q' || c == 'Q' )
break;
}
Expand Down
6 changes: 3 additions & 3 deletions samples/cpp/ffilldemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ int main( int argc, char** argv )
{
imshow("image", isColor ? image : gray);

int c = waitKey(0);
if( (c & 255) == 27 )
char c = waitChar(0);
if( c == 27 )
{
cout << "Exiting ...\n";
break;
}
switch( (char)c )
switch( c )
{
case 'c':
if( isColor )
Expand Down
4 changes: 2 additions & 2 deletions samples/cpp/grabcut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,8 @@ int main( int argc, char** argv )

for(;;)
{
int c = waitKey(0);
switch( (char) c )
char c = waitChar(0);
switch( c )
{
case '\x1b':
cout << "Exiting ..." << endl;
Expand Down
2 changes: 1 addition & 1 deletion samples/cpp/image_sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ int main(int argc, char** argv)

imshow("Image sequence | press ESC to close", image);

if(waitKey(500) == 27)
if( waitChar(500) == 27)
break;
}

Expand Down
2 changes: 1 addition & 1 deletion samples/cpp/inpaint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ int main( int argc, char** argv )

for(;;)
{
char c = (char)waitKey();
char c = waitChar();

if( c == 27 )
break;
Expand Down
4 changes: 2 additions & 2 deletions samples/cpp/kalman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ int main(int, char**)
Mat state(2, 1, CV_32F); /* (phi, delta_phi) */
Mat processNoise(2, 1, CV_32F);
Mat measurement = Mat::zeros(1, 1, CV_32F);
char code = (char)-1;
char code = 0;
Copy link
Contributor

@terfendail terfendail Aug 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it could be left uninitialized.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we leave it unitialized, then OpenCV generates a warning of an uninitialized char, which is seen as an error due to the threat warnings as errors compiler flag.


for(;;)
{
Expand Down Expand Up @@ -89,7 +89,7 @@ int main(int, char**)
state = KF.transitionMatrix*state + processNoise;

imshow( "Kalman", img );
code = (char)waitKey(100);
code = waitChar(100);

if( code > 0 )
break;
Expand Down
2 changes: 1 addition & 1 deletion samples/cpp/kmeans.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ int main( int /*argc*/, char** /*argv*/ )

imshow("clusters", img);

char key = (char)waitKey();
char key = waitChar();
if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC'
break;
}
Expand Down
4 changes: 2 additions & 2 deletions samples/cpp/laplace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ int main( int argc, char** argv )
convertScaleAbs(laplace, result, (sigma+1)*0.25);
imshow("Laplacian", result);

int c = waitKey(30);
char c = waitChar(30);
if( c == ' ' )
smoothType = smoothType == GAUSSIAN ? BLUR : smoothType == BLUR ? MEDIAN : GAUSSIAN;
if( c == 'q' || c == 'Q' || (c & 255) == 27 )
if( c == 'q' || c == 'Q' || c == 27 )
break;
}

Expand Down
2 changes: 1 addition & 1 deletion samples/cpp/lkdemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ int main( int argc, char** argv )
needToInit = false;
imshow("LK Demo", image);

char c = (char)waitKey(10);
char c = waitChar(10);
if( c == 27 )
break;
switch( c )
Expand Down
2 changes: 1 addition & 1 deletion samples/cpp/minarea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ int main( int /*argc*/, char** /*argv*/ )

imshow( "Rectangle, triangle & circle", img );

char key = (char)waitKey();
char key = waitChar();
if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC'
break;
}
Expand Down
Loading