【问题标题】:OOP opencv trackbarOOP opencv 轨迹栏
【发布时间】:2014-12-08 21:23:24
【问题描述】:

我正在尝试使this OOP,所以我创建了一个实现轨迹栏回调函数的类。

这是我的课

class Contours
{
public:
    static Mat src_gray;
    static int thresh;
Contours(Mat, int);
~Contours(void);
static void callback(int, void* );

private:
};

Contours::Contours(Mat src_gray,int thresh){
this->src_gray=src_gray;
this->thresh=thresh;

}
Contours::~Contours(void){}

void Contours::callback(int, void*)
{int largest_area=0;
int largest_contour_index=0;
Mat canny_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;

/// Detect edges using canny
Canny( src_gray, canny_output, thresh, thresh*2, 3 );
/// Find contours
findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

/// Draw contours
Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
cout<<contours.size()<<endl;
for( int i = 0; i< contours.size(); i++ )
{
    double a=contourArea( contours[i],false);  //  Find the area of contour
    if(a>largest_area){
        largest_area=a;
        largest_contour_index=i;  }              //Store the index of largest contour

    //Scalar color = Scalar(255,255,255 );
    //drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() );

}
cout<<"cnt maxim: "<<largest_contour_index<<endl;

    Scalar color = Scalar(255,255,255 );
    drawContours( drawing, contours, largest_contour_index, color, 2, 8, hierarchy, 0, Point() );

/// Show in a window
namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
imshow( "Contours", drawing );
//imwrite("sada.png",drawing);
}

并调用函数

createTrackbar( " Canny thresh:", "Source", &thresh, max_thresh, &Contours::callback );

我明白了

错误 5 错误 LNK2001: 无法解析的外部符号“public: static class cv::Mat Contours::src_gray” (?src_gray@Contours@@2VMat@cv@@A) D:\Licenta\Hand sign recognition\Algoritmi. obj手势识别

Error 6 error LNK2001: unresolved external symbol "public: static int Contours::thresh" (?thresh@Contours@@2HA) D:\Licenta\Hand sign recognition\Algoritmi.obj Hand sign recognition

有什么想法吗?

【问题讨论】:

  • 这些是链接器错误。您的代码可以编译,这可能是项目设置问题。使用 Visual Studio 的一个不错的想法是,您可以搜索以了解其错误代码的含义(例如 LNK2001)。
  • 你是否包含了 imgproc 和 highui 库?
  • 请查阅,如何define类中的静态变量(你的只是declared

标签: c++ oop opencv


【解决方案1】:

也许对“静态回调”问题使用不同的方法:

class Contours
{
public:
    Mat src_gray; // non-static
    int thresh;
    Contours(Mat, int);
    void callback(int, void* ); // non-static
};

void callback(int value, void*ptr)  // static global
{
   Contours* cnt = (Contours*)ptr;  // cast 'this'
   cnt->callback(value,0);          // call non-static member
}


int main()
{
    Mat gray = ...
    Contours cnt(gray,17);
    // now pass the address of cnt as additional void* Ptr:
    createTrackbar( " Canny thresh:", "Source", &thresh, max_thresh, callback, &cnt );
   ...

【讨论】:

  • 这很好用,只是不明白 &cnt 参数,它是从哪里来的,为什么我需要把它放在那里。
  • 将轮廓对象的地址输入 createTrackbar(),然后在回调函数中传回。
猜你喜欢
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多