opencv Mat基礎


Mat

Mat由兩部分構成

  • matrix header
  • pointer to the matrix containing the pixel values

Mat is basically a class with two data parts: the matrix header (containing information such as the size of the matrix, the method used for storing, at which address is the matrix stored, and so on) and a pointer to the matrix containing the pixel values (taking any dimensionality depending on the method chosen for storing) . The matrix header size is constant, however the size of the matrix itself may vary from image to image and usually is larger by orders of magnitude.

Mat A, C;                          // creates just the header parts
A = imread(argv[1], IMREAD_COLOR); // here we'll know the method used (allocate matrix)
Mat B(A);                                 // Use the copy constructor
C = A;                                    // Assignment operator

A,B,C的matrix header不同,但是pointer是一樣的.指向同樣的內存.修改一個會影響另一個.

Mat D (A, Rect(10, 10, 100, 100) ); // using a rectangle
Mat E = A(Range::all(), Range(1,3)); // using row and column boundaries

可以通過如上代碼方式使得matrix header指向全部數據的一個子集.

Mat F = A.clone();
Mat G;
A.copyTo(G);

opencv提供了深拷貝的方法 cv::Mat::clone() and cv::Mat::copyTo() ,會將數據部分也拷貝.

存儲方式

opencv默認的是bgr的順序.
顏色空間有好多種

  • RGB is the most common as our eyes use something similar, however keep in mind that OpenCV standard display system composes colors using the BGR color space (a switch of the red and blue channel).
  • The HSV and HLS decompose colors into their hue, saturation and value/luminance components, which is a more natural way for us to describe colors. You might, for example, dismiss the last component, making your algorithm less sensible to the light conditions of the input image.
  • YCrCb is used by the popular JPEG image format.
  • CIE Lab* is a perceptually uniform color space, which comes handy if you need to measure the distance of a given color to another color.

Mat M(2,2, CV_8UC3, Scalar(0,0,255));
CV_[The number of bits per item][Signed or Unsigned][Type Prefix]C[The channel number]
比如CV_8UC3代表每個像素值是一個8bit的unsigned char代表(表達范圍0-255),有3個通道.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM