Last active
March 5, 2025 01:22
-
-
Save priteshgohil/edce691cf557e7e3bb708ff100a18da3 to your computer and use it in GitHub Desktop.
C++ code to read images from webcam using CV 4.1.1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yeah it's fine