--- title: OpenCV introduction tags: OpenCV # type: slide --- # OpenCV introduction - Reference - https://opencv.org/releases/ - https://docs.opencv.org/4.6.0/ - Environment Setup https://hackmd.io/4TXk4tknSMmlW77bZCzZtA#Additional-Environment-Setup-If-Required --- ## Example 1: Display an Image ### Python Code ```python= import cv2 as cv import sys img = cv.imread(cv.samples.findFile("starry_night.jpg")) if img is None: sys.exit("Could not read the image.") cv.imshow("Display window", img) k = cv.waitKey(0) if k == ord("s"): cv.imwrite("starry_night.png", img) ``` ### With C++ 1. Sample Code ```cpp= #include <opencv2/core.hpp> #include <opencv2/imgcodecs.hpp> #include <opencv2/highgui.hpp> #include <iostream> using namespace cv; int main() { std::string image_path = samples::findFile("starry_night.jpg"); Mat img = imread(image_path, IMREAD_COLOR); if(img.empty()) { std::cout << "Could not read the image: " << image_path << std::endl; return 1; } imshow("Display window", img); int k = waitKey(0); // Wait for a keystroke in the window if(k == 's') { imwrite("starry_night.png", img); } return 0; } ``` 2. Create a CMake file, known as ==CMakeLists.txt== https://docs.opencv.org/4.6.0/db/df5/tutorial_linux_gcc_cmake.html >cmake_minimum_required(VERSION 2.8) >project(DisplayImage) >find_package(OpenCV REQUIRED) >include_directories(${OpenCV_INCLUDE_DIRS}) >add_executable(DisplayImage DisplayImage.cpp) >target_link_libraries(DisplayImage ${OpenCV_LIBS}) 3. Build the executable file https://docs.opencv.org/4.6.0/db/df5/tutorial_linux_gcc_cmake.html >cd <DisplayImage_directory> >cmake . >make ### Results - Display the image ![](https://i.imgur.com/7HzF36V.jpg =500x) - Key 's' to save it ![](https://i.imgur.com/oJejiKp.png =500x) --- ## Example 2: Capture a Video ### Python Code ```python= import numpy as np import cv2 as cv cap = cv.VideoCapture(0) if not cap.isOpened(): print("Cannot open camera") exit() while True: # Capture frame-by-frame ret, frame = cap.read() # if frame is read correctly ret is True if not ret: print("Can't receive frame (stream end?). Exiting ...") break # Our operations on the frame come here gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) # Display the original frame cv.imshow('color frame', frame) # Display the resulting frame cv.imshow('gray frame', gray) if cv.waitKey(1) == ord('q'): break # When everything done, release the capture cap.release() cv.destroyAllWindows() ``` ### With C++ 1. Sample Code Modified from https://docs.opencv.org/4.x/d8/dfe/classcv_1_1VideoCapture.html ```cpp= #include <iostream> // for standard I/O #include <opencv2/core.hpp> // Basic OpenCV structures (cv::Mat) #include <opencv2/videoio.hpp> // Video I/O #include <opencv2/highgui.hpp> // for gui applications, like imshow() #include <opencv2/gapi/core.hpp> // for image processing using namespace std; using namespace cv; int main() { VideoCapture cap_vid; int cam_id = 0; // Camera ID cap_vid.open(cam_id); // Open video if (!cap_vid.isOpened()) { cout << "Could not open the camera, id: " << cam_id << endl; return -1; } Mat frame; Mat gray; do { // Capture frames bool ret = cap_vid.read(frame); if (!ret) { cout << "Can't receive frame (stream end?). Exiting ...\n"; break; } // Color to gray cvtColor(frame, gray, COLOR_BGR2GRAY); // Display live frames imshow("Live", frame); // Color // imshow("live", gray); // Gray } while (waitKey(30) != 'q'); return 0; } ``` 2. Create a CMake file, known as ==CMakeLists.txt== ```cmake cmake_minimum_required(VERSION 2.8) project(CaptureVideo) find_package(OpenCV REQUIRED) include_directories(${OpenCV_INCLUDE_DIRS}) add_executable(CaptureVideo CaptureVideo.cpp) target_link_libraries(CaptureVideo ${OpenCV_LIBS}) ``` 3. Switch to shell and build the executable file ```shell $ cd <CaptureVideo_directory> && mkdir build $ cd build # Collect all of the files generated by cmake. $ cmake ../ $ make ``` ---