opencv C ++ structure and access single-channel and multi-channel Mat.

A: structure and access to a single channel.

  
  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) {// following similar
        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;
        @ Dimensional image: step [0], each row represents the number of bytes occupied, and if spaced, then, as part of this interval is the number of bytes counted.
        @ Dimensional image: step [1] represents the number of bytes occupied by each value.
        // data is a pointer to a first value, type uchar.
 
 
        // (int *) (BaseAddr + IER) It is forced into the int type pointer.
        // * ((int *) (BaseAddr + IER)) takes the value of the pointer.
        // BaseAddr + IER is an address.
 
 
    }
    return 0;
}
  
  

II: structure and access to multi-channel.

#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))<<",";
        :: COUT STD << STD :: endl;
         // dimensional image: step [0], each row represents the number of bytes occupied, and if spaced, then, as part of this interval is the number of bytes counted .
         @ dimensional image: step [1] representing each value of the number of bytes occupied by a.
         // Data is a pointer to a first value, type UCHAR. 

        // (CV :: Vec3i *) (m. data + m.step [0] * i + m.step [1] * j) to force it into Vec3i type pointer.
         // * ((CV :: Vec3i *) (+ m.data m.step [0 ] * i + m.step [1] * j)) takes the value of the pointer.
         // m.data + m.step [0] * I + m.step [. 1] J * is an address. 

    } 

    return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/ligei/p/11487759.html