OPENCV Learning Notes-----Understanding Mat

Source: Internet
Author: User
Tags scalar
First, Mat Foundation
1, in the computer memory, the digital image is used in the form of matrix preservation. In Opencv2, a data structure mat array is saved for image Pixel information, which consists of two parts: a matrix header and a matrix pin pointing to the pixel data. Matrix header mainly includes: Matrix size, storage mode, storage address, reference count and so on. The size of the matrix head is certain and will not change with the size of the image, usually the image pixel data will be very large. Therefore, there is no need to copy the entire MAT data during the copy and transfer of the image, just copy the matrix header and pointer to the pixel matrix.
2, there will be more than one mat to share a matrix of data, then memory recovery when the problem of releasing matrix data, for the release of memory when the reference count will work, when the Mat object is copied once, the reference count will be added one, and destroy a Mat object ( When the same image matrix data is shared), the reference count is reduced by one, and when the reference count is 0, the matrix data is removed.
3. When using mat:
A, OpenCV memory is usually automatically assigned, special circumstances need special designation.
B. Memory release is not a concern when using OpenCV's C + + interface.
c, you need to replicate the matrix data, you can use Image.clone () and Image1.copyto (Image2).

Second, Mat storage method
Different data types can be used for each element in the mat matrix, with the smallest data type char, which occupies one byte (1byte=8bits) and can be signed (0 to 255) and unsigned (-127 to 127). In the RGB color space, three char types can be used to represent 16 million colors. Cv::scalar (b,g,r) or Cv_rgb (R,G,B) can be used in OPENCV.

Third, the creation of the mat
1. Constructor function
The official documentation specifies the following rules:
Cv_[the number of bits per item][signed or unsigned][typeprefix]c[the channel number]
The four sections specify: the size of the element, whether it is signed or unsigned, the data type, and the number of channels. Cv::mat img (CV_8UC3, Cv::scalar (0,0,255));
The code above creates a matrix of 20 rows and 20 columns, the matrix element is saved with a 8-bit unsigned char type, has 3 channels, the initial value of each pixel is (0,0,255) red, and the scalar is a short vector that provides the initialization of the Matrix.
2. Create method
The method cannot set an initial value for the matrix, but re-allocates memory for the matrix data when the dimension is changed. Create a 4-row, 4-column matrix with 2 channels:
Img.create (4, 4, CV_8UC2);

3. Description of several special matrix initialization methods
Cv::mat e = Cv::mat::eye (4,4,cv_64f);
Cv::mat o = cv::mat::ones (2,2,cv_32f);
Cv::mat z = Cv::mat::zeros (3,3,CV_8UC1);
Mat e is a diagonal matrix of 4 rows and 4 columns
Mat Z is a unit matrix of 2 rows and 2 columns
Mat O is a 0 matrix of 3 rows and 3 columns

4. Initialization of small matrices
Mat m = (mat_<double> (3,3) <<-3,-2,-1,0,1,2,3,4,5);

Iv. input and output of mat
Using the Imread function, write an image to the Mat object:
Mat m = Cv::imread (".//res//lena.png");//read into the image Imread prototype:
Cv::mat imread (const string& filename,int Flags=1)
filename Specifies the location of the image to be read flags specifies the color space of the image
Flags > 03-Channel Color Images
Flags = 0 Grayscale image
Flags < 0 No changes can also have the following enumeration values
Cv_load_image_anydepth,
Cv_load_image_color,
Cv_load_image_grayscale
Use the Imwrite function to save the mat object to the specified file. The function prototypes for Imwrite are as follows:

BOOL Imwrite (const string& filename,inputarray img,constvector<int>& params=vector<int> ())
filename, the specified file
IMG To save the Mat object
The params is used to specify how the image is saved and encoded.
Use the filename extension to specify the save format (. jpg. png. bmp) for the image, and the params is a different value for different image preservation types

The jpeg,params is used to specify the quality of the image (0 to +), cv_imwrite_jpeg_quality

The default is 95.

The png,params is used to specify the compression level of the image (0 to 9), the higher the compression level, the less space the image takes, and the longer it takes to save the image. The default value is 3. Cv_imwrite_png_compression

Ppm,pgm,pbm,params is a marker (0 or 1), cv_imwrite_pxm_binary

The default is 1. Imwrite can only hold 8-bit (or 16-bit unsigned (CV_16UC) png,jpeg200 or TIFF images) single-channel or three-channel images, and if you want to save a picture other than this, you can use ConvertTo or cvtcolor to make the transition.




        




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.