C++ 添加一个变量以引用函数
这是VisualStudioC++中的代码,C++ 添加一个变量以引用函数,c++,visual-studio,C++,Visual Studio,这是VisualStudioC++中的代码, //draw a line onto the frame line(imgFrame2Copy, Point(0, imgFrame2Copy.rows / 2), Point(imgFrame2Copy.cols, imgFrame2Copy.rows / 2), Scalar(0), 3); //display the result line(imgFrame2Copy, Point(0, i
//draw a line onto the frame
line(imgFrame2Copy, Point(0, imgFrame2Copy.rows / 2), Point(imgFrame2Copy.cols, imgFrame2Copy.rows / 2), Scalar(0), 3);
//display the result
line(imgFrame2Copy, Point(0, imgFrame2Copy.rows / 8), Point(imgFrame2Copy.cols, imgFrame2Copy.rows / 8), Scalar(0), 3);
//wait some time for the frame to render
waitKey(30);
drawCarCountOnImage(carCount, imgFrame2Copy);
cv::imshow("imgFrame2Copy", imgFrame2Copy);
//cv::waitKey(0); // uncomment this line to go frame by frame for debugging
// now we prepare for the next iteration
我想添加一个变量来引用函数行(imgFrame2Copy,Point(0,imgFrame2Copy.rows/2),Point(imgFrame2Copy.cols,imgFrame2Copy.rows/2),Scalar(0),3)代码>
你能帮助我吗
CordIOR.< P>如果你有机会使用现代C++(C++ 11或更高版本),你可以使用lambda(如R SAHU所述):
请注意,通过按值捕获我假设的OpenCV矩阵cv::Mat
,您创建了一个引用计数对象的副本,因此lambda在其生存期内应该是正常的。也就是说,如果您创建了太多这些,您可能会耗尽内存;-)
否则,可以使用C++98样式的函子:
struct DrawMyLine {
void operator()(cv::Mat& imgFrame2Copy, int denominator) {
line(imgFrame2Copy,
Point(0, imgFrame2Copy.rows / denominator),
Point(imgFrame2Copy.cols, imgFrame2Copy.rows / denominator),
Scalar(0),
3);
}
};
DrawMyLine drawLine;
drawLine(imgFrame2Copy, 2);
签出lambda表达式。当你说添加一个函数时?你能澄清你的意思吗?最简单地说,如果参数具有正确的返回类型,您应该能够用函数名替换参数。欢迎使用堆栈溢出。请花点时间阅读并参考您可以在此处询问的内容和方式。此功能行(imgFrame2Copy,Point(0,imgFrame2Copy.rows/2),Point(imgFrame2Copy.cols,imgFrame2Copy.rows/2),Scalar(0),3);为了画一条线,我想在我的代码中添加一个引用这条线的变量
struct DrawMyLine {
void operator()(cv::Mat& imgFrame2Copy, int denominator) {
line(imgFrame2Copy,
Point(0, imgFrame2Copy.rows / denominator),
Point(imgFrame2Copy.cols, imgFrame2Copy.rows / denominator),
Scalar(0),
3);
}
};
DrawMyLine drawLine;
drawLine(imgFrame2Copy, 2);