API
- Here, use the #include instruction to include OpenCV HPP header file, in fact, it contains all opencv header files. By including a single file, you no longer need to include other files
- All OpenCV classes and functions are in the cv namespace, so you need to use the cv:: prefix or using namespace cv to compile statements in the source code
#include <opencv2/opencv.hpp> #include <iostream> using namespace std; using namespace cv;
read
- Use the function imread() to load an image from a file. The syntax is as follows:
imread(filename, flags)
- It requires two parameters
-
The first parameter is the image name, which requires the absolute path name of the file
-
The second parameter is an optional flag that specifies how the image is represented. OpenCV provides several options for this flag, the most common of which are:
- IMREAD_UNCHANGED or - 1
- IMREAD_GRAYSCALE or 0
- IMREAD_COLOR or 1
The default value of flags is 1, which will read the image as a color image. When you want to read an image in a specific format, you only need to specify the appropriate flag
Note that opencv reads color images in BGR format, while most other computer vision libraries use RGB channel format order. Therefore, when using OpenCV with other toolkits, don't forget to swap the blue and red channels when switching from one library to another
Mat img_color = imread("/home/kslas/OpenCV/test.jpg", 1); Mat img_grayscale = imread("/home/kslas/OpenCV/test.jpg", 0); Mat img_unchanged = imread("/home/kslas/OpenCV/test.jpg", -1);
display
- Use the function imshow(), and the syntax is as follows:
imshow(window_name, image)
- It also has two parameters
- The first parameter is the name of the window to be displayed on the window
- The second is the image to display
To display multiple images at once, you need to specify a new window name for each image
-
The function imshow() is designed to work with the functions waitKey() and destroyAllWindows()/destroyWindow()
-
The function waitKey() is a keyboard binding function
- It requires a parameter, the time (in milliseconds) that the window is displayed
- If the user presses any key during this time period, the program will continue
- If 0 is passed, it will wait for the keyboard to press
- It can also be set to detect specific keystrokes, for example, press key a, etc
-
The destroyAllWindows() function destroys all the windows we create. If you want to destroy a specific window, you should use the function destroyWindow() and give the exact window name
namedWindow("color image", WINDOW_AUTOSIZE); namedWindow("grayscale image", WINDOW_AUTOSIZE); namedWindow("unchanged image", WINDOW_AUTOSIZE); imshow("color image", img_color); imshow("grayscale image", img_grayscale); imshow("unchanged image", img_unchanged); waitKey(0); destroyAllWindows();
preservation
- Use the function imwrite() to write / save the image to the file directory. The syntax is as follows:
imwrite(filename, image)
- The first parameter is the image absolute pathname, which must contain the file extension (for example,. png,. jpg, etc.). OpenCV uses this file extension to specify the file format
- The second parameter is the image to save
imwrite("grayscale.jpg", img_grayscale);
CMakeLists.txt
cmake_minimum_required(VERSION 3.10) project(test1) set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED TRUE) find_package(OpenCV REQUIRED) include_directories(${OpenCV_INCLUDE_DIRS}) add_executable(test1 main.cpp) target_link_libraries(test1 ${OpenCV_LIBS})
Complete code
// Include Libraries #include <opencv2/opencv.hpp> #include <iostream> // Namespace nullifies the use of cv::function() using namespace std; using namespace cv; int main() { // Read an image Mat img_color = imread("/home/kslas/OpenCV/test.jpg", 1); Mat img_grayscale = imread("/home/kslas/OpenCV/test.jpg", 0); Mat img_unchanged = imread("/home/kslas/OpenCV/test.jpg", -1); // Create a window. namedWindow("color image", WINDOW_AUTOSIZE); namedWindow("grayscale image", WINDOW_AUTOSIZE); namedWindow("unchanged image", WINDOW_AUTOSIZE); // Show the image inside it imshow("color image", img_color); imshow("grayscale image", img_grayscale); imshow("unchanged image", img_unchanged); // Wait for a keystore waitKey(0); // Destroys all the windows created destroyAllWindows(); // Write the image in the same directory imwrite("grayscale.jpg", img_grayscale); return 0; }