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

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

在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 main5(){
    //讀入圖像
    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");
}

 分析:ui

1. 讀入一幅圖像spa

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

 

 2. 對圖像進行灰度化操做,將Mat轉爲二維。3d

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

 

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

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

 

4. Mat還具備size () 方法,該方法能夠獲得width寬度和height高度:blog

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

 

 

 

 

 

 

 

 

 

 

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

相關文章
相關標籤/搜索