如何处理trackbar回调参数,以使我的代码可以与键入为Mat的函数一起编译?
一些背景信息:函数rangeIN()用于接收一些输入Mat并返回Mat。 openCV中的createTrackbar()给我带来了一些麻烦,因为我不确定如何将参数与所使用的函数进行匹配。
Mat rangeIN(const Mat& src_rangein) {
Mat dst_HSV;
int low_H = 0, low_S = 0, low_V = 0;
int high_H = 0, high_S = 0, high_V = 0;
cvtColor(src_rangein, dst_HSV, COLOR_BGR2HSV);
inRange(dst_HSV, Scalar(low_H, low_S, low_V), Scalar(high_H, high_S, high_V), dst_HSV);
namedWindow(window_capture_name);
namedWindow(window_detection_name);
// Trackbars to set thresholds for HSV values
createTrackbar("Low H", window_detection_name, &low_H, max_value_H, rangeIN); //"no instance of overloaded function "rangeIN" matches the required type
createTrackbar("High H", window_detection_name, &high_H, max_value_H, rangeIN); //same problem?
createTrackbar("Low S", window_detection_name, &low_S, max_value, rangeIN); //same problem?
createTrackbar("High S", window_detection_name, &high_S, max_value, rangeIN); //same problem?
createTrackbar("Low V", window_detection_name, &low_V, max_value, rangeIN); //same problem?
createTrackbar("High V", window_detection_name, &high_V, max_value, rangeIN); //same problem?
imshow(window_capture_name, src_rangein);
imshow(window_detection_name, dst_HSV);
return dst_HSV;
}
我想这样做可以减少全局变量的使用。
当前尝试解决该问题的方法(可以运行,但仍具有全局int值):
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
using namespace cv;
const int max_value_H = 360 / 2;
const int max_value = 255;
const String window_capture_name = "Video Capture";
const String window_detection_name = "Object Detection";
int low_H = 0, low_S = 0, low_V = 0;
int high_H = max_value_H, high_S = max_value, high_V = max_value;
Mat rangeIN(const Mat & src_rangin) {
Mat dst_rangein;
cvtColor(src_rangin, dst_rangein, COLOR_BGR2HSV);
inRange(dst_rangein, Scalar(low_H, low_S, low_V), Scalar(high_H, high_S, high_V), dst_rangein);
namedWindow(window_capture_name);
namedWindow(window_detection_name);
imshow(window_capture_name, src_rangin);
imshow(window_detection_name, dst_rangein);
return dst_rangein;
}
void paraRangein(int, void*) {
createTrackbar("Low H", window_detection_name, &low_H, max_value_H, paraRangein);
createTrackbar("High H", window_detection_name, &high_H, max_value_H, paraRangein);
createTrackbar("Low S", window_detection_name, &low_S, max_value, paraRangein);
createTrackbar("High S", window_detection_name, &high_S, max_value, paraRangein);
createTrackbar("Low V", window_detection_name, &low_V, max_value, paraRangein);
createTrackbar("High V", window_detection_name, &high_V, max_value, paraRangein);
waitKey();
}
int main(int argc, char* argv[])
{
Mat src = imread("kid.jpg");
while (1) {
rangeIN(src);
paraRangein(0, 0);
}
waitKey();
return 0;
}
编辑:当前可能是我写过的最糟糕的结构化程序。如果有人可以在评论中激发想法,这可能有助于产生更好的解决方案。
如果这是一个合理的答案,请@@ Tas。 “您可以使用一个中间函数,然后将该函数调用Mat someFunction。但是您无法将值返回到cv :: createTrackbar。您可能需要详细说明自己在做什么。另请参见How to successfully pass a functional object (or lambda) to the trackbar callback second argument (void*)? “