This blog post covers the basics of OpenCV, a popular computer vision library. It explains how to load, display, and save images using OpenCV functions. Additionally, it delves into the Mat class, which is used to represent images in OpenCV as matrices. The post demonstrates how to create Mat objects, access their properties, and traverse and manipulate pixel values using both array-based and pointer-based methods.
Hello World
| |
- In OpenCV 4, you can directly use
#include <opencv2/opencv.hpp>to import all OpenCV functions. using namespace cv;uses the OpenCV namespace.- Define a
Matvariablesrcand read an image into it. - Perform robustness check; if the loaded image is not empty, display it.
- Use
namedWindow("input", WINDOW_AUTOSIZE);to create a window. The first parameter is the window name, and the second parameter is the window size, which in OpenCV 4 is defined as automatic window size. In OpenCV 3, usenameWindow("input", CV_WINDOW_AUTOSIZE)to define the window. - Use
imshow("input", src)to display the image. The first parameter is the window to display in, and the second parameter is the image to display. - To prevent the displayed image from disappearing immediately, use
waitKey(0)to wait for user input. - Use
destroyAllWindows()to destroy all windows.
Image File Loading, Displaying, and Saving
| |
Image File Loading
The imread("filename", load_mode) function:
- By default, loads a BGR color image.
- Supports grayscale and any color format images.
Common load modes include:
IMREAD_COLOR: Load as a color image, the default mode, transparency channel will be discarded.IMREAD_GRAYSCALE: Load as a grayscale image.IMREAD_ANYCOLOR: Load with any color format.IMREAD_UNCHANGED: Do not change the number of channels, supports loading PNG transparency channel.
Window and Display
The imshow("window_name", display_mode) function:
Display modes include:
WINDOW_AUTOSIZE: Automatically fit the image size, cannot be changed.WINDOW_FREERATIO: Freely change the image display size.WINDOW_NORMAL: Can change the image display size.
Image File Saving
The imwrite("save_address", variable) function:
Saves the image in the corresponding format based on the file extension.
Mat Class
| |
An image is essentially a matrix, so in OpenCV, images are stored in matrix form, i.e., Mat. Common attributes of Mat include:
src.cols;// Get the image width.src.rows;// Get the image height.src.channels();// Get the number of image channels.src.depth();// Image depth, enum type, returns the depth of each pixel element. Common values includeCV_8U(8-bit unsigned integer),CV_16U(16-bit unsigned integer),CV_32F(32-bit float), etc.src.type();// Image type, enum type. Common types includeCV_8U(8-bit unsigned integer),CV_8S(8-bit signed integer),CV_16U(16-bit unsigned integer),CV_16S(16-bit signed integer),CV_32F(32-bit float),CV_64F(64-bit float), etc.src.size// Returns a tuple containing the number of rows and columns of the image.src.empty(): Check if the Mat object is empty.src.total(): Returns the total number of elements in the Mat object.
Creating and Using Mat Objects
| |
Creating Mat Objects
- Use
Mat t1 = Mat(256, 256, CV_8UC3);to create a 256x256 grayscale image. The type represents an unsigned 3-channel image. t1 = Scalar(0, 0, 256);assigns constant values to the image matrix.Mat t2 = Mat(Size(512, 512), CV_8UC3);creates a 512x512 grayscale image.Mat t3 = Mat::zeros(Size(256, 256), CV_8SC3);directly creates a 256x256 all-black image with an unsigned 3-channel type during creation.Mat t4 = src;createst4directly fromsrc.Mat t5 = src.clone();creates a clone ofsrc.Mat t6; src.copyTo(t6);copiessrctot6.Mat t7 = Mat::zeros(src.size(), src.type());creates an all-zero image with the same size and type assrc.
Traversing and Accessing Pixel Values
| |
This code demonstrates how to traverse and access pixel values in OpenCV using two different methods: array-based and pointer-based. The array-based method uses src.at<Vec3b>(row, col) to access the pixel values directly, while the pointer-based method uses src.ptr<uchar>(row) to get a pointer to the current row and then traverses the row using pointer arithmetic.
Overall, this blog post serves as a comprehensive introduction to the fundamental concepts and techniques in OpenCV, making it a valuable resource for anyone interested in computer vision and image processing.
