Opencv3 C++ VS2017 study notes 02 Mat object

  • Questions for study notes 00 and 01:
    • Two interactive windows appear, one is gray and the other is the result, as shown in the figure below
    • Solution: Open the project-properties-linker-input-additional dependencies, and delete one of the dependencies added when configuring opencv
    • dst.size is wrong, size is not an attribute of object dst, but a method function, dst.size(), similarly dst.type();

 

  • Mat
    • The Mat object is divided into two parts: header + data part
  • The use of Mat objects (there are also some programs, not listed here)
    • Mat constructor
      • Mat dst(2,2 CV_8UC3, Scalar(0,0,255));
        • Scalar brackets are the value of each element in the 2*2 matrix. Obviously, the 4 elements are the same here, that is (0,0,255)
      • Mat dst (row, column, CV_8UC3, specific pixel value);
        • CV_8UC3
          • 8: One pixel of each channel occupies 8 bits
          • U: Unsigned
          • C: char type
          • 3: 3 channels, which determine the number of parameters of Scalar()
    • Mat copy problem
      • Partial copy: only copy the header and pointer part of Mat, not the data part
        • Mat A = imread(B);
        • Mat C(D);
      • Full copy
        • Mat F = A.clone(); F still changes with A
        • Mat G; A.copyTo(G); AG does not interfere with each other
    • Define a small array filter
      • Mat filter = (Mat_<double>(3,3)<<0,-1,0,-1,5,-1,0,-1,0);
        • double is the matrix element type
      • Get the kernel matrix learned from yesterday
    • Another way to create a matrix
      • Mat m=Mat::zeros(src.size(), src.type());
        • All 0 matrix, the same size and type as src
      • Mat m = Mat::zeros(2,2, CV_8UC1);
        • All 0 matrix, 2*2, CV_8UC1 same as above
/***Mat对象的基本使用方法***/
int main(int argc, char ** argv)
{
    Mat src;
    src = imread("地址");
    if(!src.data)
    {
        cout<<"no data"<<endl;
        return -1;
    }
    namedWindow("src_imgae", WINDOW_AUTOSIZE);
    imshow("src_image", src);
    
    Mat dst;                                //创建一个Mat对象
    dst = Mat(src.size(), src.type());       //初始化一个Mat对象,和src的大小类型都一样
    dst = Scalar(123,12,132);             //赋值, Scalar标量,括号就是RGB的值
    namedWindow("dst_image", WINDOW_AUTOSIZE);
    imshow("dst_image", dst);
    
    Mat dst_clone =src.clone();            //克隆图像会随着源图像改变而改变

    Mat dst_copy;                //复制的图像不会因为源图像的变化而变化
    src.copyTo(dst_copy);
    
    Mat dst_cvt;
    cvtColor(src, dst_cvt, RGB2GRAY);        //改变颜色空间
    cout<<"src' channels: "<<endl;
    cout<<"dst_cvt's channels: "<<endl;
    const uchar* dst_cvt_firstRow = dst_cvt.ptr<uchar>(0);
    cout<<"dst_cvt's firstRow: "<<*dst_cvt_firstRow<<endl;

    int cols = dst.cols;      //宽度
    int rows = dst.rows;       //高度
    
    Mat dst_mat(2,2,CV_8UC3,Scalar(12,122,1));    

    waitKey(0);
    return 0; 

}

 

Guess you like

Origin blog.csdn.net/Mrsherlock_/article/details/104486339
Recommended