OpenCV Getting Started: (ii: Loading, displaying, modifying and saving pictures)

Source: Internet
Author: User

Goal:

1. Open Picture from picture file (Imread)

2. Display pictures (Namedwindow and Imshow)

3. Convert the current picture to a gray picture (Cvtcolor)

4. Save Picture (Imwrite)

Code:
#include <opencv2/core/core.hpp>#include<opencv2/highgui/highgui.hpp>#include<opencv2/opencv.hpp>#include<iostream>using namespaceCV;using namespacestd;intMainintargcChar**argv)    {Mat image; //load image from file to imageImage = Imread ("lena.jpg", Cv_load_image_color); //If the picture fails to load    if(!image.data) {cout<<"Read image failed"<<Endl; return-1; }    //Create a Display picture windowNamedwindow ("Ori Pic", window_autosize); //Display windowImshow ("Ori Pic", image); //waiting for user keysWaitkey (Ten); //Create a new matrix forMat Grayimage; //convert picture to Gray, save in GrayimageCvtcolor (Image,grayimage,cv_bgr2gray); //save Grayimage to fileImwrite ("lena_gray.jpg", Grayimage); //Create a gray picture windowNamedwindow ("Gray Pic", window_autosize); //Show Gray pictureImshow ("Gray Pic", Grayimage); //waiting for user keysWaitkey (0); return 0;}
API Description:

Mat: The structure of the image data is stored, which is explained in detail later.

Mat imread (conststringint flags=1 )

FileName: The file name that needs to be loaded

Flags: Loads the color type of the image and can take the following values.

Cv_load_image_anydepth returns a 16/32-bit image if not set, returns a 8-bit image Cv_load_image_color returns a 8-bit image Cv_load_image_grayscale returns a grayscale image >0  Returns a three-channel image =0  Returns a gray image <0 returns an image with an alpha channel

void Namedwindow (conststringint flags=window_autosize)

Winname: Window name, also the identifier of the window

Flags: Creates a window ID that can be the following value

Window_normal windows can be unconstrained to modify the size window_autosize automatically set the window size to fit the size of the picture, and cannot modify Window_opengl can create a window that supports OPENGL

void imshow (conststring& Winname, Inputarray mat)

Winname: The name of the window

Mat: The picture data that is displayed.

This function is commonly used with Waitkey, Waitkey (0) displays the window until the user presses the key, Waitkey (10) Displays the window 10ms.

void int int dstcn=0 )

Image of SRC input
DST and SRC have the same size and depth (depth) of the output picture buffer
Code Color space Conversion code
DSTCN the color channel of the target buffer if 0 sets the same channel as the source image

bool imwrite (conststringconst vector<intparams=vector <int> ())

FileName: Save file name

IMG: Saved Pictures

Params: A container for specifying the format of the data store, appearing in this format, Paramid_1, Paramvalue_1,paramid_2, paramvalue_2.

For JPEG images: Parmid can be: cv_imwrite_jpeg_quality represents picture quality. Paramvalue_1 can be 0-100 (the higher the picture quality the better), the default is 95.

For PNG images: Parmid can be: Cv_imwrite_png_compression represents the compression level. Paramvalue_1 can be 0-9 (the higher the picture the smaller), the default is 3.

For PPM, PGM, and PBM images: Parmid can be: cv_imwrite_pxm_binary represents binary formatting flags (binary format flag). The paramvalue_1 can be 0 or 1. The default is 1.

Show effect

Run the above code:

Color images are displayed first:

After 10 seconds

At the same time, a picture named Lena_gray.jpg is added to the current directory.

This article concludes.

OpenCV Getting Started: (ii: Loading, displaying, modifying and saving pictures)

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.