(x,y) bug in MouseCallback when image is larger than your screen (Bug #1523)


Added by fbhaar - about 13 years ago. Updated almost 11 years ago.


Status:Done Start date:
Priority:Low Due date:
Assignee:- % Done:

0%

Category:highgui-gui
Target version:-
Affected version:2.4.9 (latest release) Operating System:Windows
Difficulty: HW Platform:x86
Pull request:https://github.com/Itseez/opencv/pull/2739

Description

DESCRIPTION OF THE BUG

The setMouseCallBack provides an incorrect (x,y) when the user clicks on an image that is larger than the screen.
When the image finds the screen mouse clicking works fine.
When only 10% of the image falls outside my screen the error is small.
When 50% of the image doesn't fit the screen the error is larger. Therefore it must be related to the visible area and the image size.
The following code demonstrates the bug, make sure to select more image.rows than your screen is able to display.
This bug was detected under windows 32-bit and windows 64-bit.

Thanks in advance, and keep up the good work!
best wishes, Frank ter Haar

SAMPLE CODE

 1#include <opencv2/highgui/highgui.hpp>
 2#include <opencv2/core/core.hpp>
 3#include <opencv2/imgproc/imgproc.hpp>
 4
 5using namespace cv;
 6
 7string title = "mouse callback bug";
 8int image_height_too_large_for_your_screen = 2000;
 9
10void onMouseClick(int event, int x, int y, int flags, void* param )
11{
12  if(event != CV_EVENT_LBUTTONDOWN) return;
13
14  printf("%d, %d\n",x,y);
15  Mat* in = (Mat*)param;
16  Mat tmp;
17  in->copyTo( tmp );
18  circle(tmp,Point(x,y),5,Scalar(255));
19
20  string text = "this is not where I clicked";
21  cv::putText(tmp,text,Point(x,y),0, 0.5, Scalar(255)); 
22  imshow(title, tmp);
23}
24
25int main( int argc, char** argv ) 
26{
27  Mat image = Mat::zeros(image_height_too_large_for_your_screen, 800, CV_8U);
28  cv::imshow(title, image);
29  cv::setMouseCallback( title, onMouseClick, (void *)(&image) );
30  cv::waitKey(0);
31}

History

Updated by Vadim Pisarevsky about 13 years ago

thanks for the report and sample. We will deal with the bug as soon as we can; for now I have to lower priority of the bug, since highgui is not meant to be very sophisticated tool. As a possible workaround, you can create the highgui window using namedWindow(title, 0), which will mean variable scale and so the window will fit the display.

Updated by sebhaase - about 13 years ago

I could not reproduce your problem on OSX. Even with 3000 rows those circles appear exactly where I click.

- Sebastian Haase

PS: thanks for posting a complete sample - that always helps a lot !

Updated by Andrey Kamaev about 13 years ago

  • Description changed from DESCRIPTION OF THE BUG The setMouseCallBack provides an incorrect (x,y) when... to DESCRIPTION OF THE BUG The @setMouseCallBack@ provides an incorrect (x,y) wh... More

Updated by Alexander Shishkov almost 13 years ago

  • Priority changed from High to Normal
  • Target version deleted ()

Updated by Alexander Shishkov almost 13 years ago

  • Priority changed from Normal to Low

Updated by Alexander Shishkov almost 13 years ago

  • Target version deleted ()

Updated by Andrey Kamaev over 12 years ago

  • Category changed from highgui-images to highgui-gui

Updated by Dmitry Retinskiy over 11 years ago

I couldn't reproduce it either (opencv 2.4.5, Ubuntu 12.04)

Updated by Jukka Holappa almost 11 years ago

I'm able to reproduce this bug with Windows 7 using OpenCV 2.4.8 compiled with Visual Studio 2012. I do not have Qt enabled. Win32 UI and OpenGL support are enabled (although OpenGL is not used in affected window). It's just created with cv::namedWindow() and updated with cv::imshow() without any special flags.

It seems like mouse coordinates given to mouse callback are scaled with (window width / screen width, window height / screen height) as they are correct in top left corner of the window and using an image twice the height of native desktop seems to double the coordinate values for mouse callback.

Just moving the window outside visible area does not trigger the bug. The window must be larger than native desktop resolution for bug to trigger.

Updated by Jukka Holappa almost 11 years ago

The problem is caused by event handler in window_w32.cpp that scales the image coordinates unconditionally for the case where CV_WINDOW_AUTOSIZE is not set. It also works correctly as long as actual window size and underlying image size is the same.

The functionality is wrong for the case where image is not drawn using rescaling (CV_WINDOW_AUTOSIZE in use) and created window does not have the correct size. In this case coordinates need to be original coordinates because part of the image is just clipped from right side or bottom.

By replacing function call in window_w32.cpp near line 1484

window->on_mouse(event, pt.x*size.cx / MAX(rect.right - rect.left, 1),
                        pt.y*size.cy / MAX(rect.bottom - rect.top, 1), flags,
                 window->on_mouse_param);

with the following code, all the cases seem to work on Windows as well:

if (window->flags & CV_WINDOW_AUTOSIZE)
{
    // As user can't change window size, do not scale window coordinates. Underlying windowing system
    // may prevent full window from being displayed and in this case coordinates should not be scaled.
    window->on_mouse(event, pt.x, pt.y, flags, window->on_mouse_param);
} else
{
    // Full window is displayed using different size. Scale coordinates to match underlying positions.
    window->on_mouse(event, pt.x*size.cx / MAX(rect.right - rect.left, 1),
                            pt.y*size.cy / MAX(rect.bottom - rect.top, 1), flags,
                     window->on_mouse_param);
}

Sorry for not providing an actual patch against the latest version.

Updated by Vladislav Vinogradov almost 11 years ago

Jukka Holappa, thank you for your investigation.

Could you create a pull request with your fix (http://code.opencv.org/projects/opencv/wiki/How_to_contribute)? All help to the project is highly appreciated!

  • HW Platform set to x86
  • Operating System set to Windows
  • Affected version set to 2.4.9 (latest release)

Updated by Steven Puttemans almost 11 years ago

Fix created!

  • Pull request set to https://github.com/Itseez/opencv/pull/2739

Updated by Steven Puttemans almost 11 years ago

Fix merged! Thanks for the report!

  • Status changed from Open to Done

Also available in: Atom PDF