- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图避免在 openCV 项目中使用全局变量(毫无疑问,我会让我的主管教我为什么它们不好 rah rah rah :) - 但目前它们似乎是唯一的方法我可以从鼠标和轨迹栏回调函数中获取信息。
使用鼠标示例 - 目前我有全局变量:
vector<Point2d> vectorOfPoints;
int clickCount = 0;
目前我在 main 中有这一行:
setMouseCallback("test",onMouse, NULL);
然后上面的主要内容:
void onMouse(int event, int x, int y, int f, void* ){
if(event == CV_EVENT_LBUTTONDOWN){
vectorOfPoints.push_back(Point(x,y));
clickCount++;
}
}
它正在工作,但是在不使用全局变量的情况下,在回调函数中获得对 vectorOfPoints 和 clickCount 的读/写访问权限的语法是什么?
我在网上发现了这个问题几次,但答案对我来说不清楚或无法解决。评论中有关于如何操作的提示,但到目前为止我无法正确解释行话。
我希望有一些像我用来将变量作为对方法的引用传递的语法一样简单的东西......
void referenceExampleMethod(vector<Point2d>& referenceExample){
//do something with referenceExample...
}
...越简单越好
我不敢问(行话过多!)但也许它 100% 相关 - 什么是 void* ??
感谢任何帮助
最佳答案
我同意 @jschultz410 关于指向内存中某个位置的指针的回答的第一部分。但是,我不同意在野外使用原始指针。
你应该定义你自己的数据类型,保存你所有的数据,它可以是struct
或class
,或std::pair
,或 std::tuple
,无论如何,选择权在您。
然后您创建一个该类型的对象并在 setMouseCallback
的最后一个参数中使用它的地址。
您必须确保的主要事情 - 该对象的生命周期必须覆盖窗口的生命周期。也就是说,对象必须在第一次调用 onMouse
之前创建,并在最后一次调用之后销毁。您可以通过在 main
的开头声明变量来做到这一点。然后该对象将在程序启动后的早期创建,并在程序结束时由编译器自动销毁。这是示例。
typedef std::pair<vector<Point2d>, int> data_holder_type; // note the absence of references, this pair holds std::vector and int
void onMouse(int event, int x, int y, int f, void* ptr){
if(event == CV_EVENT_LBUTTONDOWN){
data_holder_type *dholder = static_cast<data_holder_type *>(ptr);
dholder->first.push_back(Point(x,y));
dholder->second.clickCount++;
}
}
....
int main(void) {
data_holder_type dholder;
// add code to initialize your dholder;
...
setMouseCallback("test", onMouse, &dholder);
...
cv::waitKey(); // wait until the window is closed
// read values from dholder and process them
} //dhloder is deleted somewhere here
另一个重要的事情是对这个对象的并发访问。 onMouse
从单独的 parallel thread 调用, 如果您的 dholder
在窗口打开时同时在 main
和 onMouse
中被读取或修改,race conditions会发生。通常,它们通常会导致不可预测且很难捕获的错误。
一切正常,而您的 main
在窗口关闭之前不会访问 dholder
。
至于你关于void *
的问题。请注意 onMouse
中的 if
内的行。 ptr
指向void
类型的对象。该对象没有任何成员,first
、second
或任何其他成员。如果您尝试使用 ptr
(例如 ptr->first
)访问它们,您将遇到编译器错误。因此,您必须将此指针转换为指向另一种类型的指针,该类型包含有关它指向的对象的一些信息,在本例中为 data_holder_type *
。
任何指针类型都可以转换为void *
,而void *
可以转换为任何其他指针类型。这允许您为不同的窗口设置多个不同的回调。
小心错误的转换!编译器不进行任何检查。
此示例展示了如何为具有不同标题的窗口设置 3 种不同的鼠标回调。
typedef blah-blah-blah1 data_holder_type1;
typedef blah-blah-blah2 data_holder_type2;
typedef blah-blah-blah3 data_holder_type3;
void onMouse1(int event, int x, int y, int f, void* ptr){
if(event == CV_EVENT_LBUTTONDOWN){
data_holder_type1 *dholder = static_cast<data_holder_type1 *>(ptr);
dholder->first.push_back(Point(x,y));
dholder->second.clickCount++;
}
}
void onMouse2(int event, int x, int y, int f, void* ptr){
if(event == CV_EVENT_LBUTTONDOWN){
data_holder_type2 *dholder = static_cast<data_holder_type2 *>(ptr);
// processing, related to another data type
}
}
void onMouse3(int event, int x, int y, int f, void* ptr){
if(event == CV_EVENT_LBUTTONDOWN){
data_holder_type3 *dholder = static_cast<data_holder_type3 *>(ptr);
// process
}
}
int main(void) {
data_holder_type1 dholder1;
data_holder_type2 dholder2;
data_holder_type3 dholder3;
// add code to initialize your dholders;
...
setMouseCallback("test1", onMouse1, &dholder1);
setMouseCallback("test2", onMouse2, &dholder2);
setMouseCallback("test3", onMouse3, &dholder3);
...
}
关于c++ - OpenCV 通过引用或指向回调函数的指针传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28871794/
我正在研究 learnyounode 的 HTTP 客户端作业。 我想知道为什么控制台记录来自response.on(“end”,callback)的数据仅输出预期输出的最后一部分,而控制台记录来自r
我正在尝试创建一个对象列表(在我的示例中为 List),我在其中使用 json 将对象添加到此列表,但该列表仍为空。这是我的代码: public List readCardsFromJson() {
我有一个 JavaScript 函数“print_something”,它在大约 300 个 jsp 帮助页面中实现。我发现这个“print_something”函数必须被纠正。所以我正在寻找一个不更
有 2 个 HTML 下拉列表,一个用于 12 小时时间,一个用于每小时 5 分钟的时间间隔。 .. 1 .. 12 .. 0 .. 55 .. 一直在尝试使用 if/
我有一个 A 类,我打算在它与设备驱动程序交互时将其放入共享库中。 我有一个 B 类,将来可能是 C、D、E...,它将使用共享库中的 A 类。 我想要在类 A 中设置回调函数的功能,以便当特定事件发
我需要能够在处理完 Observable.next() 之后执行回调。 我有一个组件“A”,它有一个主题使用 Subject.next() 发送通知。我有一个组件“B”,它订阅了 Subject.as
我有一张在顶部和底部单元格下方带有阴影的表格(此处使用 Matt Gallagher 的解决方案:http://cocoawithlove.com/2009/08/adding-shadow-effe
有人可以向我解释一下为什么这段代码有效 renderSquare(i) { return ( this.handleClick(i)} /> ); } 但
我可以让两个不同的客户端监听相同的 WCF 回调并让它们都接收相同的数据而不必进行两次处理吗? 最佳答案 不是真的 - 至少不是直接的。你所描述的听起来很像发布/订阅模式。 WCF 服务基本上在任何给
我是 SignalR 的新手,如果这个问题太明显,我深表歉意,但我在文档中找不到任何答案。 这是我的代码。 /*1*/ actions.client.doActionA = function (r
我有这个应用程序,您可以在其中输入一些文本并按下一个按钮,将此文本添加到自定义小部件中。这是代码: import 'dart:core'; import 'package:flutter/materi
我读到当您还想使用模型回调时不能使用 Keras 进行交叉验证,但是 this post表明这毕竟是可能的。但是,我很难将其纳入我的上下文。 为了更详细地探讨这个问题,我正在关注 machinelea
我尝试在重力表单中提交表单失败后运行一些 jQuery 代码,也就是验证发现错误时。 我尝试使用 Ajax:complete 回调,但它根本不触发。 我尝试运行的代码基本上将监听器添加到选择下拉列表中
我有一个 $image,我 .fadeIn 和 .fadeOut,然后 .remove .fadeOut 完成。这是我的代码: $image .fadeIn() .fadeOut(func
我正在处理一个自定义文件路径类,它应该始终执行一个函数 写入相应的系统文件及其文件对象后 关闭。该函数将文件路径的内容上传到远程位置。 我希望上传功能完全在用户的幕后发生 透视,即用户可以像使用其他任
这里是 javascript 新手,所以回调在我的大脑中仍然有点不确定。 我想做的是:给定一个“菜单”,它是一个 objectId 数组,查询与该 objectId 相对应的每个 foodItem,获
我正在学习回调,我编写了以下代码: var http = require('http'); var str = ""; var count = 2; function jugglingAsync(ca
这是我的困境,我有一系列被调用的函数,我正在使用回调函数在它们完成时执行函数。回调返回一个值并且效果也很好,我的问题是当我向回调添加参数时我无法再访问返回值。这是一个有效的例子: function m
This question already has answers here: Explanation of function pointers (4个答案) 上个月关闭。 如何将函数指针作为参数传递
我无法让以下代码工作。假设 ajax 调用有效,并且 msg['username'] 预设为 'john'。我想我对如何将变量传递给回调感到困惑。编辑:我认为我的主要困惑是如何从 Ajax 中获取“m
我是一名优秀的程序员,十分优秀!