imshow() Python/macOS not showing the image
I'm literally just starting out, but already stuck .. I'm trying to get an image to display, but imshow()
only opens an empty window. I don't get any errors, and the program doesn't hang (pressing a key closes the window just fine) - it's just that there is no image in the window.
I know that the image is loaded correctly, because I'm printing out the arrays and image data looks right. Also I can see that the window always has the correct _measurements_ for whichever image file I'm picking.
import numpy as np
import cv2
img = cv2.imread('butterfly.jpg', 0)
print(img)
cv2.imshow('Butterfly', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Things I have tried (to no avail):
- using an absolute path instead
- adding
cv2.startWindowThread(); cv2.namedWindow('Butterfly')
before theimshow()
command - reading lots of posts with similar problems
I'm using Python3.6, on macOS (Mojave), cv2 v.4.0.1
Any ideas what I might be doing wrong?
-- Edit --
print(cv2.getBuildInformation())
gives me this:
General configuration for OpenCV 4.0.1 =====================================
Version control: unknown
Extra modules:
Location (extra): /tmp/opencv-20190105-31032-o160to/opencv-4.0.1/opencv_contrib/modules
Version control (extra): unknown
Platform:
Timestamp: 2019-01-05T01:41:15Z
Host: Darwin 18.2.0 x86_64
CMake: 3.13.2
CMake generator: Unix Makefiles
CMake build tool: /usr/local/Homebrew/Library/Homebrew/shims/mac/super/gmake
Configuration: Release
CPU/HW features:
Baseline: SSE SSE2 SSE3 SSSE3 SSE4_1 POPCNT SSE4_2
requested: DETECT
disabled: SSE4_1 SSE4_2 AVX AVX2
Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
SSE4_1 (0 files): +
SSE4_2 (0 files): +
FP16 (0 files): + FP16 AVX
AVX (4 files): + AVX
AVX2 (11 files): + FP16 FMA3 AVX AVX2
AVX512_SKX (1 files): + FP16 FMA3 AVX AVX2 AVX_512F AVX512_SKX
C/C++:
Built as dynamic libs?: YES
C++ Compiler: /usr/local/Homebrew/Library/Homebrew/shims/mac/super/clang++ (ver 10.0.0.10001145)
C++ flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -DNDEBUG -DNDEBUG
C++ flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
C Compiler: /usr/local/Homebrew/Library/Homebrew/shims/mac/super/clang
C flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -DNDEBUG -DNDEBUG
C flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden ...
can you check if image is None ?
Thanks for looking into this! But no, it's not None:
print(img)
gives me the correct array, and I can even docv2.imwrite('butterfly_bw.jpg',img)
and get a saved copy of the bw version.can you add cv2.namedWindow('Butterfly',cv2.WINDOW_NORMAL) before imshow ?
Does it work in C++?
can you add to your question print(cv2.getBuildInformation())
hm, that doesn't make any difference either ... :-( Sorry - overlooked the C++ question - idk ...
there is a close issue here. is it same problem
got the cv2.getBuildInformation()) ... but it's a lot ... is there a better way than pasting the whole lot in a comment here? (sorry - forum newb)
^^ I _think_ that issue is different as they only see the window title bar, and it seemed to have been related to problems getting window dimensions. I do get a window with the correct dimensions, just empty
you can add to your question and select text and apply icon 101à10
can you use opencv-python 3.3
It seems you build yourself opencv. I don't understand I'm using 'Python3.6, on macOS (Mojave), cv2 v.4.0.1' but in build I can read Interpreter: /usr/local/opt/python/bin/python3 (ver 3.7.2)
Er, yes, I did have some weird issues during installation ... I installed with 'brew install opencv' and it got installed into /usr/local/lib/python3.7/site-packages and but when I start
python3
it runs Python 3.6 and I didn't know how to start Python 3.7, so I created a symbolic link to /usr/local/opt/opencv/lib/python3.7/site-packages/cv2/python-3.7/cv2.cpython-37m-darwin.so ... (sorry, completely forgot about that once it seemed to be working!) - So I guess I should try to solve that problem some other way ...I must say that I don't know Mac install procedure. Using cmake you can set python path cmake -D