Conversion of Mat to 2d arrays in c++

asked 2018-10-07 06:48:21 -0600

tinahom gravatar image

updated 2018-10-21 05:34:48 -0600

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 dst; 

imrgb.convertTo(dst, CV_64F, 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));

   }
}
edit retag flag offensive close merge delete

Comments

  • DON'T work on a per pixel basis here. opencv is a high level matrix lib.
  • why do you assume, the type is float ? (guessing won't work)
  • row major vs. col major. are you aware of it ?
  • what are you trying to achieve, in general ? (avoid the obvious XY problem here)

I have converted a Mat to 2d arrays in c++ using opencv

what did you do there, exactly ? we probably need to look at that, too.

berak gravatar imageberak ( 2018-10-07 07:12:32 -0600 )edit

thanks for the update !

berak gravatar imageberak ( 2018-10-07 09:48:23 -0600 )edit

@berak sorry, I should have posted the code earlier. Please let me know if you see any problem in my code.

tinahom gravatar imagetinahom ( 2018-10-07 10:07:36 -0600 )edit

i see a problem with ALL the code you show. you're not using opencv, you're trying to defeat it.

please again, explain, what you're trying to achieve here. it likely can be done with 4 lines of code or so.

berak gravatar imageberak ( 2018-10-07 11:52:25 -0600 )edit
1

You really should not encode an image using a 2D array (a vector of vectors). Use indexing, on a 1D array, where index = y*x_res + x;

sjhalayka gravatar imagesjhalayka ( 2018-10-08 13:46:09 -0600 )edit

@berak in the first and second for-loop I am trying to get the average of the rgb image channels. And then by dividing it to 255 I am trying to get the grayscale image. I could not find any easy way to do this using Opencv, that is why I converted it to 2d arrays and ...

tinahom gravatar imagetinahom ( 2018-10-10 11:11:44 -0600 )edit

@sjhalayka Do you know any function in opencv for converting an RGB image to grayscale?

tinahom gravatar imagetinahom ( 2018-10-10 11:17:31 -0600 )edit
1

@tinaholm, ok, i was a bit negative and sloppy here, but:

  • cvtColor(src,dst,COLOR_BGR2GRAY);
  • meanStdDev(img, mean,stdev);
  • do you realize , you never use gyMat ?

... and so on. please bookmark docs, and refrain from writing silly for-loops.

berak gravatar imageberak ( 2018-10-10 11:25:28 -0600 )edit

berak answered your question. :)

sjhalayka gravatar imagesjhalayka ( 2018-10-10 11:35:05 -0600 )edit
1

@berak you are right. How I have implemented it is not efficient at all. I will update the code.

tinahom gravatar imagetinahom ( 2018-10-10 13:32:25 -0600 )edit

I tried cvtColor(src,dst,COLOR_BGR2GRAY); but the result is not what I expected. I thought this will make all pixel values between 0 and 1 but it does not.

tinahom gravatar imagetinahom ( 2018-10-21 04:41:41 -0600 )edit

grayscale means: single channel uchar in [0..255] range, not [0..1]

again, this is NOT matlab ...

if you need it in [0..1] do:

Mat dst; src.convertTo(dst, CV_64F, 1.0/255);
berak gravatar imageberak ( 2018-10-21 04:49:14 -0600 )edit

@berak src image is a RGB image so it has 3 channels. Just by converting it to double I will not get all pixel values in [0..1].

tinahom gravatar imagetinahom ( 2018-10-21 05:26:05 -0600 )edit