[opencv4]图片读入,显示,存储

目录

1.完整demo地址

2.读取图片:

§ imread()

3.展示图片

§ namedWindow()

4.存储图片:

§ imwrite()


  • 完整demo:

    https://github.com/youhengchan/learn_opencv4/tree/master/01-open_show_modify_save_pics

  • 读取图片:

    String imageName( "../media/fish.jpg" ); // by default
    if( argc > 1)
    {
        imageName = argv[1];
    }
 
    Mat image = imread( imageName, IMREAD_COLOR ); // Read the file

使用imread读取图片,两个参数

Mat cv::imread ( const String &  filename,
    int  flags = IMREAD_COLOR 
  )  

需要使用头文件 #include

 其中imread flags有很多选项:

Enumerator
IMREAD_UNCHANGED 

Python: cv.IMREAD_UNCHANGED

If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).

IMREAD_GRAYSCALE 

Python: cv.IMREAD_GRAYSCALE

If set, always convert image to the single channel grayscale image (codec internal conversion).

IMREAD_COLOR 

Python: cv.IMREAD_COLOR

If set, always convert image to the 3 channel BGR color image.

IMREAD_ANYDEPTH 

Python: cv.IMREAD_ANYDEPTH

If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.

IMREAD_ANYCOLOR 

Python: cv.IMREAD_ANYCOLOR

If set, the image is read in any possible color format.

IMREAD_LOAD_GDAL 

Python: cv.IMREAD_LOAD_GDAL

If set, use the gdal driver for loading the image.

IMREAD_REDUCED_GRAYSCALE_2 

Python: cv.IMREAD_REDUCED_GRAYSCALE_2

If set, always convert image to the single channel grayscale image and the image size reduced 1/2.

IMREAD_REDUCED_COLOR_2 

Python: cv.IMREAD_REDUCED_COLOR_2

If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.

IMREAD_REDUCED_GRAYSCALE_4 

Python: cv.IMREAD_REDUCED_GRAYSCALE_4

If set, always convert image to the single channel grayscale image and the image size reduced 1/4.

IMREAD_REDUCED_COLOR_4 

Python: cv.IMREAD_REDUCED_COLOR_4

If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.

IMREAD_REDUCED_GRAYSCALE_8 

Python: cv.IMREAD_REDUCED_GRAYSCALE_8

If set, always convert image to the single channel grayscale image and the image size reduced 1/8.

IMREAD_REDUCED_COLOR_8 

Python: cv.IMREAD_REDUCED_COLOR_8

If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.

IMREAD_IGNORE_ORIENTATION 

Python: cv.IMREAD_IGNORE_ORIENTATION

If set, do not rotate the image according to EXIF's orientation flag.

 

常用的有三个:对应上面的表格中的最开始的三个加粗的选项

关于alpha channel的解释:

In graphics, a portion of each pixel's data that is reserved for transparency information. 32-bit graphics systems contain four channels — three 8-bit channels for red, green, and blue (RGB) and one 8-bit alpha channel. The alpha channel is really a mask. It specifies how the pixel's colors should be merged with another pixel when the two are overlaid, one on top of the other.

Typically, you wouldn't define the alpha channel on a pixel-by-pixel basis, but rather per object. Different parts of the object would have different levels of transparency depending on how much you wanted the background to show through. This allows you to create rectangular objects that appear as if they are irregular in shape — you define the rectangular edges as transparent so that the background shows through. This is especially important for animation, where the background changes from one frame to the next.

Rendering overlapping objects that include an alpha value is called alpha blending.

-from https://www.webopedia.com/TERM/A/alpha_channel.html  author:Vangie Beal

 官方完整的内容:

§ imread()

Mat cv::imread ( const String &  filename,
    int  flags = IMREAD_COLOR 
  )    
Python:
  retval = cv.imread( filename[, flags] )

#include

Loads an image from a file.

The function imread loads an image from the specified file and returns it. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix ( Mat::data==NULL ).

Currently, the following file formats are supported:

  • Windows bitmaps - *.bmp, *.dib (always supported)
  • JPEG files - *.jpeg, *.jpg, *.jpe (see the Note section)
  • JPEG 2000 files - *.jp2 (see the Note section)
  • Portable Network Graphics - *.png (see the Note section)
  • WebP - *.webp (see the Note section)
  • Portable image format - *.pbm, *.pgm, *.ppm *.pxm, *.pnm (always supported)
  • PFM files - *.pfm (see the Note section)
  • Sun rasters - *.sr, *.ras (always supported)
  • TIFF files - *.tiff, *.tif (see the Note section)
  • OpenEXR Image files - *.exr (see the Note section)
  • Radiance HDR - *.hdr, *.pic (always supported)
  • Raster and Vector geospatial data supported by GDAL (see the Note section)

