[PYTHON] Image recognition environment construction and basics

In order to initialize the PC frequently, I will leave the environment construction for image recognition as a memorandum for myself (it may be quite forgetful because I usually do other work). We will build an environment that can perform simple image recognition processing with OpenCV with python using miniconda.

Overview


Environment construction with miniconda

Download miniconda

Anaconda is fine, but if you have a low-end PC, we recommend miniconda. Because there are few packages installed by default and it does not take up space. https://docs.conda.io/en/latest/miniconda.html It's OK if you proceed to the next. No special settings are required.


Create a Python virtual environment

AnacondaPronpt


(base) >conda create -n py36 python=3.6                          …(1)
(base) >conda env list                                           …(2)
# conda environments:
#
base                  *  C:\Users\XXXX\miniconda3
py36                     C:\Users\XXXX\miniconda3\envs\py36
(base) >activate py36                                            …(3)
(py36) >conda env list                                           …(4)
# conda environments:
#
base                     C:\Users\XXXX\miniconda3
py36                  *  C:\Users\XXXX\miniconda3\envs\py36
(py36) >conda install jupyter notebbok                           …(5)

(1) Create a virtual environment of python3.6 with the name py36 (2) Check if a virtual environment has been created (3) Since you are currently in the base (initial setting) environment, switch to the virtual environment (py36) created earlier. (4) Switching is OK if the parentheses are replaced with (py36) and * is replaced with py36. (5) Install jupyter notebook

Recently I use jupter notebook. I think you can create spyder with a similar feeling. For machine learning, I think that python ver is about 3.6. It often doesn't work with the latest version. (May change in the future)


Installation of packages used for image recognition

AnacondaPronpt


(py36) >conda install numpy
(py36) >conda install matplotlib
(py36) >conda install opencv
(py36) >jupyter notebook

Start jupyter notebook and check if each package is installed

AnacondaPronpt


(py36) >jupyter notebook

JupyterNotebook


import cv2
import numpy  as np
import matplotlib as plt
print (cv2.__version__)
print (np.__version__)
print (plt.__version__)

It's OK if each version is returned. (Pandas, Keras, scikit-learn, TensorFlor, etc. are also recommended if you are personally interested in machine learning and analysis)


Quiet talk: OpenCV story

Let's install OpenCV, the de facto standard for image recognition. Currently OpenCV supports various programming languages such as C ++, Python and Java.

There is a story about image recognition in the first place, but for the sake of simplicity, we will consider it as follows.

It is easy to distinguish things by color and shape alone, but if the sunlight is different, the colors that can be seen will be different, and even if things are cut off, why can we recognize things? Unknowingly captures multiple features and understands according to the law.

