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 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));
}
}
what did you do there, exactly ? we probably need to look at that, too.
thanks for the update !
@berak sorry, I should have posted the code earlier. Please let me know if you see any problem in my code.
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.
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;
@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 ...
@sjhalayka Do you know any function in opencv for converting an RGB image to grayscale?
@tinaholm, ok, i was a bit negative and sloppy here, but:
... and so on. please bookmark docs, and refrain from writing silly for-loops.
berak answered your question. :)
@berak you are right. How I have implemented it is not efficient at all. I will update the code.