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个通道.

posted @ 2019-09-27 15:13  core!  阅读(610)  评论(0编辑  收藏  举报