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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