[Python] Read images with OpenCV (for beginners)

Introduction

Recently, Python has become a hot topic. In Python, you can perform simple image processing by using a library called OpenCV. In this article, I will explain how to read an image with OpenCV and process it in various ways.

environment

MacOS Mojave Python 3.7.6

Preparation

Install libraries called opencv and matplotlib. matplotlib is a library for drawing graphs and can also be used for displaying images. Type the following commands into the terminal and execute them.

$pip install opencv-python
$pip install matplotlib

Please note that you cannot install with "opencv" alone. If Requirement already satisfied is displayed and you enter, there is no problem because it is already installed.

Let's load the image

This time, I will use the image of the following cat (neko.jpg). neko.jpg

Execute the following code.

opencv_read_img.py


#Loading the library
import cv2
import matplotlib.pyplot as plt

#neko.Load a jpg and put it in an img object
img = cv2.imread("neko.jpg ")

#Display img object using matlotlib
plt.imshow(img)
plt.show()

The first import part loads opencv (cv2) and matplotlib's pyplot (matplotlib.pyplot). As plt means that matplotlib.pylot is long, so I will abbreviate it to plt and use it from now on.

If you execute the above code and the following screen appears, it is successful. neko_出力.png

However, the cat has a strange color, right? This is due to the different color representations of GBR (green / blue / red) in opencv and RGB (red / green / blue) in matplotlib. Therefore, in order to express with the original color, it is necessary to convert the color.

Color conversion method

After loading the image, use cv2.cvtColor ().

opencv_read_img.py


#Loading the library
import cv2
import matplotlib.pyplot as plt

#neko.Load a jpg and put it in an img object
img = cv2.imread("neko.jpg ")

#Convert image color order from BGR to RGB
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#Display img object using matlotlib
plt.imshow(img)
plt.show()

When the above code is executed, the following screen will be output. スクリーンショット 2020-03-22 20.07.54.png The original image is displayed! Next time, I will explain how to perform simple image processing and save it.

trouble shooting

python: can't open file 'opencv_read_img.py': [Errno 2] No such file or directory

This is an error that the file does not exist. In this case, follow the procedure below.

① Place the image in the same location as the created python file. 画像のフォルダ.png

② Move to the folder where the python file is placed in the terminal. If you have python files and images in a folder called qiita_python on your desktop, run the following command.

$cd /Users/username/Desktop/qiita_python
$python opencv_read_img.py

I'm on Twitter

Please follow me if you like! https://twitter.com/ryuji33722052

Recommended Posts

[Python] Read images with OpenCV (for beginners)
OpenCV for Python beginners
Load gif images with Python + OpenCV
INSERT into MySQL with Python [For beginners]
Amplify images for machine learning with python
Capturing images with Pupil, python and OpenCV
[python, openCV] base64 Face recognition with images
WebApi creation with Python (CRUD creation) For beginners
Read text in images with python OCR
[For beginners] Try web scraping with Python
python textbook for beginners
Memo # 4 for Python beginners to read "Detailed Python Grammar"
Causal reasoning and causal search with Python (for beginners)
Memo # 3 for Python beginners to read "Detailed Python Grammar"
Memo # 1 for Python beginners to read "Detailed Python Grammar"
~ Tips for Python beginners from Pythonista with love ① ~
[Python] Collect images with Icrawler for machine learning [1000 images]
Memo # 2 for Python beginners to read "Detailed Python Grammar"
Memo # 7 for Python beginners to read "Detailed Python Grammar"
Memo # 6 for Python beginners to read "Detailed Python Grammar"
~ Tips for Python beginners from Pythonista with love ② ~
Memo # 5 for Python beginners to read "Detailed Python Grammar"
[Introduction for beginners] Working with MySQL in Python
Read csv with python pandas
Bordering images with python Part 1
"Apple processing" with OpenCV3 + Python3
Learning flow for Python beginners
Image editing with python OpenCV
Camera capture with Python + OpenCV
[Python] Using OpenCV with Python (Basic)
OpenCV3 installation for Python3 @macOS
Python3 environment construction (for beginners)
Python #function 2 for super beginners
Face detection with Python + OpenCV
Basic Python grammar for beginners
100 Pandas knocks for Python beginners
Python for super beginners Python #functions 1
Python #list for super beginners
~ Tips for beginners to Python ③ ~
Using OpenCV with Python @Mac
I tried "morphology conversion" of images with Python + OpenCV
Read json data with python
Solve AtCoder Problems Boot camp for Beginners (Medium 100) with python
[Python] Accessing and cropping image pixels using OpenCV (for beginners)
Number recognition in images with Python
[Python] Using OpenCV with Python (Image Filtering)
Neural network with OpenCV 3 and Python 3
Python Exercise for Beginners # 2 [for Statement / While Statement]
[Python] Using OpenCV with Python (Image transformation)
Python for super beginners Python # dictionary type 1 for super beginners
[Python] Using OpenCV with Python (Edge Detection)
Getting Started with Python for PHPer-Classes
Python #index for super beginners, slices
Easy Python + OpenCV programming with Canopy
Post multiple Twitter images with python
[python] Read information with Redmine API
<For beginners> python library <For machine learning>
Animate multiple still images with Python
Try face recognition with python + OpenCV
Cut out face with Python + OpenCV
Face recognition with camera with opencv3 + python2.7