Ask Your Question
2

How to access data from a cv::Mat

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

Simon gravatar image

updated 2015-08-22 04:18:48 -0500

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

1 answer

Sort by ยป oldest newest most voted
7

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

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 -0500 )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 -0500 )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 -0500 )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 -0500 )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 -0500 )edit

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

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

ok :( . thx for quick respond anyway.

YurDen gravatar imageYurDen ( 2017-11-13 05:26:17 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

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

Seen: 30,440 times

Last updated: Jun 29 '13