user2481422
user2481422

Reputation: 868

Accessing rgbMat value in openCV

I am trying to print out the values of rgbMat (in the below code) after mapping but getting some error. Can anybody tell me where I am going wrong? It's showing me the following error:

g++ `pkg-config --cflags --libs opencv` line1.cpp 
l1.cpp: In function ‘int main(int, char**)’:
l1.cpp:28:50: error: request for member ‘at’ in ‘rgbMat’, which is of non-class type ‘CvMat*’
l1.cpp:28:58: error: expected primary-expression before ‘>’ token
l1.cpp:28:62: error: name lookup of ‘x’ changed for ISO ‘for’ scoping
l1.cpp:28:62: note: (if you use ‘-fpermissive’ G++ will accept your code)

I guess I am going wrong in the following line:

  printf("matrix is %u: \n", rgbMat.at<uchar>(y,x));

My code is:

     IplImage* rgb[3];

     float L0[]={
         -1,-1,-1,-1,-1,
        0, 0, 0, 0, 0,
        2, 2, 2, 2, 2,
        0, 0, 0, 0, 0,
        -1,-1,-1,-1,-1 };

     CvMat*  rgbMat = cvCreateMat(5, 5, CV_32FC1);

     for (int y = 0; y < 5; y++)
     {
         for (int x = 0; x < 5; x++)
             cvmSet(rgbMat, y, x, L0[y*5 + x]);
           printf("matrix is %u: \n", rgbMat.at<uchar>(y,x));
     }

Upvotes: 0

Views: 404

Answers (1)

Zaphod
Zaphod

Reputation: 1927

To summarise the discussion:

It is advisable to port all use of OpenCV matrices to the new cv::Mat format. However, OpenCV provides functionality to convert between the old and new formats:

Partial yet very common cases of this user-allocated data case are conversions from CvMat and IplImage to Mat. For this purpose, there are special constructors taking pointers to CvMat or IplImage and the optional flag indicating whether to copy the data or not. Backward conversion from Mat to CvMat or IplImage is provided via cast operators Mat::operator CvMat() const and Mat::operator IplImage(). The operators do NOT copy the data.

In your code, first convert rgbMat to the cv::Mat format using cv::Mat rgbMatcpp(rgbMat). Now you should be able to access elements using at. For example, rgbMatcpp.at<float>(y, x). Next, be wary that variables x and y are limited in their scope to the for loops that they operate in. So use braces for the inner for loop and move the printf into the loop body. Also, in your attempt at using at, you have specified the type as uchar, which is erroneous because rgbMat is of type CV_32FC1. So change your use of at to rgbMatcpp.at<float>(y, x).

Upvotes: 2

Related Questions