ITや趣味など気軽に投稿しています。

【Python】 Reading Images with OpenCV

What is OpenCV

OpenCV is an open-source image processing library developed and published by Intel. It is currently being developed and is available in a variety of languages.

Additionally, there are many programs available for object and person detection using OpenCV, which makes image recognition easy.

OpenCV is developed in C++, but it can be used with a variety of languages, including Python, Java, and JavaScript.

It also offers cross-platform support and can be used in various environments, including Windows, Linux, macOS, Android, and iOS.

OpenCV Installation

Note that, in the case of pip, it is OpenCV-Python, not OpenCV.

#pip
pip install opencv-python
pip install opencv-contrib-python

#Anaconda
conda install -c conda-forge opencv

The difference between OpenCV-Python and opencv-contrib is that OpenCV-Python only includes the main (core) module, whereas OpenCV-Contrib-Python includes the contrib (extra) module.

OpenCV Practice

Here is an example of working with OpenCV in Python.

Importing OpenCV

To import OpenCV, use the code below.

import cv2

Originally developed in C, it was called cv at the time. Later, it was rewritten in C++ and renamed cv2, which is still used today.

OpenCV 4 is currently the latest version, but it is still cv2. Apparently, the version is not the issue.

Let’s try loading an image. Use the cv2.imread() function to load the image.

import cv2

#read the image
img = cv2.imread("input/fox.jpg")

Color images are read as a three-dimensional array, while grayscale images are read as a two-dimensional array. The ‘mode’ can also be set to read color images as grayscale.

Additionally, OpenCV can currently read the following file formats using cv2.imread():

TypeExtension
Windows bitmap*.bmp, *.dib
JPEG*.jpeg, *.jpg, *.jpe
JPEG2000*.jp2
Portable Network Graphics*.png
Portable image format*.pbm, *.pgm, *.ppm
Sun rasters*.sr, *.ras
TIFF*.tiff, *.tif

mode

Images can be loaded in any format by specifying the mode.

First, the mode is specified as an argument of cv2.imread.
IMREAD_GRAYSCALE is one type of mode.

import cv2

img = cv2.imread("input/fox.jpg", cv2.IMREAD_GRAYSCALE)
mode説明
IMREAD_UNCHANGEDMaintain the color and accuracy of the image file as much as possible when importing it.
IMREAD_GRAYSCALEThe image is read as a single-channel grayscale image.
Color images are also read as single channels.
IMREAD_COLOR_BGRThe image is read as a three-channel color image.
Grayscale images are also read as three-channel images.

There are many more, but these three are a good place to start.