Note

  • The function determines the type of an image by the content, not by the file extension.
  • In the case of color images, the decoded images will have the channels stored in B G R order.
  • When using IMREAD_GRAYSCALE, the codec's internal grayscale conversion will be used, if available. Results may differ to the output of cvtColor()
  • On Microsoft Windows* OS and MacOSX*, the codecs shipped with an OpenCV image (libjpeg, libpng, libtiff, and libjasper) are used by default. So, OpenCV can always read JPEGs, PNGs, and TIFFs. On MacOSX, there is also an option to use native MacOSX image readers. But beware that currently these native image loaders give images with different pixel values because of the color management embedded into MacOSX.
  • On Linux*, BSD flavors and other Unix-like open-source operating systems, OpenCV looks for codecs supplied with an OS image. Install the relevant packages (do not forget the development files, for example, "libjpeg-dev", in Debian* and Ubuntu*) to get the codec support or turn on the OPENCV_BUILD_3RDPARTY_LIBS flag in CMake.
  • In the case you set WITH_GDAL flag to true in CMake and IMREAD_LOAD_GDAL to load the image, then the GDAL driver will be used in order to decode the image, supporting the following formats: Raster, Vector.
  • If EXIF information are embedded in the image file, the EXIF orientation will be taken into account and thus the image will be rotated accordingly except if the flag IMREAD_IGNORE_ORIENTATION is passed.
  • Use the IMREAD_UNCHANGED flag to keep the floating point values from PFM image.
  • By default number of pixels must be less than 2^30. Limit can be set using system variable OPENCV_IO_MAX_IMAGE_PIXELS

Parameters

filename Name of file to be loaded.
flags Flag that can take values of cv::ImreadModes

 

  • 展示图片

使用cv::namedWindow

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

有两个参数:

void cv::namedWindow ( const String &  winname,
    int  flags = WINDOW_AUTOSIZE 
  )  

 需要使用头文件 #include

第二个参数可以使用"|"操作符号进行连接,例如:

 

namedWindow( "Display window", WINDOW_NORMAL | WINDOW_FREERATIO | WINDOW_GUI_EXPANDED )

上面的这种写法是有Qt作为IDE的时候才能使用的,如果不是Qt

那么第二个参数使用

WINDOW_AUTOSIZE

官方文档:

§ namedWindow()

void cv::namedWindow ( const String &  winname,
    int  flags = WINDOW_AUTOSIZE 
  )    
Python:
  None = cv.namedWindow( winname[, flags] )

#include

Creates a window.

The function namedWindow creates a window that can be used as a placeholder for images and trackbars. Created windows are referred to by their names.

If a window with the same name already exists, the function does nothing.

You can call cv::destroyWindow or cv::destroyAllWindows to close the window and de-allocate any associated memory usage. For a simple program, you do not really have to call these functions because all the resources and windows of the application are closed automatically by the operating system upon exit.

Note

Qt backend supports additional flags:

  • WINDOW_NORMAL or WINDOW_AUTOSIZE: WINDOW_NORMAL enables you to resize the window, whereas WINDOW_AUTOSIZE adjusts automatically the window size to fit the displayed image (see imshow ), and you cannot change the window size manually.
  • WINDOW_FREERATIO or WINDOW_KEEPRATIO: WINDOW_FREERATIO adjusts the image with no respect to its ratio, whereas WINDOW_KEEPRATIO keeps the image ratio.
  • WINDOW_GUI_NORMAL or WINDOW_GUI_EXPANDED: WINDOW_GUI_NORMAL is the old way to draw the window without statusbar and toolbar, whereas WINDOW_GUI_EXPANDED is a new enhanced GUI. By default, flags == WINDOW_AUTOSIZE | WINDOW_KEEPRATIO | WINDOW_GUI_EXPANDED

Parameters

winname Name of the window in the window caption that may be used as a window identifier.
flags Flags of the window. The supported flags are: (cv::WindowFlags)

 

  • 存储图片:

使用

cv::imwrite()

§ imwrite()

bool cv::imwrite ( const String &  filename,
    InputArray  img,
    const std::vector< int > &  params = std::vector< int >() 
  )    
Python:
  retval = cv.imwrite( filename, img[, params] )

注意以上的String 都是 cv::String不是std::string

你可能感兴趣的:(计算机视觉,计算机图形学,OpenCV4)