Machine Learning 0008 How to read image data

Machine Learning 0008 Reading Image Data


        Most machine learning beginners have such questions, how to read image data? I used to have too. Before starting machine learning, I was doing simple image processing. The main task is to write the vast majority of algorithms in photoshop: brightness, blurring, blending methods, calculating image edges, etc. I already know a lot about images, but I still don't know what to do when using Python. Generally, in many examples of machine learning, the data has been given, run a run, see the result, and then there is no more... Because of my laziness, I have not searched what should I do? A few days ago, I suddenly wanted to use python to process images and play with it. Discovery is easy. Let me share it with you to save human time as a whole:


1. Update pip and install the toolkit. There are three toolkits pillow, scipy and numpy if you have it on the machine, don't run the install command again

python3.X:

pip3 install --upgrade pip

pip3 install Pillow

pip3 install scipy

pip3 install numpy


python2.X:

pip install --upgrade pip

pip install Pillow

pip install scipy

pip install numpy



2. Look at the code below

import matplotlib.pyplot as plt
import numpy as np
from PIL import Image

#Open the image and convert it to an array
img=np.array(Image.open("./aaa.jpg"),dtype=np.int32)
print(np.shape(img))#(800, 1280, 3) The height, width, channel rgb of the image

The img in the above code is an array, which should be accepted by most machine learning frameworks.

You can also do some processing on the data: img=(img-128)/128 maps the image to [-1,1)

You can take one of the color channels R: img=img[:,:,0].











Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325721207&siteId=291194637