How to access rows and columns of Vector <Mat> Images on Open cv for Android?

I was looking at some open cv code using Android. Each of them shows me how I access the rows and columns of the Mat vector of the image? Its declared, and columns and rows are available as below, but I have errors like and.

vector<Mat> images;//Vector of Mat image declared ... int im_width = images[0].cols;//its trying to access column of images matrix int im_height = images[0].rows;//trying to access rows of Mat images 

If this is the wrong way to access the columns and rows of a vector image, then what is it? When he says images [0], he tries to access index 0 of the image vector Mat. I am not sure if this is correct or not.

+4
source share
5 answers

The members of rows and cols tell you the number of rows and columns in the matrix. To access them, you can use at :

 // let assume your image is CV_8U, so that it is accessed with unsigned char cv::Mat m = ...; for(int r = 0; r < m.rows; ++r) { for(int c = 0; c < m.cols; ++c) { char byte = m.at<unsigned char>(r, c); ... } } 

You can also save some calls to at and access the data by pointer for each row:

 // let assume your image is CV_8U, so that it is accessed with unsigned char cv::Mat m = ...; for(int r = 0; r < m.rows; ++r) { const unsigned char *p = m.ptr<unsigned char>(); for(int c = 0; c < m.cols; ++c, ++p) { char byte = *p; ... } } 

If the matrix is ​​continuous in memory, you can get a pointer only once:

 // let assume your image is CV_8U, so that it is accessed with unsigned char cv::Mat m = ...; assert(m.isContinuous()); const unsigned char *p = m.ptr<unsigned char>(); for(int r = 0; r < m.rows; ++r) { for(int c = 0; c < m.cols; ++c, ++p) { char byte = *p; ... } } 
+2
source
 Mat m; m.row(i); m.col(i); 

ref: http://opencv.willowgarage.com/wiki/Welcome?action=AttachFile&do=get&target=opencv_cheatsheet.pdf

edit: the following works and produces the expected result. perhaps you should describe your mistakes?

 #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include <iostream> using namespace cv; using namespace std; int main( int argc, char** argv ) { vector<Mat> vectors; Mat img1 = imread( "C:/test/1.jpg"); Mat img2 = imread( "C:/test/2.jpg"); vectors.push_back(img1); vectors.push_back(img2); cout<<vectors[0].rows<<endl; cout<<vectors[0].cols<<endl; cin.ignore(1); return 0; } 
+1
source

All the above sentence does not help, so I decided to create a temporary variable with the Mat data type and copy the 0th index of the vector to Mat temp and get access to the rows and columns of temp like temp.cols and temp.rows. Thanks for trying to help me.

+1
source

Mat already an image vector. Therefore, you do not need to define it as a vector. With images.cols you get only the number of columns or rows. If you want to access data in specific columns and rows, you should do it like this:

 int mat_value = image.get(i, j)[0]; 

Where i and j are the indices of your column and row.

Edit: Alternatively, you can see how the number of columns and rows of images on

 int cols = image.size().width; int rows = image.size().height; 

PS I think this is well explained in the OpenCV documentation.

0
source

Since Mat is already an image vector, you do not need to define the vector again. All you have to do is

 Mat images; for(int j =0; j <images.rows; j++) { for(int k =0; k <images.cols; k++) { cout << image.at<type>(j,k)<< endl; } } 

You can use an array if you want to keep these values.

0
source

Source: https://habr.com/ru/post/1485271/


All Articles