OpenCV 4.2 (on Ubuntu 20.04)で画像ファイル読み込み

OpenCV 4.2 (on Ubuntu 20.04)で画像ファイル読み込み

  • ひさびさにインストールしたので手順を忘れていた。docker化したい

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()
view raw read_image.py hosted with ❤ by GitHub

On Ubuntu 20.04 it read image and display by usin…

  • インストールはこれ

On Ubuntu 20.04 it install OpenCV 4.2.0

Preparation

sharo$kirima:~$ sudo apt update
sharo$kirima:~$ sudo apt upgrade -y
sharo$kirima:~$ sudo apt install -y build-essential

Install OpenCV-Python in Ubuntu

Ref. https://docs.opencv.org/3.4/d2/de6/tutorial_py_setup_in_ubuntu.html

sharo$kirima:~$ sudo apt install -y python3-opencv

It changed to python3-opencv from python-opencv.

Check OpenCV-Python

sharo@kirima:~$ python3
Python 3.8.2 (default, Apr 27 2020, 15:53:34) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2 as cv
>>> print(cv.__version__)
4.2.0
>>> exit()

On Ubuntu 20.04 it install OpenCV 4.2.0

Related.