开发者

mouse handler in opencv for large images, wrong x,y coordinates?

i am using images that are 2048 x 500 and when I use cvShowImage, I only see half the image. This is not a big deal because the interesting part is on the top half of the image. Now, when I use the mouseHandler to get the x,y coordinates of my clicks, I noticed that the coordinate for y (the dimension that doesnt fit in the screen) is wrong.

It seems OpenCV think this is the whole image and recalibrates the coordinate system although we are only effectively showing half the image.

I would need to know how to do 2 things: - display a resized image that would fit in the screen

  • get the proper coordinate.

Did anybody encounter similar problems?

Thanks!

Update: it seems the y coordinate is divided by 2 of what it is supposed to be

code:

EXPORT void click_rect(uchar * the_img, int size_x, int size_y, int * points)
{
CvSize size;
size.height = size_y ;
size.width = size_x;

IplImage * img;
img = cvCreateImageHeader(size, IPL_开发者_StackOverflowDEPTH_8U, 1);
img->imageData = (char *)the_img;
img->imageDataOrigin = img->imageData;

img1 = cvCreateImage(cvSize((int)((size.width)) , (int)((size.height)) ),IPL_DEPTH_8U, 1);

cvNamedWindow("mainWin",CV_WINDOW_AUTOSIZE); 
cvMoveWindow("mainWin", 100, 100);

cvSetMouseCallback( "mainWin", mouseHandler_rect, NULL );

cvShowImage("mainWin", img1 );

//// wait for a key
cvWaitKey(0);

points[0] = x_1;
points[1] = x_2;
points[2] = y_1;
points[3] = y_2;

//// release the image
cvDestroyWindow("mainWin");
cvReleaseImage(&img1 );
cvReleaseImage(&img);
}


You should create a window with the CV_WINDOW_KEEPRATIO flag instead of the CV_WINDOW_AUTOSIZE flag. This temporarily fixes the problem with your y values being wrong.


I use OpenCV2.1 and visual studio C++ compiler. I fix this problem with another flag CV_WINDOW_NORMAL and work properly and returns correct coordinates, this flag enables you to resize the image window.

cvNamedWindow("Box Example", CV_WINDOW_NORMAL);


I am having the same problem with OpenCV 2.1 using it with Windows and mingw compiler. It took me forever to find out what was wrong. As you describe it, cvSetMouseCallback gets too large y coordinates. This is apparently due to the image and the cvNamedWindow it is shown in being bigger than my screen resolution; thus I cannot see the bottom of the image.

As a solution I resize the images to a fixed size, such that they fit on the screen (in this case with resolution 800x600, which can be any other values:

// g_input_image, g_output_image and g_resized_image are global IplImage* pointers.

int img_w = cvGetSize(g_input_image).width;
int img_h = cvGetSize(g_input_image).height;

// If the height/width ratio is greater than 6/8 resize height to 600.
if (img_h > (img_w*6)/8) {
    g_resized_image = cvCreateImage(cvSize((img_w*600)/img_h, 600), 8, 3);
}
// else adjust width to 800.
else {
    g_resized_image = cvCreateImage(cvSize(800, (img_h*800)/img_w), 8, 3);
}

cvResize(g_output_image, g_resized_image);

Not a perfect solution, but works for me...

Cheers, Linus


How are you building the window? You are not passing CV_WINDOW_AUTOSIZE to cvNamedWindow(), are you?

Share some source, @Denis.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