views:

1537

answers:

4

Hi All,

I'm trying to learn how to use openCV's new c++ interface.

How do I access elements of a multi channel matrix. for example:

Mat myMat(size(3, 3), CV_32FC2);

for (int i = 0; i < 3; ++i)
{
    for (int j = 0; j < 3; ++j)
    {
        //myMat_at_(i,j) = (i,j);
    }
}

What is the easiest way to do this? Something like cvSet2D of the old interface
What is the most efficiant way? Similar to using direct pointers in the old interface.

Thank you

+2  A: 
OwnWaterloo
Thanks, but not quite what I was asking for. You are using CvMat - the old (version 1.1) c API.I want a similar method for the new (2.0a) c++ API using the Mat class.Is there something like CV_MAT_ELEM for the Mat class?
Yair
I'm sorry.I forget that OpenCV2.0 has been released. I downloaded the 2.0 source and found some ways to approach to your goal.See the updated code.
OwnWaterloo
Works great! Thanks :)
Yair
A: 

I'm trying to use the "new" 2.0 c++ version of OpenCV, but everything is else like in simple C version. I have some problem with changing the values in image.

The image is CV_8UC3.

for (int i=0; i<image.rows; i++)
{
    for (int j=0; j<image.cols; j++)
    {
        if (someArray[i][j] == 0)
        {
            image.at<Vec3i>(i,j)[0] = 0;
            image.at<Vec3i>(i,j)[1] = 0;
            image.at<Vec3i>(i,j)[2] = 0;
        }
    }
}

What i'm doing wrong???

Thank you!

Vic
A: 

Vic you must use Vec3b instead of Vec3i:

for (int i=0; i<image.rows; i++)
{
    for (int j=0; j<image.cols; j++)
    {
        if (someArray[i][j] == 0)
        {
            image.at<Vec3b>(i,j)[0] = 0;
            image.at<Vec3b>(i,j)[1] = 0;
            image.at<Vec3b>(i,j)[2] = 0;
        }
    }
}
Ivan
A: 

how to represent this into Mat structure? considering r = row, c = column ch = channel

(float)imgp->imageData[r*imgp->widthStep+(c*imgp->nChannels+ch)*sizeof(float)];

i've tried imgp.at(r,c)[ch] my image result is bad

rajesh