Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Conversion of Mat to 2d arrays in c++

Hi,

I have converted a Mat to 2d arrays in c++ using opencv. To be able to make sure that I am getting the same pixel values in both containers(Mat and array) I tried this:

cout << xma.at<float>(4,11) << endl;

cout << xma_[4][11] << endl;

xma is Mat and xma_ is the 2d array.

The result I get are very far from each other!

Am I trying to access the pixel values correctly?

BR Tina

Conversion of Mat to 2d arrays in c++

Hi,

I have converted a Mat to 2d arrays in c++ using opencv. To be able to make sure that I am getting the same pixel values in both containers(Mat and array) I tried this:

cout << xma.at<float>(4,11) << endl;

cout << xma_[4][11] << endl;

xma is Mat and xma_ is the 2d array.

The result I get are very far from each other!

Am I trying to access the pixel values correctly?

BR Tina

Here is the code:

Mat imrgb = imread("src.tif", CV_LOAD_IMAGE_COLOR);    
Mat bgr[3];   
split(imrgb, bgr);

double average[600][800];

for(int j=0; j<=599;j++ )
{
   for(int i=0; i<=799; i++)
   {
        average[j][i] =((((float)(bgr[0].at<uchar>(j,i)) + (float)(bgr[1].at<uchar>(j,i)) +(float)(bgr[2].at<uchar>(j,i))))/(3));
   } 
}    

for(int j=0; j<=599;j++ )
{
    for(int i=0; i<=799; i++)
    {
        average[j][i] = average[j][i] / 255;
    }  
 }       

src = Mat(600, 800, CV_64F, &average);

vector<float>gx;
vector<float>mult_vec;
for(int i=0; i<vec_first.size(); i++)
{
      mult_vec.push_back(vec1[i] * vec2[i]);     
}

for(int i=0; i<mult_vec.size(); i++)
{
    gx.push_back((exp(-mult_vec[i] / C); 
}

for(int i = 0; i < gx.size(); i++)
{
     gy.push_back(gx[i]);
}

Mat gyMat=Mat(5, 1, CV_32F);
memcpy(gyMat.data, gy.data(), gy.size()*sizeof(float));
Mat gykernel;
cv::transpose(gy, gykernel);

filter2D(src, xma, -1 , gykernel, Point( -1, -1 ), 0, BORDER_DEFAULT );

for(j=0; j<=599;j++ )
{
   for(i=0; i<=799; i++)
   {
      xma_[j][i] =(double)(xma.at<uchar>(j,i));

   }
}

Conversion of Mat to 2d arrays in c++

Hi,

I have converted a Mat to 2d arrays in c++ using opencv. To be able to make sure that I am getting the same pixel values in both containers(Mat and array) I tried this:

cout << xma.at<float>(4,11) << endl;

cout << xma_[4][11] << endl;

xma is Mat and xma_ is the 2d array.

The result I get are very far from each other!

Am I trying to access the pixel values correctly?

Here is the code:

Mat imrgb = imread("src.tif", CV_LOAD_IMAGE_COLOR);    
Mat bgr[3];   
split(imrgb, bgr);

double average[600][800];

for(int j=0; j<=599;j++ )
{
   for(int i=0; i<=799; i++)
   {
        average[j][i] =((((float)(bgr[0].at<uchar>(j,i)) + (float)(bgr[1].at<uchar>(j,i)) +(float)(bgr[2].at<uchar>(j,i))))/(3));
   } 
}    

for(int j=0; j<=599;j++ )
{
    for(int i=0; i<=799; i++)
    {
        average[j][i] = average[j][i] / 255;
    }  
 }       

src = Mat(600, 800, dst; 

imrgb.convertTo(dst, CV_64F, &average);
1.0/255);

vector<float>gx;
vector<float>mult_vec;
for(int i=0; i<vec_first.size(); i++)
{
      mult_vec.push_back(vec1[i] * vec2[i]);     
}

for(int i=0; i<mult_vec.size(); i++)
{
    gx.push_back((exp(-mult_vec[i] / C); 
}

for(int i = 0; i < gx.size(); i++)
{
     gy.push_back(gx[i]);
}

Mat gyMat=Mat(5, 1, CV_32F);
memcpy(gyMat.data, gy.data(), gy.size()*sizeof(float));
Mat gykernel;
cv::transpose(gy, gykernel);

filter2D(src, xma, -1 , gykernel, Point( -1, -1 ), 0, BORDER_DEFAULT );

for(j=0; j<=599;j++ )
{
   for(i=0; i<=799; i++)
   {
      xma_[j][i] =(double)(xma.at<uchar>(j,i));

   }
}