OpenCV read, display, save image tutorial


foreword

The full name of OpenCV is Open Source Computer Vision Library, which is a cross-platform computer vision library. OpenCV is initiated and participated in the development by Intel Corporation. It is released under the BSD license and can be used free of charge in commercial and research fields. OpenCV can be used to develop real-time image processing, computer vision, and pattern recognition programs. The library can also use Intel's IPP for accelerated processing.


1. Basic concepts

In this tutorial you will learn how to:
read an image from a file (using cv::imread)
display an image in an OpenCV window (using cv::imshow)
write an image to a file (using cv::imwrite)

2. Operation steps

1. Include the header file

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;

In OpenCV 3 we have multiple modules. Each is responsible for a different area or approach to image processing. You can already observe this in the structure of the user guides in these tutorials themselves. Before using any of these, you first need to include the header files where the contents of each individual module are declared.

You will almost always end up using:

The core part, where the basic building block of the library is defined is
the imgcodecs module, which provides read and write functionality.
The highgui module, as it contains the functionality to display images in a window.
We also include iostreams to facilitate console line output and input.

By using namespace cv; declared below, library functions can be accessed without explicitly declaring a namespace.

2. Read the image

std::string image_path = samples::findFile("starry_night.jpg");
Mat img = imread(image_path, IMREAD_COLOR);
if(img.empty())//执行检查,图像加载正确。
{
    std::cout << "Could not read the image: " << image_path << std::endl;
    return 1;
}

We read the image "starry_night.jpg" from the OpenCV sample. To do this, call the cv::imread function to load the image with the file path specified by the first parameter. The second parameter is optional and specifies the image format we want. this might be:

IMREAD_COLOR loads an image in BGR 8-bit format. This is the default used here.
IMREAD_UNCHANGED loads the image as is (including alpha channel if present)
IMREAD_GRAYSCALE loads the image as an intensity image
After reading the image data will be stored in a cv::Mat object.

3. Display image

imshow("Display window", img);
int k = waitKey(0); // Wait for a keystroke in the window

Call the cv::imshow function to display the image. The first parameter is the title of the window, and the second parameter is the cv::Mat object to be displayed.

Since we want our window to be shown before the user presses a key (otherwise the program would end too quickly), we use the cv::waitKey function whose only parameter is the time to wait for user input (in milliseconds). Zero means wait forever. The return value is the key pressed.

4. Save the image

if(k == 's')
{
    imwrite("starry_night.png", img);
}

Finally, if the key pressed was the "s" key, the image will be written to the file. For this, the cv::imwrite function is called, which has the file path and the cv::Mat object as parameters.


Summarize

The above is the reading, displaying and saving image tutorial content of OpenCV.

Guess you like

Origin blog.csdn.net/szylight2022/article/details/128551057