我的目标是创建一个vector<Rect>
,现在我只从一个Rect
开始,包含用户在一个框架中创建的任意数量的ROI,最初是从摄像机拍摄的。通过在按下并抬起鼠标时抓取点来创建ROI。
我知道填充投资回报率的代码正在onMouse
使用已注释的cout
。如果我尝试在setup
函数的while循环中打印出ROI,则它为空。我搞砸了指针还是别的什么?
using namespace std;
using namespace cv;
//Global Variables
Point P1(0,0);
Point P2(0,0);
bool complete = false;
struct MouseParams
{
Mat img;
Point pt;
int lucky;
};
void onMouse ( int event, int x, int y, int d, void *ptr ){
switch(event){
//mouse pressed (get coordinate of first point)
case CV_EVENT_LBUTTONDOWN:
cout << "press" << endl;
P1.x=x;
P1.y=y;
break;
//mouse lifted (get coordinate of final point)
case CV_EVENT_LBUTTONUP:
cout << "lift" << endl;
P2.x=x;
P2.y=y;
complete = true;
break;
default:
break;
}
//If lifted mosue and have two valid points()
if(complete && P1 != P2){
cout << "Building ROI" << endl;
Rect*ROI = (Rect*)ptr;
int w,h;
h= P2.y-P1.y;
w = P2.x-P1.x;
ROI->x=x;
ROI->y=y;
ROI->width=w;
ROI->height=h;
cout << ROI << endl;
complete = false;
}
}
Rect setup(Mat frame){
Rect ROI;
while(true){
namedWindow("ROI");
setMouseCallback("ROI",onMouse, &ROI);
cout << ROI << endl;
//draw ROI grabbed in onMouse
rectangle( frame, ROI, Scalar(0,255,0), 1, 8, 0 );
imshow("ROI",frame);
moveWindow("ROI", 0,50);
waitKey(0);
return ROI;
}
}
UPDATE:修改waitkey(),因为它正在等待输入向前移动的while循环。现在代码可以保留最新的ROI(可以在设置框架中绘制很多)但需要适应保留向量,但感觉使用指针很难做到。
key = (char)waitKey(0); // explicit cast
if (key == 27) break; // break if `esc' key was pressed.
if (key == ' ') continue;;
}
destroyAllWindows();
return ROI;