在OpenCV中试图将滑动条回调函数与变量进行封装,便于管理,尝试以下类似代码时出错,error C3867。
cv::createTrackbar ("Thresh", "HarrDemo", &this->thresh, this->maxThresh, this->HarrTrack);
//或者
cv::createTrackbar ("Thresh", "HarrDemo", &object.thresh, object.maxThresh, object.HarrTrack);
将 this->HarrTrack前面加上&会出现error C2276: “&”: 绑定成员函数表达式上的非法操作?
原因:类的成员函数默认带有一个this指针参数,那么它作为泛函的参数其实就不匹配了,因为泛函中的Func类型并没有this指针。
解决思路:用全局函数、友元函数或者类静态成员函数作为回调函数,利用了函数creatTrackbar的最后一个参数,函数原型:
@param onChange Pointer to the function to be called every time the slider changes position. This function should be prototyped as void Foo(int,void\*); , where the first parameter is the trackbar
position and the second parameter is the user data (see the next parameter). If the callback is the NULL pointer, no callbacks are called, but only value is updated.
@param userdata User data that is passed as is to the callback. It can be used to handle trackbar events without using global variables.
*/
CV_EXPORTS int createTrackbar(const String& trackbarname, const String& winname,
int* value, int count,
TrackbarCallback onChange = 0,
void* userdata = 0);
可以看到最后一个参数将作为实参传递给回调函数HarrTrack,注意HarrTrack的第二个参数是void*类型的。因此有以下方法。
/// 主函数
int main ()
{
Harr harr;
harr.mSrc = cv::imread ("../src/HarrTest.bmp", cv::IMREAD_COLOR);
cv::cvtColor (harr.mSrc, harr.mGray, cv::COLOR_BGR2GRAY);
//cv::imshow ("gray", harr.mGray);
cv::namedWindow ("HarrDemo", cv::WINDOW_NORMAL);
harr.moveBar ();
//cv::createTrackbar ("Thresh", "HarrDemo", &harr.thresh, harr.maxThresh, HarrTrack, (void*)&harr); // 这样调用也可以
//HarrTrack (0, nullptr);
cv::waitKey (0);
return 0;
}
Solution1:全局函数。将指向实例对象的指针强转传入回调函数。参考https://docs.opencv.org/4.1.1/d2/dcf/samples_2cpp_2falsecolor_8cpp-example.html#a29
void HarrTrack (int, void* data);
class Harr
{
public:
cv::Mat mSrc;
cv::Mat mGray;
int thresh = 135;
int maxThresh = 255;
public:
void moveBar ()
{
cv::createTrackbar ("Thresh", "HarrDemo", &this->thresh, this->maxThresh, HarrTrack, this); //把this当作HarrTrack的第二个参数
HarrTrack (0, this);
}
};
void HarrTrack (int, void* data) // 用全局函数作为回调函数,不能用成员函数作为回调函数
{
Harr* p = (Harr*)data;
int blockSize = 2;
int ksize = 3;
double k = 0.04;
cv::Mat dst;
//dst = cv::Mat::zeros (p->mGray.size (), CV_32FC1);
cv::cornerHarris (p->mGray, dst, blockSize, ksize, k, cv::BORDER_DEFAULT); // 输出 CV_32FC1
cv::normalize (dst, dst, 0, 255, cv::NORM_MINMAX, CV_8U);
//cv::convertScaleAbs (dst, dst);
//dst.convertTo (dst, CV_8U);
cv::imshow ("dst", dst);
cv::Mat srcTmp = p->mSrc.clone ();
for (int row = 0; row < dst.rows; row++)
{
const uchar* pCurrRow = dst.ptr<uchar>(row);
for (int col = 0; col < dst.cols; col++)
{
if (int(pCurrRow[col]) > p->thresh)
{
cv::circle (srcTmp, cv::Point (col, row), 1, cv::Scalar (0, 255, 0), 1, 8);
}
}
}
cv::imshow ("HarrDemo", srcTmp);
}
Solution2:友元函数。其实也是将指向实例对象的指针强转传入回调函数,只不过这种方法将算法进行了封装,对外提供了一个接口,比全局函数安全一点。
void HarrTrack (int, void* data);
class Harr
{
friend void HarrTrack (int, void* data);
public:
cv::Mat mSrc;
cv::Mat mGray;
int thresh = 135;
int maxThresh = 255;
private:
void findCorner (void* data)
{
Harr* p = (Harr*)data; // 其实这里的p就是this,因此实际上findCorner可以是无参,直接用this代替p.
int blockSize = 2;
int ksize = 3;
double k = 0.04;
cv::Mat dst;
//dst = cv::Mat::zeros (p->mGray.size (), CV_32FC1);
cv::cornerHarris (p->mGray, dst, blockSize, ksize, k, cv::BORDER_DEFAULT); // 输出 CV_32FC1
cv::normalize (dst, dst, 0, 255, cv::NORM_MINMAX, CV_8U);
//cv::convertScaleAbs (dst, dst);
//dst.convertTo (dst, CV_8U);
cv::imshow ("dst", dst);
cv::Mat resImg = p->mSrc.clone ();
for (int row = 0; row < dst.rows; row++)
{
const uchar* pCurrRow = dst.ptr<uchar> (row);
for (int col = 0; col < dst.cols; col++)
{
if (int (pCurrRow[col]) > this->thresh) // 如上所述,这里直接用this也可
{
cv::circle (resImg, cv::Point (col, row), 1, cv::Scalar (0, 255, 0), 1, 8);
}
}
}
cv::imshow ("HarrDemo", resImg);
}
public:
void moveBar ()
{
cv::createTrackbar ("Thresh", "HarrDemo", &this->thresh, this->maxThresh, HarrTrack, this); //把this当作友元函数HarrTrack的第二个参数
HarrTrack (0, this);
}
};
void HarrTrack (int, void* data) // 用友元函数替代成员函数,避免了成员函数的this指针问题
{
((Harr*)data)->findCorner (data);
}
Solution3:静态成员函数。这样就完全把方法和属性封装起来了。但是如果将其设为公有,并在主函数中用类名调用会出错。
void HarrTrack (int, void* data);
class Harr
{
public:
cv::Mat mSrc;
cv::Mat mGray;
int thresh = 135;
int maxThresh = 255;
private:
static void HarrTrack (int, void* data)
{
Harr* p = (Harr*)data;
int blockSize = 2;
int ksize = 3;
double k = 0.04;
cv::Mat dst;
//dst = cv::Mat::zeros (p->mGray.size (), CV_32FC1);
cv::cornerHarris (p->mGray, dst, blockSize, ksize, k, cv::BORDER_DEFAULT); // 输出 CV_32FC1
cv::normalize (dst, dst, 0, 255, cv::NORM_MINMAX, CV_8U);
//cv::convertScaleAbs (dst, dst);
//dst.convertTo (dst, CV_8U);
cv::imshow ("dst", dst);
cv::Mat resImg = p->mSrc.clone ();
for (int row = 0; row < dst.rows; row++)
{
const uchar* pCurrRow = dst.ptr<uchar> (row);
for (int col = 0; col < dst.cols; col++)
{
if (int (pCurrRow[col]) > p->thresh)
{
cv::circle (resImg, cv::Point (col, row), 1, cv::Scalar (0, 255, 0), 1, 8);
}
}
}
cv::imshow ("HarrDemo", resImg);
}
public:
void moveBar ()
{
cv::createTrackbar ("Thresh", "HarrDemo", &this->thresh, this->maxThresh, HarrTrack, this); //把this当作静态成员函数HarrTrack的第二个参数
HarrTrack (0, this);
}
};