问题
I've been trying to do some work with OpenCV in VS2010, specifically in the area of mouse handling. So far, I have this:
CV_EVENT_LBUTTONDOWN
:drawing_line = true;
cvLine( frame, cvPoint(x,y),cvPoint(350,500), CV_RGB(255,0,0), CV_AA, 15,0 );
fprintf( stdout, "Point found. %i, %i \n", object_x0, object_y0 );
break;
What I want it to do is return the location of the pixels that I clicked on but all it returns is "Point found. 0,0" instead of the actual location. Eventually, I would like to use the points with cvLine to draw a line but right now I would just like to get some values returned to me. Any suggestions would be much appreciated. Thanks!
回答1:
You can obtain the position of a mouse-click by passing it as the parameter to the mouse callback function like so:
void onMouse(int evt, int x, int y, int flags, void* param) {
if(evt == CV_EVENT_LBUTTONDOWN) {
cv::Point* ptPtr = (cv::Point*)param;
ptPtr->x = x;
ptPtr->y = y;
}
}
int main() {
cv::Point2i pt(-1,-1);
cv::namedWindow("Output Window");
frame = cv::imread("image.jpg");
cv::imshow(winName, frame);
cv::setMouseCallback(winName, onMouse, (void*)&pt);
// Note that we passed '&pt' (a pointer
// to `pt`) to the mouse callback function.
// Therefore `pt` will update its [x,y] coordinates
// whenever user left-clicks on the image in "Output Window".
}
回答2:
Points are passed in as arguments to the Mouse callback function.
void onMouse(int event, int x, int y, int flags, void* param)
You'll want to save those x, y into a global when you click down, then a different global when you click up, then draw a line between the two.
来源:https://stackoverflow.com/questions/5988077/mouse-handling-printing-pixel-location