Opencv数组点问题

时间:2014-07-20 04:02:02

标签: c++ opencv

我正在开发一个基于使用Opencv进行模板匹配的项目。

我正在尝试定义一个从OpenCV函数中得到的点数组 minMaxLoc(),但我收到了这些错误:

temp.cpp:45:35: error: ‘minval’ was not declared in this scope
temp.cpp:45:44: error: ‘maxval’ was not declared in this scope
temp.cpp:45:53: error: ‘minloc’ was not declared in this scope
temp.cpp:45:62: error: ‘maxloc’ was not declared in this scope
temp.cpp:56:69: error: request for member ‘cols’ in ‘ref_temp’, which is of non-class   type ‘cv::Mat [7]’
temp.cpp:56:93: error: request for member ‘rows’ in ‘ref_temp’, which is of non-class type ‘cv::Mat [7]’
make: *** [temp.o] Error 1

这是我写的:

Point minloc_array[7],maxloc_array[7];
double minval, maxval;
Point minloc, maxloc; 
int temp_count; for(temp_count=0;temp_count<8;temp_count++)
{

    cv::matchTemplate(ref_image, ref_temp[temp_count], des1, CV_TM_CCORR_NORMED);
    cv::minMaxLoc(des1, &minval, &maxval, &minloc, &maxloc);
    while (true){
        double minval, maxval;
        Point minloc, maxloc;    
                if (maxval >= 0.9){
                    minloc_array[temp_count]= minloc;
                    maxloc_array[temp_count]= maxloc;
                    std::cout <<  "Template Matched " << std::endl ;
                    cv::rectangle(image,maxloc,cv::Point(maxloc.x +   ref_temp[temp_count].cols, maxloc.y+ref_temp[temp_count].rows),CV_RGB(0,255,0),2,8);
                    break;                  
                    }
                else
                    std::cout <<  "Template NOT Matched" << std::endl ;
                    break;
            }   




}

1 个答案:

答案 0 :(得分:1)

我可以看到很多问题。

  • minloc_array和maxloc_array的长度与for循环的限制不一致。
  • cv :: minMaxLoc()找到的minval等值将被声明隐藏
  • 您可能没有发布所有相关代码,但似乎ref_temp不是cv :: Mat
  • 的数组
  • 错误消息的行号与您发布的代码不一致,因此可能后者不准确,并且无法确定问题究竟是什么(下面的第42,43行确实存在于

以下是您在注释中添加注释的代码:

41: Point minloc_array[7],maxloc_array[7];  // *** arrays have length 7 (max index is 6)
42: double minval, maxval;
43: Point minloc, maxloc; 
44: int temp_count;   // *** newline added for readability
44: for(temp_count = 0; temp_count < 8; temp_count++)
    // *** should be: temp_count < 7
45: {
46:    // *** error message is on this line  - is posted code accurate?
47:    cv::matchTemplate(ref_image, ref_temp[temp_count], des1, CV_TM_CCORR_NORMED);
48:    cv::minMaxLoc(des1, &minval, &maxval, &minloc, &maxloc);
49:    while (true){
50:        double minval, maxval;   // *** hides minval, maxval in outer scope
51:        Point minloc, maxloc;    // ***  likewise
52:        if (maxval >= 0.9){
53:            minloc_array[temp_count]= minloc;
54:            maxloc_array[temp_count]= maxloc;
55:            std::cout <<  "Template Matched " << std::endl ;
56:            cv::rectangle(image,maxloc,cv::Point(maxloc.x +   ref_temp[temp_count].cols, maxloc.y+ref_temp[temp_count].rows),CV_RGB(0,255,0),2,8);
            // *** is ref_temp an array of Cv::Mat??
            break;                  
            }
        else
            std::cout <<  "Template NOT Matched" << std::endl ;
            break;
        }   

}