Defect in imshow() with Qt which truncates displayed XY coordinates

There is an example program to test the opencv Python install which pops up a window displaying an image and writes the mouse pointer XY coordinates to the lower left hand corner of the window. Half of the coordinates fall outside of the window and the window is not resizeable. The same error occurs with the .cpp version.

Opensuse leap 15.4/64bit, 5.14.21-150400.24.88-default #1 SMP PREEMPT_DYNAMIC
/usr/bin/python3.10

Here is the Python code:

#!/usr/bin/python
import cv2 as cv
img = cv.imread(“rad_weight.f.jpg”)

cv.imshow(“Brian’s Python window”, img)
k = cv.waitKey(0) # Wait for a keystroke in the window

========================
Here is a screen capture of the lower left corner of the image window:
python.defect.xy.coords.truncated

it pretty much sounds like the problem is not imread() but the gui / imshow() part.

you could add a simple

cv.imwrite("output.png", img)

and check the result with another image viewer

then, how did you install cv2 ?
what does

print(cv.getBuildInformation())

say in the GUI section ?

The print statement does nothing. It does not show up on the GUI nor on the command shell stdout/stderr.
I built the cpp version from source from gethub opencv/opencv/archive/4.x zip by the instructions in docs.opencv.org 4.x/d7/d9f/tutorial_linux_install html

Python source collided with VLC player and wanted to downgrade ffmpeg, libavformat57 and ~30 other source modules. I installed with:
sudo pip3 install opencv-python

I found an opencv_version.py:

python  /usr/share/doc/packages/opencv-doc/examples/python/opencv_version.py --build

prints OpenCV version

Usage:
    opencv_version.py [<params>]
    params:
        --build: print complete build info
        --help:  print this help


