I am new to C++ and opencv, I am trying to use the coordinates of the clicked mouse and later track that object using Kalman filter.
The problem is I can't access the x and y coordinate of the mouse clicking on the object in real time video.
There are lots of codes which show how to do it but it didnt work for me.
Here is my code:
void CallBackFunc(int event, int x, int y, int flags, void* leftCoordinate){
Point *p = (Point*) leftCoordinate;
if ( event == EVENT_LBUTTONDOWN )
{
cout << "Left button position (" << x << ", " << y << ")" << endl;
p->x = x;
p->y = y;
cout << "this is the pointer : " << *p << endl;
}
}
int main(int argc, const char** argv )
{
// Getting the video here
Point TestP;
setMouseCallback("Original", CallBackFunc, &TestP);
cout << "The coordinates : x = " << TestP.x << " y = " << TestP.y << endl;
}
The problem is, TestP is always empty and I need to use that x and y coordinate in my main.
I really appreciate any help. Thanks
Seems like your main function will just exit before you even click the image. The TestP
will change only when the callback is called, that is when you click on an image. You probably won't be able to show it in your main function like you show in your example because the end of the function is reached before the coordinate is updated.
In your code you didn't show any imshow
or waitKey
. imshow
is essential to grab the mouse events, while waitKey
is needed to refresh the event queue.
To work with mouse callbacks in OpenCV, is often easier to use global variables. I know, it's not a good practice in general, but in this context works just fine and makes things easier. Remember that OpenCV HighGui module is not meant to create a full featured GUI, but basically just to help debugging. If you need a complete GUI, you should rely on GUI libraries such as Qt.
Have a look at this code. It will print out the clicked point coordinates, and shows on the video stream the last clicked coordinate:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
Mat frame;
Point pt(-1,-1);
bool newCoords = false;
void mouse_callback(int event, int x, int y, int flag, void *param)
{
if (event == EVENT_LBUTTONDOWN)
{
// Store point coordinates
pt.x = x;
pt.y = y;
newCoords = true;
}
}
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if (!cap.isOpened()) // check if we succeeded
return -1;
Mat edges;
namedWindow("img", 1);
// Set callback
setMouseCallback("img", mouse_callback);
for (;;)
{
cap >> frame; // get a new frame from camera
// Show last point clicked, if valid
if (pt.x != -1 && pt.y != -1)
{
circle(frame, pt, 3, Scalar(0, 0, 255));
if (newCoords)
{
std::cout << "Clicked coordinates: " << pt << std::endl;
newCoords = false;
}
}
imshow("img", frame);
// Exit if 'q' is pressed
if ((waitKey(1) & 0xFF) == 'q') break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.