OpenCV image loading and display

OpenCV image loading and display

load the image (using cv::imread)

Create a window named OpenCV (use cv::namedWindow)

Display image in OpenCV window (using cv::imshow)**

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
    
    
    String imageName( "../data/HappyFish.jpg" ); // by default
    if( argc > 1)
    {
    
    
        imageName = argv[1];
    }
    Mat image;
    image = imread( imageName, IMREAD_COLOR ); // Read the file
    if( image.empty() )                      // Check for invalid input
    {
    
    
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }
    namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.
    imshow( "Display window", image );                // Show our image inside it.
    waitKey(0); // Wait for a keystroke in the window
    return 0;
}

illustrate

In OpenCV 2 we have multiple modules. Each is responsible for handling a different area or method. You can observe this in the structure of the user guides in these tutorials themselves. Before you can use any of these, you first need to include the header files that declare the contents of each individual module.

You almost always end up using:


The core part, the highgui module, which is the basic building block of the library, is defined here , as it contains functions for input and output operations

#include < opencv2 / core.hpp >
#include < opencv2 / imgcodecs.hpp >
#include < opencv2 / highgui.hpp >
#include <iostream>
#include <string>

We also include iostreams to facilitate console line output and input. In order to avoid data structure and function name conflicts with other libraries, OpenCV has its own namespace: cv. To avoid the need to append before each of these cv:: keywords, you can import the namespace throughout the file with the following line:

using namespace cv;

The same is true for the STL library (for console I/O). Now, let's analyze the main functions. We start by making sure we get valid image name arguments from the command line. Otherwise, take a picture by default: "HappyFish.jpg".

   String imageName( "../data/HappyFish.jpg" ); // by default
    if( argc > 1)
    {
    
    
        imageName = argv[1];
    }

Then create a Mat object which will store the data of the loaded image.

Mat image;

Now we call the cv::imread function that loads the image name specified by the first argument (argv[1]). The second parameter specifies the format of the image we want. this might be:

IMREAD_UNCHANGED(<0)按原样加载图像(包括alpha通道(如果存在)
IMREAD_GRAYSCALE(0)将图像作为强度加载
IMREAD_COLOR(> 0)以RGB格式加载图像
    image = imread( imageName, IMREAD_COLOR ); // Read the file

Precautions

OpenCV supports image formats Windows bitmap (bmp), portable image formats (pbm, pgm, ppm) and Sun raster (sr, ras). With the help of plugins (you need to specify to use them if you build your own library, but in the package we ship, by default) you can also load images like JPEG (jpeg, jpg, jpe), JPEG 2000 (jp2 - Jasper, codenamed CMake), TIFF files (tiff, tif) and Portable Network Graphics (png). Also, OpenEXR is also a possibility.
After checking that the image data is loaded correctly, we want to display the image, so we create an OpenCV window using the cv::namedWindow function. They are automatically managed after OpenCV is created. To do this, you need to specify its name and how to handle changing the size of the image it contains. may be:

WINDOW_AUTOSIZE is only supported if the Qt backend is not used. In this case the window size will occupy the size of the displayed image. Resizing not allowed!
WINDOW_NORMAL In Qt you can use this to allow window resizing. The image will resize itself according to the current window size. By using the | operator you also need to specify whether you want the image to keep its aspect ratio (WINDOW_KEEPRATIO) (WINDOW_FREERATIO).

namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.

at last

To update the content of the OpenCV window with a new image, use the cv::imshow function. Specify the name of the OpenCV window to update and the image to use during this operation:

imshow( "Display window", image );                // Show our image inside it.

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

waitKey(0); // Wait for a keystroke in the window

result

Compile the code, then run the executable with the image path as an argument. If you're on Windows, the executable will of course also contain an exe extension. Of course make sure the image file is near your program files.

./DisplayImage HappyFish.jpg

You should get a nice window like this:
OpenCV image loading and display
insert image description here

Guess you like

Origin blog.csdn.net/qq_41704415/article/details/123796034