OpenCV Basics
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
1 |
|
- 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
Mat
variablesrc
and 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
1 |
|
Image File Loading
The imread("filename", load_mode)
function: 1. By
default, loads a BGR color image. 2. Supports grayscale and any color
format images.
Common load modes include: 1. IMREAD_COLOR
: Load as a
color image, the default mode, transparency channel will be discarded.
2. IMREAD_GRAYSCALE
: Load as a grayscale image. 3.
IMREAD_ANYCOLOR
: Load with any color format. 4.
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: 1. WINDOW_AUTOSIZE
: Automatically
fit the image size, cannot be changed. 2. WINDOW_FREERATIO
:
Freely change the image display size. 3. 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
1 |
|
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
1 |
|
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;
createst4
directly fromsrc
.Mat t5 = src.clone();
creates a clone ofsrc
.Mat t6; src.copyTo(t6);
copiessrc
tot6
.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
OpenCV provides several methods to access and manipulate individual pixels in an image. Here's a concise overview of the different techniques:
1. Array-based Pixel Access
with at
1 |
|
- Use nested loops to iterate over every row and column of the image
- Access color pixels with
image.at<Vec3b>(y, x)
- Access grayscale pixels with
image.at<uchar>(y, x)
- Directly read and write pixel values, e.g.,
pixel[0] = 255 - blue
- Requires obtaining the image's height, width, and number of channels
2. Pointer-based Pixel Access
1 |
|
- Use nested loops to iterate over every row and column of the image
- Get a pointer to the start of each row with
image.ptr<uchar>(y)
- Access color pixels by incrementing the pointer by
x * 3
for each channel - Access grayscale pixels by incrementing the pointer by
x
3. Iterator-based Pixel Access
1 |
|
- Use iterators to traverse the image pixels
image.begin<Vec3b>()
returns an iterator to the first pixel in the imageimage.end<Vec3b>()
returns an iterator to the position after the last pixel- Directly access and modify pixel values using the iterator
dereferencing operator
*it
These techniques provide different ways to access and manipulate pixel data in OpenCV images. The choice depends on personal preference, code readability, and performance considerations for the specific use case.