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
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.
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)
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)
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
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
When you try to understand an image using OpenCV, you cannot write a program without understanding how the image is understood by OpenCV.
The image is a 3D array of shapes (height, width, channels) https://teratail.com/questions/249839
Here, we will introduce two types, RGB and HSV.
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.
note: When the alpha channel is read, it is treated as a four-dimensional vector as BGRA.
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). 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.
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