Skip to content

Instantly share code, notes, and snippets.

@priteshgohil
Last active July 18, 2024 07:20
Show Gist options
  • Save priteshgohil/edce691cf557e7e3bb708ff100a18da3 to your computer and use it in GitHub Desktop.
Save priteshgohil/edce691cf557e7e3bb708ff100a18da3 to your computer and use it in GitHub Desktop.
C++ code to read images from webcam using CV 4.1.1
#include <opencv2/opencv.hpp>
#include <iostream>
int main(int, char**) {
// open the first webcam plugged in the computer
cv::VideoCapture camera(0); // in linux check $ ls /dev/video0
if (!camera.isOpened()) {
std::cerr << "ERROR: Could not open camera" << std::endl;
return 1;
}
// create a window to display the images from the webcam
cv::namedWindow("Webcam", cv::WINDOW_AUTOSIZE);
// array to hold image
cv::Mat frame;
// display the frame until you press a key
while (1) {
// capture the next frame from the webcam
camera >> frame;
// show the image on the window
cv::imshow("Webcam", frame);
// wait (10ms) for esc key to be pressed to stop
if (cv::waitKey(10) == 27)
break;
}
return 0;
}
// CMD to generate executable:
// g++ webcam_opencv.cpp -o webcam_demo -I/usr/include/opencv4 -lopencv_core -lopencv_videoio -lopencv_highgui
// Note: check your opencv hpp files - for many users it is at /usr/local/include/opencv4
// Add more packages during compilation from the list obtained by $ pkg-config --cflags --libs opencv4
@CHARLES-NKWABI
Copy link

Yeah it's fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment