Skip to content

Instantly share code, notes, and snippets.

@takurx
Created June 13, 2022 02:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takurx/55bde7a2e2834117763fe404d807dad4 to your computer and use it in GitHub Desktop.
Save takurx/55bde7a2e2834117763fe404d807dad4 to your computer and use it in GitHub Desktop.
On Ubuntu 20.04 it read image and display by using OpenCV 4.2

log read image

install opencv 4.2, matplotlib

sudo apt update
sudo apt -y upgrade
sudo apt install -y build-essential
sudo apt install -y python3-opencv
sudo apt install -y python3-matplotlib

error about missing opencv

chino@kafu:~$ python3 ./read_image.py 
Traceback (most recent call last):
  File "./read_image.py", line 4, in <module>
    import cv2
ModuleNotFoundError: No module named 'cv2'

error about missing matplotlib

matplotlib also can install through pip3.

chino@kafu:~$ python3 ./read_image.py 
Traceback (most recent call last):
  File "./read_image.py", line 5, in <module>
    from matplotlib import pyplot as plt
ModuleNotFoundError: No module named 'matplotlib'

error about missing image path

Traceback (most recent call last):
  File "./read_image.py", line 10, in <module>
    img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.error: OpenCV(4.2.0) ../modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'
#!/usr/bin/env python3
# read_image.py : test for reading image
import cv2
from matplotlib import pyplot as plt
#Read the image
filename1 = "/home/chino/sample.png" #absolute path
img = cv2.imread(filename1, 1)
#plt.imshow(img)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment