Opnecv notes (1) Image load, display, save, change grayscale

Source: Internet
Author: User
Tags function prototype scalar

Image loading, displaying, saving functions:

1 Image Load function: Imread () Mat imread (const string& filename, int flags=1);

The const string& type filename is the path that loads the image (absolute path and relative path)

Flags is a variable of type int, flags>0, which returns a 3-channel color image;

Flags = 0, returns the grayscale image;

Flags < 0, returns the loaded image containing the alpha channel.

The default value of flags is 1, which can be omitted for assignment.

For example: Mat srcimage = Imread (' 1.jpg ');

2 Image Display function: Imshow () void imshow (const string& winname, Inputarray mat);

The winname of the Const string& type is the name of the window that needs to be displayed;

Inputarray type of mat, fill in the image that needs to be displayed.

For example: Imshow (' figure ', srcimage);

The 3 mat type is a data structure for saving images and other matrix data, the default size is 0, or you can initialize dimensions. Inputarray/outputarray in Opnecv and Mat is a kind of data format, and many times, encountered in the function prototype of the Inputarray/outputarray type, you can simply consider it as the mat type.

4 output image to file: Imwrite () bool Imwrite (const string& filename,inputarray img, const vector<int> & Params=ve Ctor<int> ());

The const string& filename indicates that the required file name needs to be appended with the suffix, such as "1.jpg";

Inputarray img Represents the image data to be saved;

The const vector<int> & type params, whose default value is Vector<int> (), needs to be filled in:

1. JPEG format picture, the parameter is 0-100, indicates the image quality, the default is 95;

2. PNG format picture, the parameter is 0-9, indicates the compression level, the high value indicates the smaller size and the compression time is longer;

3. PPM,PGM,PBM format, represents the binary format flag, parameter 0 or 1, which defaults to 1.

For example: Imwrite (' 123.jpg ', dstimage);

Base Image Container Mat

5 using the mat () constructor

Mat M (2,2,cv_8uc3,scalar (0,0,255)), which represents the creation of 2 rows and 2 columns, CV_8UC3 represents a three-channel with 8-bit unsigned char, each pixel consisting of three elements. In addition, scalar is a short vector that can initialize the matrix with the specified customizations and also represent the color.

Common data structures and functions

6 points: Point

Usage: Point point;

Point.x = 10;

Point.y = 8;

Or: Point point = Point (10,8); are represented as 2D points with x=10 and y=8 positioning.

7 Expression of the color: Scalar

Scalar (a,b,c) indicates that the defined RGB values are: The red component is C, the green component is B, and the blue component is a. Depending on the opnecv and MATLAB different storage three channel color mode, MATLAB for the RGB sequence, opnecv for BGR, can also be understood as the opposite.

8 Rectangle representation: Rect

The member variables of the Rect class have x,y,width,height that represent the upper-left corner of the coordinate and rectangle width and height. Its member functions are:

The size () return value is size (size), area () returns the region of the rectangle, contains (point) determines whether the dot is within the rectangle, inside (Rect) determines whether the rectangle is within the rectangle, the TL () returns the upper-left corner coordinates, and BR () returns the lower-right point coordinates.

Rect rect = rect1 & rect2; Intersection of two rectangles

Rect rect = rect1 & tect2; Two rectangles and a set

Rect rectshift = rect + point; Rectangular panning

Rect rectscale = rect + size; Rectangular Zoom

9 Color space Conversion: Cvtcolor ()

Where RGB and gray conversions are: Opnecv2 version of Cv_rgb2gray,cv_gray2rgb

Opencv3 version of Color_rgb2gray,color_gray2rgb

RGB and HSV Conversions are: OPNECV2 version of CV_RGB2HSV,CV_BGR2HSV,CV_HSV2RGB,CV_HSV2BGR

Opencv3 version of COLOR_RGB2HSV,COLOR_BGR2HSV,COLOR_HSV2RGB,COLOR_HSV2BGR

That is, for color conversion, the macro naming specification for the CV_ prefix of the OPNECV2 is replaced by the COLOR_-style macro naming prefix in Opnecv3, and the default picture channel storage order in OPNECV is BGR.

10 General Examples:

#include <opnecv2/opencv.hpp>

using namespace CV;

int main ()

{

Mat srcimage = Imread ("1.jpg");

Mat Dstimage;

Cvtcolor (Srcimage,dstimage, Color_bgr2gray);

Imwrite ("Figure 1.jpg", srcimage);

Imshow ("original", Srcimage);

Imshow ("Transform graph", dstimage);

Waitkey (0);

return 0;

}

Opnecv notes (1) Image load, display, save, change grayscale

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.