2013-07-04 50 views
0

我想在映射後打印出rgbMat的值(在下面的代碼中),但得到一些錯誤。有人可以告訴我我哪裏出錯了嗎?它顯示我以下錯誤:在openCV中訪問rgbMat值

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) 

我想我錯了以下行:

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

我的代碼是:

 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)); 
    } 
+0

你正在調用cv :: mat :: at()來自C-api的結構,它不起作用。使用cv :: Mat for C++ api函數。 – hetepeperfan

+0

要從CvMat轉換爲cv :: Mat,構建'cv :: Mat rgbMatcpp(rgbMat)'。然後你可以使用'rgbMatcpp.at (x,y)'。 – Zaphod

+0

@Zaphod我做了CvMat cv :: Mat轉換,然後pritned爲printf(「matrix is%u:\ n」,rgbMatcpp.at (x,y));但它仍然給我以下錯誤:line1.cpp:在函數'int main(int,char **)'中: l1.cpp:28:63:warning:'x'的名稱查找已更改爲ISO'for'範圍 l1.cpp:26:23:警告:在'x – user2481422

回答

2

總結討論:

建議將所有OpenCV矩陣的使用移植到新的cv :: Mat格式。然而,OpenCV提供的功能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.

在你的代碼,先轉換rgbMat使用cv::Mat rgbMatcpp(rgbMat)cv::Mat格式。現在您應該可以使用at訪問元素。例如,rgbMatcpp.at<float>(y, x)。接下來,請注意,變量xy在它們的範圍內受到它們所在的for循環的限制。因此,使用大括號表示內部for循環,並將printf移動到循環體中。此外,在您嘗試使用at時,您已將類型指定爲uchar,這是錯誤的,因爲rgbMat的類型爲CV_32FC1。因此請將您使用的at更改爲rgbMatcpp.at<float>(y, x)