opencv C++构造并访问单通道和多通道Mat。

一:构造并访问单通道。

int main(){
    cv::Mat m=(cv::Mat_<int>(3,2)<<1,2,3,4,5,6);
    for(int i=0;i<m.rows;++i){
        for(int j=0;j<m.cols;++j)
            std::cout<<m.at<int>(i,j)<<",";  // row Index along the dimension 0,col Index along the dimension 1
        std::cout<<std::endl;
    }
    // cv::Size size=m.size();
    std::cout<<"size:"<<m.size()<<std::endl;  // m.size():return the width and the height.
    std::cout<<"channel:"<<m.channels()<<std::endl;  // m.channels():The method returns the number of matrix channels.
    std::cout<<"total:"<<m.total()<<std::endl;  // m.total():The method returns the number of array elements
    std::cout<<"dimension:"<<m.dims<<std::endl;  // m.dims:the matrix dimensionality, >= 2.
    for(int i=0;i<m.rows;++i){
        const int *ptr=m.ptr<int>(i);  // i-based row index.
        for(int j=0;j<m.cols;++j)
            std::cout<<ptr[j]<<",";
        std::cout<<std::endl;
    }
    if(m.isContinuous()){
        const int *ptr=m.ptr<int>(0);  // 0-based row index.
        for(int i=0;i<m.rows*m.cols;++i)
            std::cout<<ptr[i]<<",";
        std::cout<<std::endl;
    }
    for(int i=0;i<m.rows;++i){  // 与下面相似
        for(int j=0;j<m.cols;++j){
           int *ptr=(int*)(m.data+m.step[0]*i+m.step[1]*j);
            std::cout<<*ptr<<",";
        }
        std::cout<<std::endl;
    }
    for(int i=0;i<m.rows;++i){
        for(int j=0;j<m.cols;++j)
            std::cout<<*((int*)(m.data+m.step[0]*i+m.step[1]*j))<<",";
        std::cout<<std::endl;
        // 二维图像:step[0]代表每一行所占的字节数,而如果有间隔的话,这个间隔也作为字节数的一部分被计算在内.
        // 二维图像:step[1]代表每一个数值所占的字节数.
        // data是指向第一个数值的指针,类型为uchar.
        // (int*)(BaseAddr+IER)把它强制变成int型指针.
        // *((int*)(BaseAddr+IER))取这个指针指向的值.
        // BaseAddr+IER是个地址.
    }
    return 0;
}

二:构造并访问多通道。

#include<opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include<iostream>
int main(){
    cv::Mat m=(cv::Mat_<cv::Vec3i>(3,2)<<cv::Vec3i(1,2,3),cv::Vec3i(2,4,6),cv::Vec3i(3,6,9),cv::Vec3i(4,8,12));
    for(int i=0;i<m.rows;++i){
        for(int j=0;j<m.cols;++j)
            std::cout<<m.at<cv::Vec3i>(i,j)<<",";  // row Index along the dimension 0,col Index along the dimension 1
        std::cout<<std::endl;
    }

    for(int i=0;i<m.rows;++i){
        const cv::Vec3i *ptr=m.ptr<cv::Vec3i>(i);  // i-based row index.
        for(int j=0;j<m.cols;++j)
            std::cout<<ptr[j]<<",";
        std::cout<<std::endl;
    }

    if(m.isContinuous()){
        const cv::Vec3i *ptr=m.ptr<cv::Vec3i>(0);  // 0-based row index.
        for(int i=0;i<m.rows*m.cols;++i)
            std::cout<<ptr[i]<<",";
        std::cout<<std::endl;
    }

    for(int i=0;i<m.rows;++i){  // 与下面相似
        for(int j=0;j<m.cols;++j){
            cv::Vec3i *ptr=(cv::Vec3i*)(m.data+m.step[0]*i+m.step[1]*j);
            std::cout<<*ptr<<",";
        }
        std::cout<<std::endl;
    }

    for(int i=0;i<m.rows;++i){
        for(int j=0;j<m.cols;++j)
            std::cout<<*((cv::Vec3i*)(m.data+m.step[0]*i+m.step[1]*j))<<",";
        std::cout<<std::endl;
        // 二维图像:step[0]代表每一行所占的字节数,而如果有间隔的话,这个间隔也作为字节数的一部分被计算在内.
        // 二维图像:step[1]代表每一个数值所占的字节数.
        // data是指向第一个数值的指针,类型为uchar.

        // (cv::Vec3i*)(m.data+m.step[0]*i+m.step[1]*j)把它强制变成Vec3i型指针.
        // *((cv::Vec3i*)(m.data+m.step[0]*i+m.step[1]*j))取这个指针指向的值.
        // m.data+m.step[0]*i+m.step[1]*j是个地址.

    }

    return 0;
}