waitkey in opencv

waitKey is a function in the OpenCV library for C++. It is used to display an image or video frame in a window and wait for a keyboard event.

Here are the steps involved in using waitKey:

  1. Include the necessary header files:
  2. #include <opencv2/opencv.hpp>: This includes the main OpenCV library.
  3. #include <opencv2/highgui.hpp>: This includes the highgui module, which provides functions for creating and manipulating windows.

  4. Create a window:

  5. cv::namedWindow("Window Name", cv::WINDOW_NORMAL): This creates a named window with the given name. The cv::WINDOW_NORMAL flag allows the window to be resized.

  6. Load an image or video frame:

  7. cv::Mat image = cv::imread("image.jpg"): This loads an image from a file named "image.jpg" and stores it in a cv::Mat object. Alternatively, you can capture a frame from a video using the cv::VideoCapture class.

  8. Display the image or video frame:

  9. cv::imshow("Window Name", image): This displays the image or video frame in the specified window.

  10. Wait for a keyboard event:

  11. int key = cv::waitKey(delay): This waits for a keyboard event for a specified delay (in milliseconds). The function returns the key code of the pressed key, or -1 if no key was pressed during the delay.

  12. Process the keyboard event:

  13. You can use conditional statements or switch-case statements to perform different actions based on the key code returned by waitKey.

  14. Close the window:

  15. cv::destroyWindow("Window Name"): This closes the specified window.

That's it! These are the steps involved in using waitKey in OpenCV with C++.