Mr.Hazyzhao 2021-09-07 13:52 采纳率: 90.9%
浏览 128
已结题

如何给一个回调函数传数据?

在openCV的canny检测中,我想用一个类完成这个事,但是传参的时候出现若干问题


class MyCanny{
private:
    Mat src, src_gray;
    Mat dst, detected_edges; // input and output matrix
    int lowThreshold = 0;
    const int max_lowThreshold = 100;
    const int ratio = 3;
    const int kernel_size = 3;
    const char* window_name = "Edge Map"; // some parameters

public:
    explicit MyCanny(const Mat &img); // 构造函数,用于对类的对象赋值,由于数据是private的,只能通过此种方式赋值
    MyCanny(); // constructor
    Mat get_dst(); // 可以不需要通过static静态就获得dst矩阵
    void canny_process(); // 用于进行主要的CannyEdge处理过程
    void canny_threshold(int pos, void* userdata);
}

然后是函数成员


void MyCanny::canny_process() {

    createTrackbar("Min Threshold:", window_name, &lowThreshold, max_lowThreshold, canny_threshold);
    //(Reference to non-static function member must be called, so I have to make this function static, 
    //but static function can't access data from class)
    canny_threshold(0, nullptr);
    waitKey(0);
}

void MyCanny::canny_threshold(int pos, void *userdata) {
    //(how to send data from class to this function if I made this function static)
    blur(src_gray, detected_edges, Size(3, 3));
    Canny(detected_edges, detected_edges, lowThreshold, lowThreshold * ratio, kernel_size);
    dst = Scalar::all(0);
    src.copyTo(dst, detected_edges);
    imshow(window_name, dst);
}
  • 如果canny_threshold这个函数不是静态static的,createTrackbar的最后一个参数就不能引用
  • 如果它静态了,就没法获得类的数据了

所以我该怎么给他传参?,肯定是通过这个void* userdata了

  • 写回答

5条回答 默认 最新

  • baidu_30233079 2021-09-07 14:22
    关注

    canny_threshold的第二个参数,把类的this指针传过去就行了。在该函数内能过类指针 访问

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 9月9日
  • 已采纳回答 9月7日
  • 创建了问题 9月7日

悬赏问题

  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