---restore content starts---
In OpenCV, the mat (two-dimensional) corresponds to a two-dimensional array, assigning each pixel value in the mat to a two-dimensional array.
The full code is as follows:
#include <iostream>#include<opencv2/core/core.hpp>#include<opencv2/highgui/highgui.hpp>//contains identifiers such as Imread, imshow, etc.#include"opencv2/imgproc/imgproc.hpp" //includes Cvtcolor, etc.using namespacestd;using namespaceCV;//Test MatvoidMain5 () {//Read in ImageMat mat = Imread ("trabeculae.jpg"); //determine if a picture was read in error if(Mat.empty ()) {if(!mat.data) {printf ("Oh,no, read the picture file Error ~! \ n"); } cout<<"Error"<<Endl; } //perform grayscale operation of the imageCvtcolor (Mat, Mat, cv_bgr2gray); //Getrow and column of Mat introw =mat.rows; intCol =Mat.cols; cout<<"mat.rows:"<< mat.rows <<Endl; cout<<"Mat.cols:"<< Mat.cols <<Endl; //Create a two-dimensional array dynamically, row row col column int**la =New int*[row]; for(inti =0; i < row; i + +) {La[i]=New int[col]; }//Loop two-dimensional array and mat, and assign the mat corresponding value to the corresponding value of the two-dimensional array, for(inti =0; i < row; i + +){ for(intj =0; J < Col; J + +) {La[i][j]= mat.at<uchar>(i, j); }}//Free allocated space for(inti =0; i < row; i + +){ Delete[]la[i]; } Delete[] La;cout<<Endl; Waitkey (0); System ("Pause");}
Analysis:
1. Read in an image
// read in image Mat mat = Imread ("trabeculae.jpg"); // determine if a picture was read in error if (Mat.empty ()) { if (! Mat.data) { printf ("oh,no, read picture file Error ~! \ n") ; } " Error " << Endl; }
2. Grayscale operation of the image to convert the mat to two-dimensional.
// Image grayscale Operation Cvtcolor (Mat, Mat, Cv_bgr2gray);
3. MAT has rows and cols properties, rows represents the number of corresponding matrix rows, and cols represents the number of corresponding matrix columns:
// Save the rows and columns of the mat int row = mat.rows; int col = mat.cols;
4. MAT also has a size () method, which can be used to obtain width and Height heights:
Size s = mat.size (); int width = s.width; int height = s.height;
5. The relationship between Rows,cols,width,height:
" << s.width << endl; " "<< s.height << endl;
" << mat.rows << endl; " "<< mat.cols << Endl;
6. Printing results:
7. Conclusion:
The 5th and 6th steps are available:
Mat.size (). width == mat.rows;
8. Create a two-dimensional array dynamically:
Example: Create a two-dimensional array of 4 rows and 3 columns, as follows:
{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}}
That is: {{0, 0, 0},
{0, 0, 0},
{0, 0, 0},
{0, 0, 0}
}
//Create a two-dimensional array containing 4 one-dimensional arrays, i.e. define 4 rows firstint**array =New int*[4]; //loops a two-dimensional array and creates each element in a two-dimensional array as a one-dimensional array containing 3 elements //How many rows are allocated first, and how many columns are allocated per row for(inti =0; I <4; i + +) {Array[i]=New int[3]; } //Iterate through two-dimensional arrays, rows as outer loops, columns as inner loops, one line through for(inti =0; I <4; i + +){ for(intj =0; J <3; J + +) {Array[i][j]=0; cout<<" "<<Array[i][j]; } cout<<Endl; } //The allocated space (memory) needs to be freed when the requested value is used//Free allocated space, one row of deletes for(inti =0; I <4; i + +){ Delete[]array[i]; } Delete[] array;
Results:
9. Dynamically create a two-dimensional array with the width and height of the mat image, and the height (row) represents the number of rows, and the width (col) represents the number of columns.
//Create a two-dimensional array, height (row) row width (col) columnint**la =New int*[height]; for(inti =0; i < height; i + +) {La[i]=New int[Width]; } //loop assigns the corresponding value in the mat to the LA two-dimensional array for(inti =0; i < row; i + +){ for(intj =0; J < Col; J + +) {La[i][j]= mat.at<uchar>(i, j); //cout << "<< la[i][j"; } //cout << Endl; } //Releasing allocated Space for(inti =0; i < height; i + +){ Delete[]la[i]; } Delete[] La;
10. Create a two-dimensional array of the row and column sizes of the mat:
When you know the size of a two-dimensional array, you need to assign the values in the two-dimensional array to a mat of the same size, using the mat display.
// Create a mat variable height (row) row width (col) column Mat temp = Mat (height, width, cv_8u, Scalar::all ( 0 )); // Cyclic Assignment for (int0; i < height; i + +) {for ( int 0; J < width; J + +) { mat.at<uchar> (i, j) = la[i][j]; } }
---restore content ends---
Conversion between mat and two-dimensional array in OpenCV