Ask Your Question
0

imshow displays image too wide

asked 2016-10-09 10:36:21 -0600

psc gravatar image

I installed OpenCV 3 on my Mac (El Capitan, 10.11.6) via homebrew and tried to load and display an image by using exactly the code from this tutorial.

My input image was this 100x100Px Jpg file: Input Image

For some reason the output window looks like this: Displayed Image

So there is this gray area added which is as big as the original image.


Furthermore this leads to some more problems:

1.onMouse Callback reports wrong click positions: I attached this onMouse Callback to the window to log positions of my clicks:

static void onMouse( int event, int x, int y, int, void* ){
    if( event != EVENT_LBUTTONDOWN ) return;
    cout << "You clicked on (" << x << "," << y << ")" << endl;}

The results are:

  • When I click in the middle of the 4 coloured fields (should be 50,50): (25,50)
    • When I click in the bottom right corner of the blue field (should be 100,100): (50,100)
    • When I click in the bottom right corner of the gray area: (100,100)

2.I tried to extend the onMouse callback to print the colour of the pixel that was clicked:

static void onMouse( int event, int x, int y, int, void* ){
    if( event != EVENT_LBUTTONDOWN ) return;
    cout << "You clicked on (" << x << "," << y << ")" << endl;
    Vec3b bgrPixel = image.at<Vec3b>(x, y);
    cout << "The color of this pixel is: " << bgrPixel << endl;}
  • When i click into the black area i get: You clicked on (11,18). The color of this pixel is: [0, 0, 0] (CORRECT)

  • When i click into the green area i get: You clicked on (37,23).The color of this pixel is: [0, 0, 0] (WRONG)

  • When i click into the red area i get: You clicked on (10,72). The color of this pixel is: [1, 255, 0] (WRONG)

  • When i click into the blue area i get: You clicked on (35,74). The color of this pixel is: [1, 255, 0] (WRONG)


My theory is that there is a problem with the datatype of the pixel BGR-values which are stored in the image Mat, but as I'm completely new to OpenCV I'm stuck at this point and don't know how to solve this problem.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-10-09 13:51:11 -0600

unxnut gravatar image

Your input image is of lesser width than the window title. Change the window title to a smaller string and the problem will go away. The utility is trying to show the complete window name string.

edit flag offensive delete link more

Comments

Changing the window title to a shorter string did actually not work for me. Even with an empty string the window was to wide. But you are right that the problem apparently comes from some minimum window size: When I display bigger images there is no additional gray area. What bothers me most is, that the gray area is clickable and the click coordinates of the onMouse callback are wrong. Because the picture is 100x100Px I would like to get the coordinates (100,100) when I click on the bottom right corner of the blue square. Any solution for that?

psc gravatar imagepsc ( 2016-10-11 07:13:18 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-10-09 10:34:35 -0600

Seen: 728 times

Last updated: Oct 09 '16