Ask Your Question
4

How to access data from a cv::Mat

asked 2013-06-29 03:58:34 -0600

Simon gravatar image

updated 2020-11-17 15:51:49 -0600

I am having an Mat structure which size is 382 x 510 x 32. How can I access the data from it?

From what I have read on the internet it should be something like this :
unsigned char *input = (unsigned char*)(img.data);

Anyhow, my code is something like this :

const int mySizes[3]={382,510,32};

cv::Mat f(3,mySizes,CV_64F);

Can you help me to go through the position of my 3D array?

EDIT 1:

Here is a link to the same question that I asked on stackoverflow.com : http://stackoverflow.com/questions/17305231/how-to-access-data-from-cvmat

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
8

answered 2013-06-29 04:18:37 -0600

berak gravatar image

you can use the Mat::at<type>(i,j,k) operator for that:

const int mySizes[3]={3,5,5};

cv::Mat f = Mat::zeros(3,mySizes,CV_64F);
f.at<double>(1,2,3) = 17;

for ( int i=0;i<mySizes[0];i++) {
    for ( int j=0;j<mySizes[1];j++) {
        for ( int k=0;k<mySizes[2];k++) {
            cerr << f.at<double>(i,j,k) << " ";
        }
        cerr << endl;
    }
    cerr << endl;
}
cerr << endl;

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

0 0 0 0 0
0 0 0 0 0
0 0 0 17 0
0 0 0 0 0
0 0 0 0 0

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
edit flag offensive delete link more

Comments

I have encountered another problem which I cannot comprehend. I have the following lines of code: cv::Mat image=imread("D:\pic.jpg"); cout<<image.at<double>(1,1,1)<<endl;. When It reaches the cout instruction it crashes and I don't know why.

Simon gravatar imageSimon ( 2013-06-29 04:48:23 -0600 )edit

@Simon, I don't think imread reads in your images as a double matrix. It probably loads it as CV_8UC3, you could try image.at<uint8_t>(1, 1, 1) instead. Better yet, print out image.type() and check what it loads it as.

Hansg91 gravatar imageHansg91 ( 2013-06-29 05:14:50 -0600 )edit

@Hansg91, I have printed image.type() and it returns 16. How do I interpret this? What type does it represent and where can I find an interpretation for it?

Simon gravatar imageSimon ( 2013-06-29 06:07:45 -0600 )edit
1
  1. imread returns 2d mats. it can only read integral types, not floats or doubles
  2. type() == 16 means CV_8UC3, 3 uchar channels. that's not the same as a3d matrix

    you'd access that like Vec3b pixel = mat.at<Vec3b>(i,j); pixel[0] is blue

    if type() == 0, or CV_8U it's a 1 channel (grayscale) img, you'd access that like uchar pixel = mat.at<uchar>(i,j);

  3. opencv\modules\core\include\opencv2\core\cvdef.h has the definitions

berak gravatar imageberak ( 2013-06-29 08:09:38 -0600 )edit

Can you tell me please, how to do the same thing in C# with emgu?

YurDen gravatar imageYurDen ( 2017-11-13 04:25:30 -0600 )edit

@YurDen, no, we can't. emgu is entirely off-topic here.

berak gravatar imageberak ( 2017-11-13 04:33:25 -0600 )edit

ok :( . thx for quick respond anyway.

YurDen gravatar imageYurDen ( 2017-11-13 05:26:17 -0600 )edit
1

answered 2019-09-15 09:48:02 -0600

Bheemesh gravatar image

updated 2019-09-15 10:04:34 -0600

I have tried the above solution. But it didn't work out for me. So i came up with an alternative. The solution is mentioned over down. Hope you find it useful :). Happy Coding!!!

Mat B(4,2,CV_8UC1,Scalar::all(1) );
 cout<<B<<endl<<B.row(0).col(0);

O/p :

[  1,   1;
   1,   1;
   1,   1;
   1,   1]
[  1]
edit flag offensive delete link more

Comments

even if you don't answer the real question I appreciate your effort. +1 for your first post.

sturkmen gravatar imagesturkmen ( 2019-09-15 10:11:40 -0600 )edit

Question Tools

Stats

Asked: 2013-06-29 03:58:34 -0600

Seen: 59,774 times

Last updated: Sep 15 '19