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

Source: Internet
Author: User
Tags image processing library shallow copy

1--We know Mat is an image container class, this data structure consists of two parts: 1--Matrix Header------------------------------------------------- Just created a mat header and did not create a matrix, that is to say, we did not open up the corresponding space for the image to be stored 2--matrix header-Contains: the size of the 1--matrix----such as---class mat----data members in this class Rows,cols ---can specify the size of the image 2--storage method------Corresponding---a variety of mat constructors 3--store address 4--and a----pointer to a matrix that----storing all pixel values 2--Therefore, when you pass an image and create a copy in your program, the large overhead is made by the matrix
into, not the message head. 3--OPENCV is an image processing library, contains a large number of image processing functions, in order to solve the problem, usually use a library of multiple functions, so it is common to pass images in a function. Also, don't forget that we're talking about a large computational graph processing algorithm, so unless you have to, We should not copy large images because it lowers the speed of the program 4--in order to fix this problem, OPENCV uses the---Reference counting mechanism, the idea is to let each mat object have its own message header, but share a matrix. Implemented by having the matrix pointer point to the same address.
		
The copy constructor copies only: the 1--information header 2--The matrix pointer and not the matrix.
        /********************************************************************************************* Program Features: 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.25 author information: September ****************************************************************************************/
/********************* "Header file.namespace contains part "***********************************/#include <opencv2/highgui/highgui.hpp> #include <opencv2/
Core/core.hpp> #include <iostream> using namespace CV;


using namespace Std; #define MAT_INFO_HEADER_SIZE MAT The size of the memory space occupied by the image container class header-----> "#define WINDOW_SRC_NAME" "Original image" #define Window_dst_na ME "copy constructor image" "#define WINDOW_ASSIGN_NAME" "Assignment image" "#define Window_roi_name" "ROI Original image part data" "#define Window_r Ange_name The image data section specified in the range "/*****************************************" main () function ******************************* /int main (int argc,char** argv) {/**** * The point to be explained here is that there is a huge difference between the definition of a class object in C + + and the definition of an object in Java: * "1" C + +, when the object of a class is defined, it is     The storage space of the class is allocated---the popular point, that is, in C + +, when using * A class to define a class object, in fact, the class---is instantiated, resulting in a class of----concrete instance * "2" and in Java, the definition of class objects and the instantiation of objects by classes are performed separately: * "1" person Lili---Class object definition---opened up a four-byte space----Lili This class object is actually equivalent to * C + + An object pointer, at this time does not produce the---concrete instance of class * "2" Java can only manually, with
	The new keyword instantiates the object of a class, as follows: * lili=new person (); ////"1" only created informationThe head part Mat Src,assign;
    
	"2" Here we test the size of the MAT header for memory space cout<<mat_info_header_size<<sizeof (src) << "bytes" <<endl;
    "3" here, for the matrix to open up memory space---which is equivalent to---matrix body src=imread ("D:\\scenery.png", Cv_load_image_color);


	"4" Display picture imshow (WINDOW_SRC_NAME,SRC);
	"5" uses the copy constructor to copy only the information header of the matrix-----Typical shallow copy Mat DST (SRC);
	ASSIGN=SRC;
	"6" Display picture imshow (WINDOW_DST_NAME,DST);

	Imshow (window_assign_name,assign); /**** * "1" Through the above code results are known. All Mat objects end up pointing to one and only one----data matrix. Although their * information headers are different, however, the changes made to the image matrix by any one object will also affect other objects * "2" in fact, different mat objects, just different ways to access the same data ****//**** * "1" here introduces a more powerful function: you can Creates an information header that references only the partial data---of the---image matrix. For example, to create an area of interest (ROI), you only need to create an information header * "2" that contains boundary information: * Mat Dstroi (Src,rect (0,0,100,100)) * Mat dstroi_1 (Ran
	Ge:all (), range (1,3)) ****///"7" uses a rectangle to define the ROI region 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 row rows and column cols to intercept an image//"10" of the specified SRC image in the specified area of the original image, including all rows of the images and from columns No. 0 through 199th//"11" MatMat::operator () (range _rowrange, range _colrange) const----Create//new headers for the SRC object's child array, the following equivalent Src.colrange () Mat Dstra
	NGE=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, then who will be responsible for clearing it when it is not needed? The answer is: The last one uses its object.
	Implemented by reference counting mechanism. Whenever someone copies the header of a mat object, it increases the number of references to the matrix; Conversely, when a head is released, the count is reduced by one, and when the count is zero, the matrix is cleared. * "2" in this piece of the class object that we created through the mat src method is a static object of the----class, in the process of running a program, such objects * occupy the space allocated and the point of release is fixed ***//** * "1" but, at some point, you will still want to copy the matrix itself (no
	Just the information header and the matrix pointer), you can use---deep copy---function: * Mat clone () const;
	* void CopyTo (outputarray m) const;  
	* void CopyTo (Outputarray m, Inputarray mask) const;
	**/Mat Dstdeep1=src.clone ();
	Mat DstDeep2;
    Src.copyto (SRC);
	"12" After deep replication, at this time, we change again, will no longer affect the original image//"13" For example, we now put all the elements in the image to white//"14" Access color image 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 ("" After the deep copy processing Image "", DSTDEEP1);
Waitkey (0); Now, to sum up, we need to keep in mind that the memory allocation of the output image in the 1--OPENCV function is automatically completed (if not specifically specified) 2--use OpenCV's C + + interface without the need to consider memory release issues 3-- Copy operators and copy constructor functions only copy---header 4--use the function clone () or CopyTo () to copy a matrix of an image

 

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.