Like other tools, OpenCV has tutorials. The official documentation is important, but this tutorial is easy to understand. (Personally, it's several times easier to understand than the Django tutorial) </ font> http://labs.eecs.tottori-u.ac.jp/sd/Member/oyamada/OpenCV/html/py_tutorials/py_tutorials.html


Try using OpenCV

First, let's load the image and display it. Place the image file (opencv-logo.png) in the directory where you want to save the program in advance.

JupyterNotebook


import cv2
img = cv2.imread('./opencv-logo.png',1) 
#The second argument is 0:Grayscale, 1:Color image-1:Color + transparency(α)
cv2.imshow('image',img)
#Display image in Window
#The first argument is the window name specified as a character string type,The second argument is the image you want to display
cv2.waitKey(0)
#Wait for key input
#The argument is the input wait time. 0 is unlimited
cv2.destroyAllWindows()
#close the window

Warning If the image file path is incorrect, no error will be returned, but print img and the command will display None. http://labs.eecs.tottori-u.ac.jp/sd/Member/oyamada/OpenCV/html/py_tutorials/py_gui/py_image_display/py_image_display.html#display-image


Deepen your understanding of images

When you try to understand an image using OpenCV, you cannot write a program without understanding how the image is understood by OpenCV.

How to read image data

img.png

The image is a 3D array of shapes (height, width, channels) https://teratail.com/questions/249839


How to express color

Here, we will introduce two types, RGB and HSV.

  • RGB (* Note that in OpenCV, the order is BGR)

RGB is commonly used to represent additive mixing. RGB is an acronym for red, green, and blue, respectively. It is the three primary colors of light, and it becomes whiter as the value increases. On the contrary, it becomes black as the number is reduced. It is this RGB that is also used in computer monitors. RGB.png

note: When the alpha channel is read, it is treated as a four-dimensional vector as BGRA.

  • HSV

HSV is used for drawing pictures on a computer and as a color swatch. This is because colors are more natural and intuitive than additive or subtractive mixing when considered in terms of hue (hue) and saturation. HSV includes hue, saturation, and value. Also called HSB (hue, saturation, brightness). HSV.jpg https://ja.wikipedia.org/wiki/%E8%89%B2%E7%A9%BA%E9%96%93

By properly using RGB and HSV, we will try to obtain the necessary information from the image data.


How to access and change pixel values

python


import cv2
import numpy as np
img= cv2.imread('RGB.png')
px = img[100,100]
#100,Data at 100 pixels (BGR)
print (px)

blue =img[100,100,0]
#100,0 channels on 100 pixels(B)data from
print (blue)

#It is also possible to change the pixel value using this
img[100,100] = [255,255,255]

The pixel value of the BGR image is an array of blue, green, and red color component values, and the pixel value of the grayscale image returns the brightness value.


It will be updated

Recommended Posts

Image recognition environment construction and basics
CNN 1 Image Recognition Basics
Python environment construction and TensorFlow
NumPy and matplotlib environment construction
Environment construction of python and opencv
Image recognition
Environment construction with pyenv and pyenv-virtualenv
Python: Basics of image recognition using CNN
Python and machine learning environment construction (macOS)
Django environment construction
DeepIE3D environment construction
Emacs-based environment construction
Linux environment construction
Python environment construction
Environment construction (python)
django environment construction
CodeIgniter environment construction
python environment construction
Python --Environment construction
Python environment construction
Golang environment construction
python environment construction
Word2vec environment construction
Python3 TensorFlow environment construction (Mac and pyenv virtualenv)
MacOS 10.11 environment construction: Powerline with Anaconda and Dein.vim
Installation of Python3 and Flask [Environment construction summary]
Pillow environment construction --For Docker + iPython (and OpenCV)
[Django3] Environment construction and various settings summary [Python3]
Environment construction: GCP + Docker
Django project environment construction
Image recognition with keras
Go language environment construction
ConoHa environment construction memo
homebrew python environment construction
PyData related environment construction
Anaconda-4.2.0-python3 environment construction (Mac)
Python development environment construction
YOLO v4 environment construction ①
Pepper Tutorial (7): Image Recognition
pyenv + fish environment construction
Perceptron basics and implementation
python2.7 development environment construction
BigGorilla environment construction memo
grip environment construction onCentOS6.5
Anaconda environment construction memo
Golang environment construction [goenv]
Mac environment construction Python
Pyxel environment construction (Mac)
Python environment construction @ Win7
Raspberry Pi + python + IoT device, environment construction procedure to start image processing and machine learning
Tree disease determination by image recognition using CNTK and SVM
Environment construction, simple confirmation and skill test for each language
Build GPU environment with GCP and kaggle official image (docker)
Similar face image detection using face recognition and PCA and K-means clustering
[Ubuntu 18.04] Tensorflow 2.0.0-GPU environment construction
PyTorch C ++ (LibTorch) environment construction
Anaconda environment construction on CentOS7
Django development environment construction memo
Deep learning image recognition 1 theory
First LAMP environment construction (Linux)
Python environment construction (Windows10 + Emacs)