Location>code7788 >text

Getting Started with OpenCV in C++ 002: Loading, Modifying, and Saving Images

Popularity:124 ℃/2024-10-29 10:48:44

I. Introduction
Today is the second post in this series, "Getting Started with Opencv in C++". Today's article is very simple, just a simple introduction to how to use Opencv to load images, display images, modify images and save images, to give you a most intuitive feeling. However, you can't think it's simple, it just makes the learning process a little less smooth, and the road ahead will be better. I'm not going to talk about the specific content of OpenCV, there are a lot of them on the Internet, so you can make up your own mind.
The official website address for OpenCV:/The component download address:/releases/
OpenCV official learning site:/4.10.0/

I need to make clarifications in case it is not clear to you, and I have listed the specifics.
Operating system: Windows Professional 10 (64-bit)
Development kit: OpenCV - 4.10.0
Development Tools: Microsoft Visual Studio Community 2022 (64-bit) - Current Version 17.8.3
Development language: C++ (VC16)

II. Knowledge presentation
As today's content is very simple, not too much to write the text, directly on the code, and in the code are detailed comments, start today's journey.
There are images in the source code that require them, replace the address with your own.

 1 #include <opencv2/>
 2 #include <iostream>
 3 
 4 using namespace cv;
 5 using namespace std;
 6 
 7 /// <summary>
 8 /// image processing
 9 /// 1, load image imread( const String& filename, int flags = IMREAD_COLOR )
10 /// 2、Create a window void namedWindow(const String& winname, int flags = WINDOW_AUTOSIZE)
11 /// 3、Show image void imshow(const String& winname, InputArray mat)
12 /// 4、Modify image void cvtColor( InputArray src, OutputArray dst, int code, int dstCn = 0 )
13 /// 5、Save Image imwrite( const String& filename, InputArray img,const std::vector<int>& params = std::vector<int>())
14 /// </summary>
15 /// <returns></returns>
16 int main()
17 {
18     //1. Load image
19     //Mat imread( const String& filename, int flags = IMREAD_COLOR ): the first parameter is the address of the image, the second parameter is the way to load the image.
20     //IMREAD_UNCHANGED(<0) means load the original image without any modification.
21     //IMREAD_GRAYSCALE(0) indicates that the original image is loaded in as a grayscale image.
22     //IMREAD_COLOR(>0) means load in the original image as an RGB image.
23     // opencv supports jpg, png, tiff and other common image formats.
24     Mat src = imread("D:\\360MoveData\\Users\\Administrator\\Desktop\\TestImage\\", IMREAD_UNCHANGED);
25 
26     //Load the original image as a grayscale image
27     Mat src2 = imread("D:\\360MoveData\\Users\\Administrator\\Desktop\\TestImage\\", IMREAD_GRAYSCALE);
28 
29     //2、Create window
30     //void namedWindow(const String& winname, int flags = WINDOW_AUTOSIZE): winname denotes the name of the window. flags denotes the window characteristics.
31     // namedWindow: indicates the creation of a window with the specified name and characteristics, which is used to display the image. It is created and destroyed automatically by OpenCV and does not need to be released manually.
32     //flags:list of window features
33     //WINDOW_AUTOSIZE: Adaptive image size, user can't modify the window size.
34     //WINDOW_FREERATIO: Window size adaptive scale
35     //WINDOW_FULLSCREEN: full screen display
36     //WINDOW_GUI_NORMAL: is the old way of drawing windows without status bar and toolbar, while WINDOW_GUI_EXPANDED is a new enhanced GUI.
37     //WINDOW_KEEPRATIO: Keeps the image proportional.
38     //WINDOW_NORMAL: used when integrating with QT, allows to modify the window size.
39     //
40 
41     namedWindow("OrigianImage", WINDOW_AUTOSIZE);
42     namedWindow("GrayImage", WINDOW_AUTOSIZE);
43 
44     //3. Display image
45     //void imshow(const String& winname, InputArray mat): winname The name of the window created using namedWindow; mat The image object to be displayed.
46     //imshow: a window created using namedWindow to display the specified image.
47 
48     imshow("OrigianImage", src);
49     imshow("GrayImage", src2);
50 
51     //4、Modify the image
52     //void cvtColor( InputArray src, OutputArray dst, int code, int dstCn = 0 ): src the source image, the image to be converted; dst the image after the color space conversion; code the conversion method of the source color space and the target color space; dstCn
53     //cvtColor: Converts an image from one color space to another.
54     //code list:
55     //COLOR_BGR2GRAY: Converts from RGB to Gray, from color to gray.
56     //
57 
58     Mat des;
59     cvtColor(src, des, COLOR_BGR2HSV);
60 
61     namedWindow("cvtColorImage", WINDOW_AUTOSIZE);
62     imshow("cvtColorImage", des);
63 
64 
65     //5. Saving images
66     //imwrite( const String& filename, InputArray img,const std::vector<int>& params = std::vector<int>()): filename The name of the file where the image is to be saved , contains the full path and file name and extension, the extension is different, the generated image is different; img The image to be saved.
67     //imwrite: used to save as an image with the specified name.
68     //Only images in 8-bit, 16-bit PNG, JPG, TIFF file formats and single-channel or three-channel BGR can be saved in this way.
69     //You can save the image of the transparent channel when saving the PNG format.
70     //Compression parameters can be specified.
71 
72     //Saves three formats, realized by suffix names.
73     imwrite("D:\\ScanImages\\",des);
74     imwrite("D:\\ScanImages\\", des);
75     imwrite("D:\\ScanImages\\", des);
76 
77     waitKey(0);
78 
79     return 0;
80 }

Original image code:

1 Mat src = imread("D:\\360MoveData\\Users\\Administrator\\Desktop\\TestImage\\", IMREAD_UNCHANGED);
2 namedWindow("OrigianImage", WINDOW_AUTOSIZE);

 

3 imshow("OrigianImage", src);

 

The effect is shown in the picture:

Code for grayscale images:

1 Mat src2 = imread("D:\\360MoveData\\Users\\Administrator\\Desktop\\TestImage\\", IMREAD_GRAYSCALE);
2 namedWindow("GrayImage", WINDOW_AUTOSIZE);
3 imshow("GrayImage", src2);

The grayscale image is shown in Fig:

Code for changing the color space:

1 Mat des;
2 cvtColor(src, des, COLOR_BGR2HSV);
3 
4 namedWindow("cvtColorImage", WINDOW_AUTOSIZE);
5 imshow("cvtColorImage", des);

The effect is shown in the picture:

Without going into much else, the content is simple.



III. Summary
This is the second post on using OpenCV with C++, and it's not really that hard, so if it feels like it's still a good place to get started, let's move on. The first results, continue to work hard. The sky is the limit, do not forget the original intention, continue to work hard, do what you like to do, happy.