General configuration for OpenCV 4.5.5 =====================================
  Version control:               unknown

  Platform:
    Timestamp:                   2022-01-10T12:00:00Z
    Host:                        Linux 5.14.21-150400.21-default x86_64
    CMake:                       3.20.4
    CMake generator:             Unix Makefiles
    CMake build tool:            /usr/bin/gmake
    Configuration:               Release

  CPU/HW features:
    Baseline:                    SSE SSE2
      requested:                 SSE2
    Dispatched code generation:  SSE3 SSE4_1 SSE4_2 FP16 FMA3 AVX AVX2
      requested:                 SSE3 SSE4_1 SSE4_2 FP16 FMA3 AVX AVX2 AVX512_ICL
      SSE3 (1 files):            + SSE3
      SSE4_1 (18 files):         + SSE3 SSSE3 SSE4_1
      SSE4_2 (2 files):          + SSE3 SSSE3 SSE4_1 POPCNT SSE4_2
      FP16 (1 files):            + SSE3 SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
      FMA3 (0 files):            + SSE3 SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
      AVX (5 files):             + SSE3 SSSE3 SSE4_1 POPCNT SSE4_2 AVX
      AVX2 (33 files):           + SSE3 SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2

  C/C++:
    Built as dynamic libs?:      YES
    C++ standard:                11
    C++ Compiler:                /usr/bin/c++  (ver 7.5.0)
    C++ flags (Release):         -fmessage-length=0 -grecord-gcc-switches -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection -g -DNDEBUG   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fno-omit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
    C++ flags (Debug):           -fmessage-length=0 -grecord-gcc-switches -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection -g -DNDEBUG   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fno-omit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -fvisibility=hidden -fvisibility-inlines-hidden -g  -DDEBUG -D_DEBUG
    C Compiler:                  /usr/bin/cc
    C flags (Release):           -fmessage-length=0 -grecord-gcc-switches -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection -g -DNDEBUG   -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fno-omit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
    C flags (Debug):             -fmessage-length=0 -grecord-gcc-switches -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection -g -DNDEBUG   -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fno-omit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -fvisibility=hidden -g  -DDEBUG -D_DEBUG
    Linker flags (Release):      -Wl,--as-needed -Wl,--no-undefined -Wl,-z,now  -Wl,--gc-sections -Wl,--as-needed  
    Linker flags (Debug):        -Wl,--as-needed -Wl,--no-undefined -Wl,-z,now  -Wl,--gc-sections -Wl,--as-needed  
    ccache:                      NO
    Precompiled headers:         NO
    Extra dependencies:          dl m pthread rt
    3rdparty dependencies:

  OpenCV modules:
    To be built:                 aruco calib3d core dnn face features2d flann gapi highgui imgcodecs imgproc ml objdetect optflow photo plot python3 shape stitching superres tracking ts video videoio videostab ximgproc
    Disabled:                    world
    Disabled by dependency:      -
    Unavailable:                 java python2
    Applications:                tests perf_tests apps
    Documentation:               NO
    Non-free algorithms:         NO

  GUI:                           QT5
    QT:                          YES (ver 5.15.2 )
      QT OpenGL support:         YES (Qt5::OpenGL 5.15.2)
    GTK+:                        NO
    OpenGL support:              YES (/usr/lib64/libGL.so /usr/lib64/libGLU.so)
    VTK support:                 NO

  Media I/O: 
    ZLib:                        /usr/lib64/libz.so (ver 1.2.11)
    JPEG:                        /usr/lib64/libjpeg.so (ver 80)
    WEBP:                        /usr/lib64/libwebp.so (ver encoder: 0x020e)
    PNG:                         /usr/lib64/libpng.so (ver 1.6.34)
    TIFF:                        /usr/lib64/libtiff.so (ver 42 / 4.0.9)
    JPEG 2000:                   OpenJPEG (ver 2.3.0)
    OpenEXR:                     /usr/lib64/libImath.so /usr/lib64/libIlmImf.so /usr/lib64/libIex.so /usr/lib64/libHalf.so /usr/lib64/libIlmThread.so (ver 2_2)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:
    DC1394:                      YES (2.2.5)
    FFMPEG:                      YES
      avcodec:                   YES (58.134.100)
      avformat:                  YES (58.76.100)
      avutil:                    YES (56.70.100)
      swscale:                   YES (5.9.100)
      avresample:                YES (4.0.0)
    GStreamer:                   YES (1.20.1)
    v4l/v4l2:                    YES (linux/videodev2.h)
    Xine:                        NO

  Parallel framework:            TBB (ver . interface )

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    VA:                          YES
    Lapack:                      YES (/usr/lib64/libopenblas.so)
    Eigen:                       YES (ver 3.3.4)
    Custom HAL:                  NO
    Protobuf:                    build (3.19.1)

  OpenCL:                        YES (INTELVA)
    Include path:                /home/abuild/rpmbuild/BUILD/opencv-4.5.5/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

  Python 3:
    Interpreter:                 /usr/bin/python3 (ver 3.6.15)
    Libraries:                   /usr/lib64/libpython3.6m.so (ver 3.6.15)
    numpy:                       /usr/lib64/python3.6/site-packages/numpy/core/include (ver 1.17.3)
    install path:                /usr/lib64/python3.6/site-packages

  Python (for build):            /usr/bin/python3

  Java:                          
    ant:                         NO
    JNI:                         NO
    Java wrappers:               NO
    Java tests:                  NO

  Install to:                    /usr
-----------------------------------------------------------------


Done

again, please try to save an image & check it

also, try to set the WINDOW_NORMAL flag, to see, if disabling the qt window decorations makes a difference for you

you also better try with recent opencv 4.8 instead of 4.5.5, there was trouble with qt5 in the past and you’re probably missing resp. fixes

I wrote the “output.png” and it is identical to the input image.

The mouse pointer XY coordinates are not written over the image but rather to the window decoration below the image. I took a screenshot of original image in xv with the imread.py gui over it to show the window decoration below the image.

Where would the “WINDOW_NORMAL” flag be inserted into this code?

#!/usr/bin/python

import cv2 as cv
img = cv.imread("/home/brianp/bin/rad_weight.f.jpg")
    
cv.imshow("Brian's Python window", img)
#cv.imwrite("output.png", img)
k = cv.waitKey(0) # Wait for a keystroke in the window

I compiled the cpp version from latest code. It has exactly the same behavior and is running opencv 4.8.

Clicking the “X” icon in the upper left corner → more_actions → special_window_settings, the “window types” == “Normal Window”.

cpp.defect.xy.coords.truncated2
The exact same defect is shown in the CPP version compiled from opencv 4.8 source today.

great. so we know, it’s not imread()

sorry for getting this wrong (typo), add:

cv.namedWindow("Brian's Python window", cv.WINDOW_GUI_NORMAL)

before the imshow()

ok, so we must look for current github issues …

I added this before the imread(). It had 3 effects; The window was shrunk to postage stamp size, the window was made resizeable (so the decoration at the bottom was expandable) and the coordinate writing was disabled.

