Load, Modify, and Save an Image - 加载、修改和保存图像
OpenCV 4.2.0 - Modules
https://docs.opencv.org/4.2.0/index.html
OpenCV 4.2.0 - Tutorials
https://docs.opencv.org/4.2.0/d9/df8/tutorial_root.html
0. Load, Modify, and Save an Image
https://docs.opencv.org/4.2.0/db/d64/tutorial_load_save_image.html
OpenCV modules -> OpenCV Tutorials -> Introduction to OpenCV -> Load, Modify, and Save an Image
1. Goals
Load an image using cv::imread
Transform an image from BGR to Grayscale format by using cv::cvtColor
Save your transformed image in a file on disk (using cv::imwrite
)
2. Code
//============================================================================
// Name : using namespace cv;
// Author : Yongqiang Cheng
// Version : Feb 22, 2020
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv)
{
char* imageName = argv[1];
Mat image;
image = imread(imageName, IMREAD_COLOR);
if (argc != 2 || !image.data)
{
printf(" No image data \n ");
return -1;
}
Mat gray_image;
cvtColor(image, gray_image, COLOR_BGR2GRAY);
imwrite("../images/Gray_Image.jpg", gray_image);
namedWindow(imageName, WINDOW_AUTOSIZE);
namedWindow("Gray image", WINDOW_AUTOSIZE);
imshow(imageName, image);
imshow("Gray image", gray_image);
waitKey(0);
return 0;
}
3. Explanation
-
We begin by loading an image using
cv::imread
, located in the path given byimageName
. For this example, assume you are loading a BGR image.
对于此示例,假设您正在加载 BGR 图像。 -
Now we are going to convert our image from BGR to Grayscale format. OpenCV has a really nice function to do this kind of transformations:
cvtColor( image, gray_image, COLOR_BGR2GRAY );
As you can see,cv::cvtColor
takes as arguments:
- a source image (
image
) - a destination image (
gray_image
), in which we will save the converted image. - an additional parameter that indicates what kind of transformation will be performed. In this case we use
COLOR_BGR2GRAY
(because ofcv::imread
has BGR default channel order in case of color images).
-
So now we have our new
gray_image
and want to save it on disk (otherwise it will get lost after the program ends). To save it, we will use a function analogous tocv::imread
:cv::imwrite
因此,现在我们有了新的gray_image
,并希望将其保存在磁盘上 (否则它将在程序结束后丢失)。
imwrite( "../../images/Gray_Image.jpg", gray_image );
Which will save ourgray_image
asGray_Image.jpg
in the folder images located two levels up of my current location.
这会将我们的gray_image
保存为Gray_Image.jpg
在我当前位置上两层的文件夹图像中。 -
Finally, let’s check out the images. We create two windows and use them to show the original image as well as the new one:
namedWindow( imageName, WINDOW_AUTOSIZE );
namedWindow( "Gray image", WINDOW_AUTOSIZE );
imshow( imageName, image );
imshow( "Gray image", gray_image );
-
Add the
waitKey(0)
function call for the program to wait forever for an user key press.
添加waitKey(0)
函数调用,使程序永远等待用户按下按键。
4. Result
When you run your program you should get something like this:
当您运行程序时,应该得到如下信息:
17:39:13 **** Incremental Build of configuration Debug for project DisplayImage ****
make all
Building file: ../src/DisplayImage.cpp
Invoking: GCC C++ Compiler
g++ -std=c++0x -I/usr/local/include -I/usr/local/include/opencv4 -I/usr/local/include/opencv4/opencv2 -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/DisplayImage.d" -MT"src/DisplayImage.o" -o "src/DisplayImage.o" "../src/DisplayImage.cpp"
Finished building: ../src/DisplayImage.cpp
Building target: DisplayImage
Invoking: GCC C++ Linker
g++ -L/usr/local/lib -o "DisplayImage" ./src/DisplayImage.o -lopencv_core -lopencv_video -lopencv_ml -lopencv_imgproc -lopencv_img_hash -lopencv_flann -lopencv_features2d -lopencv_calib3d -lopencv_dnn -lopencv_dnn_objdetect -lopencv_cvv -lopencv_text -lopencv_datasets -lopencv_aruco -lopencv_bgsegm -lopencv_shape -lopencv_imgcodecs -lopencv_videoio -lopencv_highgui -lopencv_bioinspired
Finished building target: DisplayImage
17:39:16 Build Finished (took 2s.982ms)
And if you check in your folder (images
), you should have a newly .jpg
file named Gray_Image.jpg
:
strong@foreverstrong:~/eclipse-work/DisplayImage/Debug$ ls -l ./DisplayImage
-rwxrwxr-x 1 strong strong 730488 Feb 23 17:39 ./DisplayImage
strong@foreverstrong:~/eclipse-work/DisplayImage/Debug$
strong@foreverstrong:~/eclipse-work/DisplayImage/Debug$ ./DisplayImage ../images/person.jpg
QXcbConnection: XCB error: 148 (Unknown), sequence: 172, resource id: 0, major code: 140 (Unknown), minor code: 20
strong@foreverstrong:~/eclipse-work/DisplayImage/Debug$
Congratulations, you are done with this tutorial!