Ask Your Question

Simon's profile - activity

2019-09-15 10:06:16 -0600 received badge  Nice Question (source)
2016-03-08 10:56:15 -0600 received badge  Famous Question (source)
2015-06-23 10:58:06 -0600 received badge  Notable Question (source)
2014-11-14 04:35:47 -0600 received badge  Popular Question (source)
2013-12-07 13:59:30 -0600 received badge  Student (source)
2013-06-29 10:38:09 -0600 asked a question How to access data from a cv::Mat converted from mxArray

I have a function which converts an mxArray mat to cv::Mat. The body of the method is listed bellow :

   cv::Mat Utils::mxArray2cvMat( mxArray* p_)
   {
   // Create cv::Mat object.
   mwSize ndims_= mxGetNumberOfDimensions(p_);
   const mwSize* dims=mxGetDimensions(p_);
   std::vector<int> d(dims, dims+ndims_);
   int ndims = (d.size()>2) ? d.size()-1 : d.size();
   int nchannels = (d.size()>2) ? *(d.end()-1) : 1;
   //depth = (depth==CV_USRTYPE1) ? DepthOf[classID()] : depth;
   int depth=CV_64F;
   std::swap(d[0], d[1]);
   cv::Mat mat(ndims, &d[0], CV_MAKETYPE(depth, nchannels));
   // Copy each channel.
   std::vector<cv::Mat> channels(nchannels);
   std::vector<mwSize> si(d.size(), 0); // subscript index
   int type = CV_MAKETYPE(depth, 1); // Source type
   for (int i = 0; i<nchannels; ++i)
   {
   si[d.size()-1] = i;
   void *pd = reinterpret_cast<void*>(
           reinterpret_cast<size_t>(mxGetData(p_))+
           mxGetElementSize(p_)*mxCalcSingleSubscript(p_, si.size(), &si[0]));
   cv::Mat m(ndims, &d[0], type, pd);
   // Read from mxArray through m
   m.convertTo(channels[i], CV_MAKETYPE(depth, 1));
   }
   cv::merge(channels, mat);
   return  mat;
   }

Now I want to access the values within the returned array. I have an array whose dimensions are 300 x 500 x 32. How can I access the values within this array?

I have the following code:

   cv::Mat f=Utils::mxArray2cvMat(features);
   cout<<f.at<double>(1,1,1);

However, it crashes giving some assertion failed error at runtime. I have also run the following command in order to identify the data type within the f variable ( cout<<f.type() ) and it returned the value 254. What type is that? And how can I access the values within this type of Mat?

Can you please give me any help regarding this matter?

2013-06-29 10:29:32 -0600 received badge  Scholar (source)
2013-06-29 06:07:45 -0600 commented answer How to access data from a cv::Mat

@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?

2013-06-29 04:56:03 -0600 received badge  Critic (source)
2013-06-29 04:48:23 -0600 commented answer How to access data from a cv::Mat

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.

2013-06-29 04:02:41 -0600 received badge  Editor (source)
2013-06-29 03:58:34 -0600 asked a question How to access data from a cv::Mat

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