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;
}

 


免責聲明!

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



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