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:
- Include the necessary header files:
#include <opencv2/opencv.hpp>
: This includes the main OpenCV library.#include <opencv2/highgui.hpp>
: This includes the highgui module, which provides functions for creating and manipulating windows.Create a window:
cv::namedWindow("Window Name", cv::WINDOW_NORMAL)
: This creates a named window with the given name. Thecv::WINDOW_NORMAL
flag allows the window to be resized.Load an image or video frame:
cv::Mat image = cv::imread("image.jpg")
: This loads an image from a file named "image.jpg" and stores it in acv::Mat
object. Alternatively, you can capture a frame from a video using thecv::VideoCapture
class.Display the image or video frame:
cv::imshow("Window Name", image)
: This displays the image or video frame in the specified window.Wait for a keyboard event:
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.Process the keyboard event:
You can use conditional statements or switch-case statements to perform different actions based on the key code returned by
waitKey
.Close the window:
cv::destroyWindow("Window Name")
: This closes the specified window.
That's it! These are the steps involved in using waitKey in OpenCV with C++.