OPENCV Study Notes (iv)--MAT basic image Container Mat object information header, matrix creation, deep copy, shallow copy detailed

Source: Internet
Author: User
Tags image processing library

We know that Mat is an image container class, a data structure consisting of two parts: the head of the matrix-the data stored in the space created by class objects instantiated by class mat classes---is the information of this matrix, when we use Mat object to declare class objects, It just creates a mat header and does not create a matrix, that is, we do not have space for the image that will be stored 2--matrix head-contains: the size of the matrix----such as---class mat----data members in this class rows, COLS---You can specify the size of the image 2--storage method------corresponds to---Various mat constructors 3--store address 4--and a----pointer to a matrix that----store all pixel values 2--so when you pass an image and create a copy in a program, The large overhead is caused by the matrix, not the information header. 3--OPENCV is an image processing library that includes a large number of image processing functions, and in order to solve the problem, it is often necessary to use multiple functions in the library, so it is a common thing to pass an image in a function. Also, don't forget that we're talking about a computationally large graph processing algorithm, so unless it's a last resort, We should not copy large images, because this will reduce the speed of the program 4--in order to solve this problem, OPENCV uses the---Reference counting mechanism, the idea is to let each mat object has its own information header, but share a matrix. Implemented by having the matrix pointer point to the same address. The copy constructor is only copied: the 2--matrix pointer without copying the matrix./***************************************************************************** Program function: Mat basic image Container Mat object information header, matrix body creation, deep copy, shallow copy detailed writing environment: opencv2.4.8+vs2010 location Time: Shaanxi Normal University 2016.4.2 5 Author Information: September **********************************************************************************************//****** "Header file. namespace contains section" ***********************************/#include <opencv2/highgui/highgui.hpp> #include <opencv2/core/core.hpp> #include <iostream>using namespace CV; using namespace Std; the size of the memory space occupied by the #define MAT_INFO_HEADER_SIZE "MAT Image Container class header----->" #define WINDOW_SRC_NAME "" Original image "#de Fine Window_dst_name "" Copy constructor Image "" #define WINDOW_ASSIGN_NAME "" Assignment image "" #define Window_roi_name "" ROI Original image partial Data "" # Define Window_range_name "" RANGE Specified image Data section ""/***************************************** "Main () function" ***************** /int Main (int argc,char** argv) {/***** One thing to note here is that the definition of class objects in C + + is significantly different from the definition of objects in Java: * "1" in C + +, When an object of a class is defined, it is assigned the storage space of the class---say, in C + +, when you use a class to define a class object, you actually instantiate the class---, resulting in a class of----concrete instance * "2" and Java,     The definition of the class object and the instantiation of the class to the object are performed separately: * "1" person Lili---Class object definition---opens up a four-byte space----Lili This class object is actually equivalent to an object pointer in C + +, and there is no---concrete instance of the class being generated * "2" Java can only be manually, with the new keyword to instantiate a class of objects, as follows: * lili=new person (); ****///"1" only created the information header part mat src,assign;//"2" We'll test it here, Mat. The size of the memory space occupied by the head cout<<mat_info_header_size<<sizeof (SRC) << "bytes" <<endl;    "3" here, for the matrix to open up memory space---this equivalent to---matrix Src=imread ("D:\\scenery.png", Cv_load_image_color); "4" Display picture imshow (WINDOW_SRC_NAME,SRC);//"5" Use copy constructor, copy only information header of matrix-----Typical Shallow copy Mat DST (SRC); assign=src;//"6" Displays picture Imshow ( WINDOW_DST_NAME,DST); Imshow (window_assign_name,assign);/***** "1" is known through the results of the above code. All the Mat objects end up pointing to a----data matrix that is also unique. Although their * information headers are different, the changes made to the image matrix by any object can affect other objects * "2" in fact, different mat class objects are just different ways of accessing the same data. ****//***** "1" Here is a more powerful feature: You can create information headers that reference only part of the data------image matrix. For example, to create an area of interest (ROI), you only need to create an information header with boundary information * "2" instance as follows: * Mat Dstroi (Src,rect (0,0,100,100)) * Mat dstroi_1 (Range:all () , Range (1,3)) ****///"7" uses a rectangle to define the ROI area Mat Dstroi (Src,rect (0,0,200,200));//"8" Create window + display image Namedwindow (window_roi_name , cv_window_autosize); Imshow (window_roi_name,dstroi);//"9" uses rows rows and columns cols to intercept images of the specified area of the original image//"10" The area of the specified SRC image includes all lines of the image and columns from No. 0 to 199th//"11" Mat Mat::operator (range _rowrange, range _colrange) const----created for a sub-array of SRC objects/ /new information header, below the equivalent of Src.colrange () Mat dstrange=src (Range::all (), Range (0,200)); Namedwindow (Window_range_name,cv_window_autosize); Imshow (window_range_name,dstrange);/*** "1" Now you may ask, if the matrix belongs to more than one Mat object, who is responsible for cleaning when it is not needed? The answer is: the last * object that uses it. Implemented by reference counting mechanism. Whenever someone copies a mat object's header, it increases the number of references to the matrix; Conversely, when a head is released, the count is reduced by one; When the count is zero, the matrix is cleaned up. * "2" in this method we created by way of Mat Src is a static object of the----class, in the process of running the program, such an object * occupies space allocated and the time of release is fixed ***//*** "1" but, at some point, you still want to copy the Matrix itself ( Not just information headers and matrix pointers), you can copy---functions with---deep: * Mat clone () const;* void CopyTo (outputarray m) const;* void CopyTo (Outputarray m, I  Nputarray mask) const; **/mat Dstdeep1=src.clone ();    Mat Dstdeep2;src.copyto (SRC); "12" After deep copy, then, we change again, will not affect the original image//"13" For example, we now set all the elements in the image as white//"14" To access the color image of the pixel for (int i=0;i<dstdeep1.rows;i++) {for (int j=0;j<dstdeep1.cols;j++) {dstdeep1.at<vec3b> (i,j) [0]=255;//blue channel dstdeep1.at<vec3b> (I,J) [1 ]=255;//Red channel dstdeep1.at<vec3b> (I,J) [2]=255;//Green channel}}imshow ("Original image" ", SRC); Imshow (" "Image after deep copy processing" ", DSTDEEP1 ); Waitkey (0);} Now, to summarize, we need to remember that the memory allocation for the output image in the 1--OPENCV function is automatic (if not specifically specified) when 2--uses the OpenCV C + + interface. No need to consider memory release issues 3--copy operator and copy constructor only copy---information header 4--using function clOne () or CopyTo () to copy the matrix of a pair of images 



OPENCV Study Notes (iv)--MAT basic image Container Mat object information header, matrix creation, deep copy, shallow copy detailed

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.