OpenCV中Mat與二維數組之間的轉換


---恢復內容開始---

在OpenCV中將Mat(二維)與二維數組相對應,即將Mat中的每個像素值賦給一個二維數組。

全部代碼如下:

#include <iostream>
#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> //包含imread, imshow等標識符
#include "opencv2/imgproc/imgproc.hpp" //包含cvtColor等

using namespace std;
using namespace cv;

//測試Mat
void main(){
    //讀入圖像
    Mat mat = imread("trabeculae.jpg");
    //判斷讀入圖片是否有誤
    if(mat.empty())  
    {   
        if (!mat.data) { 
            printf("Oh,no,讀取圖片文件錯誤~! \n"); 
        } 
        cout << "error" << endl;
    }  

    // 進行圖像灰度化操作
    cvtColor(mat, mat, CV_BGR2GRAY);
    //獲取 mat 的行和列
    int row = mat.rows;
    int col = mat.cols;
    cout << "  mat.rows : " << mat.rows << endl;
    cout << "  mat.cols : " << mat.cols << endl;

   //動態創建二維數組,row行col列
    int **La = new int *[row];
    for (int i = 0; i < row; i ++){
        La[i] = new int[col];
    }
    // 循環二維數組和mat,並將mat對應值賦給二維數組對應值,
    for (int i = 0; i < row; i ++){
        for (int j = 0; j < col; j ++){
            La[i][j] = mat.at<uchar>(i, j);
        }
    }
    // 釋放分配空間
    for (int i = 0; i < row; i ++){
        delete []La[i];
    }
    delete [] La;

    cout << endl;
    waitKey(0);
    system("pause");
}

 分析:

1. 讀入一幅圖像

    //讀入圖像
    Mat mat = imread("trabeculae.jpg");
    //判斷讀入圖片是否有誤
    if(mat.empty())  
    {   
        if (!mat.data) { 
            printf("Oh,no,讀取圖片文件錯誤~! \n"); 
        } 
        cout << "error" << endl;
    }  

 

 2. 對圖像進行灰度化操作,將Mat轉為二維。

    // 進行圖像灰度化操作
    cvtColor(mat, mat, CV_BGR2GRAY);

 

3. Mat有rows和cols屬性,rows表示對應矩陣行數,cols表示對應矩陣列數:

    //保存mat的行和列
    int row = mat.rows;
    int col = mat.cols;

 

4. Mat還具有size () 方法,該方法可以得到width寬度和height高度:

    Size s = mat.size();
    int width = s.width;
    int height = s.height;

 

5. rows,cols,width,height 之間的關系:

    cout << "  s.width : " << s.width << endl;
    cout << "  s.height : " << s.height << endl;
cout
<< " mat.rows : " << mat.rows << endl; cout << " mat.cols : " << mat.cols << endl;

 6. 打印結果:

7. 結論:

由第5步和第6步可得:

mat.size().width = mat.cols;
mat.size().height = mat.rows;

 

8. 動態創建二維數組:

例:創建一個4行3列的二維數組,如下:

{ { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } }

即:{ { 0, 0, 0 },

         { 0, 0, 0 },

         { 0, 0, 0 },

        { 0, 0, 0 }

     }

    // 創建一個二維數組中包含4個一維數組,即先定義4行
    int **array = new int *[4];
    // 循環二維數組,並將二維數組中的每個元素創建為包含3個元素的一維數組
    // 先分配多少行,再每行分配多少列
    for (int i = 0; i < 4; i ++){
        array[i] = new int[3];
    }
    // 循環遍歷二維數組,行作為外循環,列作為內循環,一行一行地遍歷
    for (int i = 0; i < 4; i ++){
        for (int j = 0; j < 3; j ++){
            array[i][j] = 0;
            cout << " " << array[i][j] ;
        }
        cout << endl;
    }
    // 使用完請求分配的數值需要釋放分配空間(內存)
    // 釋放分配空間,一行一行的刪除
    for (int i = 0; i < 4; i ++){
        delete []array[i];
    }
    delete [] array;

 

 結果:

 9. 使用Mat圖像的寬度和高度進行動態創建二維數組,Height(row)代表具有多少行,width(col)代表具有多少列。

    // 創建一個二維數組,height(row)行width(col)列
    int **La = new int *[height];
    for (int i = 0; i < height; i ++){
        La[i] = new int[width];
    }

    // 循環將Mat中對應的值賦給La二維數組
    for (int i = 0; i < row; i ++){
        for (int j = 0; j < col; j ++){
            La[i][j] = mat.at<uchar>(i, j);
            //cout << " " << La[i][j] ;
        }
        //cout << endl;
    }

    // 釋放分配空間
    for (int i = 0; i < height; i ++){
        delete []La[i];
    }
    delete [] La;

 

10. 創建一個和二維數組行列大小的Mat:

當知道二維數組的大小,需要將二維數組中的值賦給同樣大小的Mat,使用Mat顯示。

    //創建一個Mat變量  height(row)行width(col)列
    Mat temp = Mat(height, width, CV_8U, Scalar::all(0));
    // 循環賦值
    for (int i = 0; i < height; i ++){
        for (int j = 0; j < width; j ++){
            mat.at<uchar>(i, j) = La[i][j];
        }
    }

 

 

 

 

 

 

 

 

 

 

---恢復內容結束---


免責聲明!

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



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