Imread function, Namedwindow function, imshow function, Imwrite function

Source: Internet
Author: User

1.imread function

First, we look at the Imread function, which can be found in the OPENCV official documentation as follows:

Mat imread (const string& filename, int flags=1);

Where the first parameter, the const string& type filename, fills in the picture path name we need to load.

Under the Windows operating system, OPENCV's Imread function supports the following types of image loading:

    • JPEG Files-*.jpeg, *.jpg, *.jpe
    • JPEG 2000 File-*.JP2
    • PNG image-*.png
    • Portable file Formats-*.PBM, *.PGM, *.ppm
    • Sun rasters Raster Files-*.SR, *.ras
    • TIFF files-*.tiff, *.tif
    • Windows Bitmap-*.bmp,*.dib

The second argument, flags of type int, is the load identity, which specifies a color type for the loaded image. You can see that it comes with a default value of 1. So sometimes this parameter can be ignored at the time of invocation, and after looking at the following explanation, we will find that if we omit this parameter at the time of invocation, it means to load a three-channel color image.

You can take a value in the enumeration body that identifies the image format in OpenCV. By going to the definition, we can find the definition of this enumeration in Higui_c.h:

  1. Enum
  2. {
  3. /* 8bit, color or not */
  4. Cv_load_image_unchanged =-1,
  5. /* 8bit, Gray */
  6. Cv_load_image_grayscale = 0,
  7. /*?, Color */
  8. Cv_load_image_color = 1,
  9. /* Any depth,? */
  10. Cv_load_image_anydepth = 2,
  11. /*?, any color */
  12. Cv_load_image_anycolor =4
  13. };

The corresponding explanation:

    • Cv_load_image_unchanged, this identity is abandoned in the new version, ignored.
    • cv_load_image_anydepth-If this sign is taken, if the image is loaded with a depth of 16 or 32 bits, the image of the corresponding depth is returned, otherwise it is converted to a 8-bit image and returned.
    • cv_load_image_color-If you take this logo, you always convert the image into color.
    • cv_load_image_grayscale-the image is always converted to grayscale if you take this identity 1

If the input has conflicting flags, a smaller numeric value will be used. Like Cv_load_image_color | The Cv_load_image_anycolor will load a 3-channel diagram.

If you want to load the most realistic image, choose Cv_load_image_anydepth | Cv_load_image_anycolor.

Because flags is a variable of type int, if we do not take values in this enumeration, we can do the following:

    • Flags >0 Returns a 3-channel color image.
    • Flags = 0 Returns a grayscale image.
    • Flags <0 returns the loaded image that contains the alpha channel.

Points to note: The output image is not loaded into the alpha channel by default. If we need to load the alpha channel, we need to take a negative value here.

If you're funny, and the flags take 1999, it's also possible to return a 3-channel color image.

2.namedWindow function

As the name implies, the Namedwindow function is used to create a window. The function prototype is this:

    1. void Namedwindow (const string& winname,int flags=window_autosize);

      The first parameter, the const string& name, fills the window names that are used as window identifiers.

      The second argument, the int type of flags, identifies the window, which can be filled with the following values:

      • Window_normal set this value, the user can change the size of the window (No limit)
      • Window_autosize If this value is set, the window size is automatically adjusted to fit the displayed image, and the window size cannot be changed manually.

Window_opengl If this value is set, OPENGL is supported when the window is created.

Function anatomy:

The first thing to note is that it has a default value of Window_autosize, so, in general, this function we fill a variable on the line.

The purpose of the Namedwindow function is to create a container window that can act as an image and a progress bar by specifying the name. If a window with the same name already exists, the function doesn't do anything.

We can call the DestroyWindow () or destroyallwindows () function to close the window and cancel any previously allocated memory space associated with the window.

But that said, in fact, for a small code-less simple program, we have no need to manually invoke the above DestroyWindow () or destroyallwindows () function, because at the exit, all the resources and application windows will be automatically closed by the operating system.

3.imshow function

Displays an image in the specified window.

    1. void Imshow (const string& winname, Inputarray mat);
    • The first parameter, the const string& type of winname, fills the window identification name that needs to be displayed.

      The second parameter, the Inputarray type of mat, fills the image that needs to be displayed.

The Imshow function is detailed:

The Imshow function is used to display the image in the specified window. If the window was created with the Cv_window_autosize (default) flag, the original size of the image is displayed. Otherwise, the image is scaled to fit the window. The Imshow function scales the image, depending on the depth of the image:

    • If the loaded image is a 8-bit unsigned type (8-bit unsigned), the image will be displayed.
    • If the image is a 16-bit unsigned type (16-bit unsigned) or 32-bit integer (32-bit integer), the pixel value is divided by 256. In other words, the range of values is [0,255 x 256] mapped to [0,255].
    • If the image is a 32-bit floating-point type (32-bit floating-point), the pixel value is multiplied by 255. That is, the range of the value is [0,1] mapped to [0,255].
Iv. output image to file--imwrite function

In OpenCV, the output image to the file, we generally use the Imwrite function, it is declared as follows:

    1. BOOL Imwrite (const string& filename,inputarray img, const vector<int>& params=vector<  int> ());

The first argument, the const string& type filename, fills in the file name that needs to be written, with the suffix, for example, "123.jpg".

The second parameter, the Inputarray type of IMG, generally fills in a mat type of image data on the line.

The third parameter, the const vector<int>& type of params, represents a parameter encoding saved for a particular format, which has a default value of vector<int> (), so it is generally not required to be filled in.

Imread function, Namedwindow function, imshow function, Imwrite function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.