0

I am reading openCV documentation.

I read this paragraph:

The idea is that each Mat object has its own header, however the matrix may be shared between two instance of them by having their matrix pointers point to the same address. Moreover, the copy operators will only copy the headers and the pointer to the large matrix, not the data itself.

Mat A, C;                                 // creates just the header parts
A = imread(argv[1], CV_LOAD_IMAGE_COLOR); // here we'll know the method used (allocate matrix)

Mat B(A);                                 // Use the copy constructor

C = A;                                    // Assignment operator

All the above objects, in the end, point to the same single data matrix. Their headers are different, however, and making a modification using any of them will affect all the other ones as well. In practice the different objects just provide different access method to the same underlying data. Nevertheless, their header parts are different.

What is the use of having different headers though their image data is same ?

Bhawan
  • 2,441
  • 3
  • 22
  • 47
  • `What is the use of having different headers though their image data is same ?` (1) To save memory, (2) To operate on the same data use different name. – Kinght 金 Jan 10 '18 at 06:46
  • A, B and C will have same image data but different headers. What is the use of having different headers , that is my query. – Bhawan Jan 10 '18 at 06:47
  • `(1) To save memory, (2) To operate on the same data use different names. ` This is my answer. – Kinght 金 Jan 10 '18 at 07:35
  • 3
    Maybe an ROI (Region Of Interest) is a better example. You have one, big image and another Mat which is only a small rectangle within it, so you just process the smaller ROI (for speed/efficiency reasons) but the relevant region of the larger image is also updated. – Mark Setchell Jan 10 '18 at 07:42
  • 1
    One more thing, a cv::Mat behaves as a smart pointer (shared ones) which tracks how many of them point to the data and deletes it when none points to it (some restrictions apply to this). This way you may have different aliases to the same data. Also, different functions expect the data in different ways, for instance you may put all the pixels in one row or one column or as a matrix or you may want to process a part of it and copying it every time is expensive, specially in real time algorithms. – api55 Jan 10 '18 at 08:31

0 Answers0