Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

imshow displays image too wide

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.