Try   HackMD

OpenCV introduction


Example 1: Display an Image

Python Code

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
#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; }
  1. 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})

  2. 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

  • Key 's' to save it


Example 2: Capture a Video

Python Code

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
#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; }
  1. Create a CMake file, known as CMakeLists.txt
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})
  1. Switch to shell and build the executable file
$ cd <CaptureVideo_directory> && mkdir build
$ cd build  # Collect all of the files generated by cmake.
$ cmake ../
$ make