Preface
On the gorgeous stage of computer vision, OpenCV is undoubtedly a dazzling star, which gives developers the super ability to manipulate images and mine visual information. Before formally diving into those amazing code examples, let’s first consolidate the theoretical foundation and lay a solid foundation for subsequent practical practice.
1. Overview of OpenCV basics and image processing
OpenCV (Open Source Computer Vision Library), an open source computer vision library, is like a treasure chest full of image processing "artifacts". After years of iteration and polishing by countless developers, it spans multiple platforms, writes interfaces in popular languages such as C++ and Python, adapts to different programming habits, and takes on 80% of the basic tasks of computer vision, from simple image reading and storage to complex object detection and image recognition, it is no problem.
Image processing is essentially a careful crafting of image pixels. The images are stored in the computer, like neatly arranged "pixel square arrays", each pixel contains color and brightness information. When we do image processing, we are cleverly changing these pixel attributes. To adjust the brightness of the image is to add the brightness value of the pixel; to modify the color is to mix the red, green and blue components. Different means and consistent purpose: make the image meet specific needs, be beautiful, or accurate.
2. Analysis of image transformation theory
(I) Remapping (Remapping)
Remapping can be called the "Great Movement of the Universe" of image coordinates. Normal image pixels are positioned according to row and column rules, remapping breaks the rules, and "move" the pixels according to custom mapping rules. The core principle is to create two new "coordinate maps" (src_x, src_y) with the same size as the original image, record the new position, like a tour guide map, guiding the direction of pixels "migration". This operation is practical and shows off your skills when creating waves and distorting special effects, making the picture instantly flexible.
(II) Scaling
Scaling is to "zoom in and out" the image. Mathematically, the number of pixels in row and column is changed according to the scale factor (scale_x, scale_y), and the merge of the near pixels becomes smaller, and the interval interpolation becomes “fat” and becomes larger. Interpolation algorithm is the key, and common linear interpolation (cv::INTER_LINEAR), like experienced tailors, calculate the "appropriate face" of new pixels based on the surrounding pixel information to ensure smooth transition of the image.
(Three) Rotation (Rotation)
Rotate makes the image elegant “turn around”. First lock the rotation center, mostly the image geometry center, and then use the rotation matrix (generated by cv::getRotationMatrix2D) as the "rotation command". This matrix contains rotation angle and scaling information. Combined with affine transformation (cv::warpAffine), the pixels are in place according to the instructions, and the picture rotates smoothly without any contradictory "moving sickness".
3. Image enhancement and feature extraction theory
(I) Color Conversion
There are many color spaces like stars, and RGB, HSV, grayscale, etc. RGB emphasizes color presentation, HSV facilitates color adjustment, and grayscale focuses on brightness information, abandoning color redundancy. For example, if you turn to grayscale, use cv::cvtColor to "fade" with one click, and follow the brightness perception rules of the human eye, weight and fuse RGB values to extract key brightness, which is conducive to subsequent processing, and can also reduce the amount of data and increase the processing speed.
(2) Edge Detection
The edge is the "skeleton" of the image, hiding the key contour information. The edge detection algorithm is like a keen detective, keeping a close eye on the pixel grayscale mutation. The Canny algorithm is a trump card, set low and high double thresholds, and the gradient exceeds the "high line" to lock the edge; between the "low line" and the "high line", check the connection situation; lower than the "low line", pass directly. After non-maximum suppression and double-threshold screening, the edges are accurately identified and paved the way for target recognition and image segmentation.
(III) Gaussian Blur (Gaussian Blur)
Realistic images often have noise, such as scratches and digital noise in old photos. Gaussian blur is an incarnation of "beauty and skin polisher", using Gaussian function to generate a weight kernel, with high weight of the center pixel and decreasing peripheral pixels, weighted average peripheral pixel values, blur images and soften noise, making the picture delicate and clean, and clearing out obstacles for subsequent precise processing.
(IV) Image Sharpening
Sharpening is blurring "nemesis" that retrieves lost details of the image. The principle is to strengthen the high-frequency component and weaken the low-frequency. Relying on the special convolution kernel (such as the 3x3 kernel in the article), the central pixel is "protruded", the peripherals are adjusted in reverse, and the contrast highlights the edge details. The clarity soars after processing photos and scanned documents.
(V) Color Inversion
Color reversal can be called the "mirror world" of images, simple and rough bitwise inverting pixel values. White turns black, black turns white, each color is flipped, and when specific artistic creations and negative effects are simulated, you can easily grasp the atmosphere.
III. Example analysis
1. Define the constant M_PI
#ifndef M_PI #define M_PI 3.14159265358979323846 // If M_PI is not defined, manually define it#endif
The purpose of this code is to ensure that during the compilation process, ifM_PI
The constant is not defined, then it is manually defined as the value of pi. This is necessary in some cases, as different compilers and environments may define certain constants differently.
2. Remap the image to create waveform effects
void wave(const cv::Mat& img, cv::Mat& res, const std::string& wave_type, double amplitude, double frequency) { cv::Mat src_x(, , CV_32F); cv::Mat src_y(, , CV_32F); for (int i = 0; i < ; i++) { for (int j = 0; j < ; j++) { src_x.at<float>(i, j) = static_cast<float>(j); double wave_value = 0.0; if (wave_type == "sine") { wave_value = amplitude * sin(frequency * static_cast<double>(j)); // Sine wave } else if (wave_type == "cosine") { wave_value = amplitude * cos(frequency * static_cast<double>(j)); // Cosine wave } else if (wave_type == "triangle") { wave_value = amplitude * (2.0 / M_PI) * asin(sin(frequency * static_cast<double>(j))); // Triangular wave } else if (wave_type == "sawtooth") { wave_value = amplitude * (2.0 * (j / static_cast<double>()) - 1.0); // Sawtooth wave } src_y.at<float>(i, j) = static_cast<float>(i + wave_value); } } cv::remap(img, res, src_x, src_y, cv::INTER_LINEAR); }
This function is used to create different types of waveform effects, which are implemented by remapping the pixel coordinates of the image. It accepts input imagesimg
, output imageres
, waveform typewave_type
, amplitudeamplitude
and frequencyfrequency
as a parameter.
First, create two matrices with the same size as the input image.src_x
andsrc_y
, used to store the x and y coordinates after remapping. Then, each pixel of the input image is traversed through a double-layer loop. For each pixel,src_x
The corresponding value in is set to the x coordinate of the current pixel and according towave_type
The value of the waveform is calculated by differentwave_value
. Finally,src_y
The corresponding value in is set to the y coordinate of the current pixel plus the waveform value.
Finally, usecv::remap
Functions basedsrc_x
andsrc_y
The matrix remaps the input image to obtain the output imageres
。
3. Zoom the image
void scale(const cv::Mat& img, cv::Mat& res, double scale_x, double scale_y) { cv::resize(img, res, cv::Size(), scale_x, scale_y, cv::INTER_LINEAR); }
This function is used to scale images. It accepts input imagesimg
, output imageres
, horizontal scaling ratioscale_x
and vertical scalingscale_y
as a parameter.
usecv::resize
The function scales the input image and sets the size of the output image to the size of the input image multiplied by the scaling ratio. Used herecv::INTER_LINEAR
Interpolation method to obtain a smoother scaling effect.
4. Rotate the image
void rotate1(const cv::Mat& img, cv::Mat& res, double angle) { cv::Point2f center( / 2.0, / 2.0); cv::Mat rot = cv::getRotationMatrix2D(center, angle, 1.0); cv::warpAffine(img, res, rot, ()); }
This function is used to rotate the image. It accepts input imagesimg
, output imageres
and rotation angleangle
as a parameter.
First, calculate the center coordinates of the image. Then, usecv::getRotationMatrix2D
Function creates a rotation matrixrot
, This matrix rotates the image around the center at a specified angle. Finally, usecv::warpAffine
The function performs affine transformation of the input image based on the rotation matrix to obtain the output imageres
。
5. Color transformation (grayscale)
void grayscale(const cv::Mat& img, cv::Mat& res) { cv::cvtColor(img, res, cv::COLOR_BGR2GRAY); }
This function is used to convert color images to grayscale images. It accepts input imagesimg
and output imagesres
as a parameter.
usecv::cvtColor
The function converts the input image from the BGR color space to the grayscale color space to obtain the output imageres
。
6. Edge detection
void edge_detection(const cv::Mat& img, cv::Mat& res) { cv::Canny(img, res, 100, 200); }
This function is used for edge detection. It accepts input imagesimg
and output imagesres
as a parameter.
usecv::Canny
The function performs edge detection on the input image, which accepts the input image, the output image, the low threshold and the high threshold as parameters. The edge detection algorithm determines whether it is an edge pixel based on the gradient intensity of the pixel. If the gradient intensity of the pixel is greater than the high threshold, it is considered an edge pixel; if the gradient intensity of the pixel is less than the low threshold, it is considered not an edge pixel; if the gradient intensity of the pixel is between the low threshold and the high threshold, it determines whether it is an edge pixel based on its connection with the edge pixel.
7. Gaussian Fuzzy
void gaussian_blur(const cv::Mat& img, cv::Mat& res) { cv::GaussianBlur(img, res, cv::Size(5, 5), 0); }
This function is used to perform Gaussian blurring on the image. It accepts input imagesimg
and output imagesres
as a parameter.
usecv::GaussianBlur
The function performs Gaussian blurring of the input image, which accepts the input image, the output image, the blur kernel size and the standard deviation as parameters. The fuzzy kernel size used here iscv::Size(5, 5)
, the standard deviation is 0, indicating that the standard deviation is automatically calculated by the function.
8. Image Sharpening
void sharpen(const cv::Mat& img, cv::Mat& res) { cv::Mat kernel = (cv::Mat_<float>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0); cv::filter2D(img, res, (), kernel); }
This function is used to sharpen the image. It accepts input imagesimg
and output imagesres
as a parameter.
First, define a 3x3 sharpened kernelkernel
. Then, usecv::filter2D
The function performs convolution operations on the input image, which accepts the input image, the output image, the image depth and the convolution kernel as parameters. Here the input image is convolution with the sharpened kernel to obtain the output imageres
, thereby achieving the effect of image sharpening.
9. Color reversal
void invert_colors(const cv::Mat& img, cv::Mat& res) { cv::bitwise_not(img, res); }
This function is used to invert the image color. It accepts input imagesimg
and output imagesres
as a parameter.
usecv::bitwise_not
The function performs bit-inverting operation on the input image, that is, inverting the color value of each pixel to obtain the output image.res
。
IV. Main function implementation
int main() { cv::Mat img = cv::imread("E:/pro/sdl_code/res/test_img.png"); if (()) { std::cerr << "Error: Image not found." << std::endl; return -1; } cv::Mat sine_wave_img, cosine_wave_img, triangle_wave_img, sawtooth_wave_img, scale_img, rotate_img, gray_img, edge_img, blur_img, sharp_img, invert_img; // Different waveform effects wave(img, sine_wave_img, "sine", 20, 0.1); // Sine wave wave(img, cosine_wave_img, "cosine", 20, 0.1); // Cosine wave wave(img, triangle_wave_img, "triangle", 20, 0.1); // Triangular wave wave(img, sawtooth_wave_img, "sawtooth", 20, 10); // Sawtooth wave // Zoom the image scale(img, scale_img, 0.5, 0.5); // Rotate the image rotate1(img, rotate_img, 45); // Color transformation (grayscale image) grayscale(img, gray_img); // Edge detection edge_detection(img, edge_img); // Gaussian blur gaussian_blur(img, blur_img); // Image Sharpening sharpen(img, sharp_img); // Color reversal invert_colors(img, invert_img); // Show all effects cv::imshow("Original Image", img); cv::imshow("Sine Wave Effect", sine_wave_img); cv::imshow("Cosine Wave Effect", cosine_wave_img); cv::imshow("Triangle Wave Effect", triangle_wave_img); cv::imshow("Sawtooth Wave Effect", sawtooth_wave_img); cv::imshow("Scaled Image", scale_img); cv::imshow("Rotated Image", rotate_img); cv::imshow("Grayscale Image", gray_img); cv::imshow("Edge Detection", edge_img); cv::imshow("Gaussian Blur", blur_img); cv::imshow("Sharpened Image", sharp_img); cv::imshow("Inverted Colors", invert_img); cv::waitKey(0); // Save the result image //cv::imwrite("output_wave_image.jpg", wave_img); //cv::imwrite("output_scaled_image.jpg", scale_img); //cv::imwrite("output_rotated_image.jpg", rotate_img); //cv::imwrite("output_grayscale_image.jpg", gray_img); //cv::imwrite("output_edge_detection.jpg", edge_img); //cv::imwrite("output_gaussian_blur.jpg", blur_img); //cv::imwrite("output_sharpened_image.jpg", sharp_img); //cv::imwrite("output_inverted_colors.jpg", invert_img); return 0; }
The main function first reads the input image, and if the image is read, the error message is output and returns -1. Then, multiple output images are created to store different processing effects. Next, different functions are called to process the input image to obtain the output image with various effects. Finally, all images are displayed and wait for the user to press any key to exit the program. If you need to save the result image, you can uncomment the corresponding line of code.
5. Summary
This article describes how to use OpenCV to achieve a variety of image transformation and processing effects, including remapping images to create waveform effects, zoom images, rotate images, color transformation, edge detection, Gaussian blur, image sharpening and color inversion, etc. Through these functions, we can perform various operations on the image to achieve different visual effects. In practical applications, appropriate image transformation and processing methods can be selected according to specific needs to achieve better results.
I hope this article will be helpful to everyone to learn and use OpenCV. If you have any questions or suggestions, please leave a message in the comment area. First, we introduce the importance of OpenCV and what to show in this article. Next, the environment preparation instructions include installing OpenCV and C++ compilers. Then the key code was analyzed in detail and the definition constants were explained separately.M_PI
, the functions and implementation principles of various image transformation and processing functions. Finally, the implementation of the main function is shown, including reading images, creating output images, calling various processing functions, displaying images and waiting for user operations, etc. The overall article is structured clearly and written in the order of introduction, preparation, analysis, implementation and summary to help readers better understand OpenCV's image transformation and processing functions.
This is the article about OpenCV image transformation and processing practical methods. For more related OpenCV image transformation and processing content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!