Load, Modify, and Save an Image - 加载、修改和保存图像

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

  1. We begin by loading an image using cv::imread, located in the path given by imageName. For this example, assume you are loading a BGR image.
    对于此示例,假设您正在加载 BGR 图像。

  2. 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 of cv::imread has BGR default channel order in case of color images).
  1. 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 to cv::imread : cv::imwrite
    因此,现在我们有了新的 gray_image,并希望将其保存在磁盘上 (否则它将在程序结束后丢失)。
    imwrite( "../../images/Gray_Image.jpg", gray_image );
    Which will save our gray_image as Gray_Image.jpg in the folder images located two levels up of my current location.
    这会将我们的 gray_image 保存为 Gray_Image.jpg 在我当前位置上两层的文件夹图像中。

  2. 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 );

  3. 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!

发布了454 篇原创文章 · 获赞 1740 · 访问量 103万+

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/104463711