C++ OOP opencv轨迹栏

C++ OOP opencv轨迹栏,c++,oop,opencv,C++,Oop,Opencv,我正在尝试创建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->

我正在尝试创建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);
}
我明白了

错误5错误LNK2001:未解析的外部符号公共:静态类cv::Mat轮廓::src_gray?src_gray@Contours@@2VMat@cv@@A D:\Licensta\Hand sign recognition\Algoritmi.obj手语识别

错误6错误LNK2001:未解析的外部符号公共:静态整数轮廓::阈值?thresh@Contours@@2HA D:\Licenta\Hand sign recognition\Algoritmi.obj手语识别


想知道为什么吗?

可能会使用不同的方法解决“静态回调”问题:

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 );
   ...

这些是链接器错误。您的代码已编译,可能是项目设置问题。visual studio的一个好主意是,您可以搜索以找出它们的错误代码(如LNK2001)的含义。是否包含imgproc和highui库?请查找,如何在类中定义静态变量您只声明此操作非常有效,只是不理解&cnt参数,它从哪里来,为什么我需要把它放在那里。将轮廓对象的地址输入createTrackbar,然后在回调函数中传回。
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 );
   ...