Opencv2 Study Notes: Reading and displaying images

Source: Internet
Author: User

1. Image reading: imread ()

 Mat imread(const string& ?lename, int ?ags=1 )

Parameter introduction:

  Filename: Name of the file to be loaded.

  Flags: Specifies the color type of the loaded image ). The value of this flag can be:

--Cv_load_image_anydepth: If this flag is set, if the image is a 16-bit or 32-bit depth image, the corresponding depth image is returned; otherwise, the image is converted to an 8-bit depth image and then returned.

--Cv_load_image_color: If this flag is set, the image is always converted to a color image.

--Cv_load_image_grayscale: If this flag is set, the image is always converted to a grayscale image.

Of course, we can also choose not to use these three flags, as long as they are Integer Variables. However, the values must follow the following rules:

    Flags> 0: Returns a 3-channel color image. Note: In this implementation, the output image is not loaded into the alpha channel. If you need to use the alpha channel, use a negative value.

    Flags = 0:Returns a grayscale image.

    Flags <0: Returns an image in its own form (with an alpha channel ).

 

2. Image Display: imshow ()

void imshow(const string& winname, InputArray mat)

Parameter introduction:

  Winname:Display the window name of the image.

  MAT:The image to be displayed.

 

3. program example

The following code reads images in three different forms and displays them.

 1 #include "stdafx.h" 2 #include <opencv2\highgui\highgui.hpp> 3 #include <opencv2\core\core.hpp> 4  5 using namespace cv; 6  7  8 int _tmain(int argc, _TCHAR* argv[]) 9 {10     Mat img1 = imread("D:/Media/Image/girl03.jpg", -1);11     if (!img1.empty()) imshow("Alpha Image", img1);12 13     Mat img2 = imread("D:/Media/Image/girl03.jpg", 0);14     if (!img2.empty()) imshow("Gray Image", img2);15 16     Mat img3 = imread("D:/Media/Image/girl03.jpg", 1);17     if (!img3.empty()) imshow("Color Image", img3);18 19     waitKey();20 21     return 0;22 }
View code

The running result is as follows:

Flags at <0

Flags = 0

Flags> 0

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.