#!/usr/bin/python

import cv2 as cv
img = cv.imread("/home/brianp/bin/rad_weight.f.jpg")
    
cv.namedWindow("Brian's Python window", cv.WINDOW_GUI_NORMAL)    
cv.imshow("Brian's Python window", img)
#cv.imwrite("output.png", img)
k = cv.waitKey(0) # Wait for a keystroke in the window

OpenCV 4.8.0-dev is really broken compared to 4.5.5!

I altered the #include to point to 4.8 and altered the g++ to also point to 4.8. Then I verified the linking with ldd to show the libraries used like: libopencv_core.so.408 => /usr/local/lib64/libopencv_core.so.408 (-> libopencv_core.so.4.8.0)

Now, there is no decoration above or below and no XY coordinates are printed at all.

/*
 g++-10 -L/usr/local/lib64 -I/usr/local/include/opencv4/opencv2 \
 	-lopencv_imgcodecs -lopencv_core  -lopencv_highgui   \
    imread.cv.cpp -o imread

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib64

 ldd imread   | grep opencv        
libopencv_core.so.408 => 
	/usr/local/lib64/libopencv_core.so.408 (0x00007f73caf90000)
libopencv_highgui.so.408 => 
	/usr/local/lib64/libopencv_highgui.so.408 (0x00007f73caf52000)
libopencv_imgcodecs.so.408 => 
	/usr/local/lib64/libopencv_imgcodecs.so.408 (0x00007f73cbcdb000)
libopencv_imgproc.so.408 => 
	/usr/local/lib64/libopencv_imgproc.so.408 (0x00007f73c8f3b000)
	
*/
#include <iostream> 
// #include <opencv2/opencv.hpp> 
#include "/usr/local/include/opencv4/opencv2/opencv.hpp"
using namespace cv; 
using namespace std; 
  
// Driver code 
int main(int argc, char** argv)  { 
    // Read the image file 
    Mat image = imread(argv[1]); 
  
    // Error Handling 
    if (image.empty()) { 
        cout << "Image File Not Found" << endl; 
        cin.get();  // wait for any key press 
        return -1; 
    } 
  
    // Show Image inside a window with the name provided 
    imshow("Brian's Image Window", image); 
	cout << getBuildInformation() << endl; 
    waitKey(0);  // Wait for any keystroke 
    return 0; 
}

The getBuildInfo() dump verifies the version:
General configuration for OpenCV 4.8.0-dev ====

unrelated, but please use:

-I/usr/local/include/opencv4/

and

#include <opencv2/opencv.hpp> 

with c++

408 ?

so all expected, but did it still truncate the image, or not ?

They are all synonymous links of varying formats:
/usr/local/lib64/libopencv_core.so → libopencv_core.so.408 → libopencv_core.so.4.8.0

The Image is not truncated in 4.5 or 4.8. The XY coordinates written to the “shell” which was part of the gui window decorations above and below the jpg image. 4.5 shows whitespace above and below with the XY coordinates truncated in the bottom window and only whitespace in the top part. 4.8 shows no decorations at all, exactly the same thing that “xv” shows.

This is the code which was written half off of the gui in 4.5 and now is entirely off the window in 4.8.

# displaying the coordinates on the Shell 
        print(x, ' ', y)

python.defect.xy.coords.truncated2

On top is the 4.5 window showing the top half of the XY coords and the color. Below is the 4.8 behavior with no bottom decoration at all, just 100% image.

hmm, trying to recap, where we are now:

  • it’s NOT imread()
  • it’s NOT python
  • it’s NOT a specific opencv version
  • it does NOT truncate your image (!!!), but the statusbar below that (only top 50% of that visible)
  • it IS related to qt (and probably opensuse)

do you think, i’m correct about it ?

btw, do you see that buttonbar on top ?

The behavior is different between opencv 4.5 and 4.8, 4.5 is half truncated whereas 4.8 is fully truncated.

The idea was to show the XY coordinates wrt/ the image on the gui window while the mouse was moved.

It does not appear to be so. The QT, the Suse and the application code are constant.
The defect varies with the opencv libraries linked to. Using opencv 4.5 truncates half of the XY coordinates. Linking to opencv 4.8 truncates the XY coordinates entirely.

The workaround is to open a file and write the XY data there as part of the mouseClick event handler.

All of the buttons in the buttonbar are hidden (invisible entirely) and appear only when moused over. None do anything except the save_as button.