While learning Pytorch, it is important to learn how to create or load data correctly first.

With data, a lot of functions, the effect of the operation becomes very intuitive.

This article focuses on using other libraries to read image files (learn this so you can visualize some of the effects later on)

Better article organization:

  • Github
  • Follow tuduisuinian and get the PyTorch tutorial PDF at the bottom of the menu

Zero: to prepare

Before loading data, learn the correct path method. There are many examples in the tutorial where no images are provided at the time of presentation, or the reader does not know how to modify the read path in the tutorial, discouraging enthusiasm.

** Suggestion: To make sure you can follow the tutorial step by step, there will be a sample image in the tutorial, I suggest you right-click – save as image, save the image to the folder where you run the application. ** As shown below:

One: Data set preparation

Task: We read the two images in different ways (remember to right-click – save as image, save to the program location, remember to rename the file the way you like, mine to 002.jpg and 003.jpg

After saving the file as and creating a new Python file, I looked like this:

Now we can do this in load_images.py.

Ii: Use other libraries to read image files

  • usematplotlibLibrary for image reading

The functions in Matplotlib are very similar to Matlab.

We need to use the function in matplotlib.pyplot:

Imread (file address) : The operation to read the image (parameter is the path to read the image file)

Imshow (array) : Perform image display operation (display array of images)

Show () : Displays a window for displaying images (many times you forget to use this function if you don’t display images)

We’re trying to talk about the type of data, the size and shape of the image that’s being read

import matplotlib.pyplot as plt

img = plt.imread('002.jpg')
The height H of the image is 460, the width W is 346, and the color channel C is 3
print(img.shape)
print(img.dtype)
print(type(img))
plt.imshow(img)
plt.show()
Copy the code

The output is:

(460, 346, 3)
uint8
<class 'numpy.ndarray'>
Copy the code

Conclusion:imreadThe picture read isnumpy.ndarryThe size of the array is: height × width × number of channels, and the data type of the array isuint8, that is, each data size is [0,255]

If you don’t want to manually read multiple images, you need to use Python files, path manipulation, etc. No introduction

If you add it manually, you join the arrays in the form [array1,array2]

import matplotlib.pyplot as plt

img1 = plt.imread('002.jpg')
img2 = plt.imread('003.jpg')
img = [img1, img2]
for i in img:
    plt.imshow(i)
    plt.show()
Copy the code

  • usecv2The image is read

In the CV2 library, you need to use the following functions:

Imread (file address) : Reads the file image at the address

Imshow (‘ window name ‘, image array) : Displays an array of images, but must be used in conjunction with waitKey() otherwise the image cannot be displayed

WaitKey (delay time) : The delay time needs to be set. When the delay time is less than or equal to 0, the window will be delayed indefinitely. Press any key to continue to execute the following program. When the delay time > 0, that is, the window image will display the corresponding milliseconds, automatically disappear.

import cv2
img1 = cv2.imread('002.jpg')
print(img1.shape)
print(img1.dtype)
print(type(img1))
cv2.imshow('img', img1)
cv2.waitKey(0)
Copy the code

The output is:

(460, 346, 3)
uint8
<class 'numpy.ndarray'>
Copy the code

Conclusion: the imreadThe picture read isnumpy.ndarryThe size of the array is: height × width × number of channels, and the data type of the array isUint8 ‘, i.e. the size of each data is [0,255]

Matplotlib and CV2 deal with the data format of images similarly, but there are also differences:

For channel reads, Cv2 is read in BGR order, while Matplotlib is read in RGB order


  • usePILLibrary for image processing

PIL is called Python Image Library. Python is a library for image processing.

Functions to use:

Open (file address) : Open the file, note that it is open, not read. The main function is to keep checking the file address and ensure that the file is open. When the image is processed, it is automatically loaded.

Show () : use the built-in image viewer of the system to view images

from PIL import Image
img1 = Image.open('002.jpg')
PIL has its own data structure, so there is no shape,dtype attribute
# print(img1.shape)
# print(img1.dtype)
print(type(img1))
img1.show()
Copy the code

The output is:

<class 'PIL.JpegImagePlugin.JpegImageFile'>
Copy the code

We can use the numpy.array() function to convert PIL structure data into numpy array.

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

img1 = Image.open('002.jpg')
img1 = np.array(img1)
print(img1.shape)
print(img1.dtype)
plt.imshow(img1)
plt.show()
Copy the code

Output:

(460.346.3)
uint8
Copy the code

As you can see, the data type of PIL converted to Numpy is uint8.


3:

This paper mainly introduces the use of Matplotlib, CV2,PIL library for image file reading

  • matplotlibIn theimread.imshow.showfunction
  • cv2In theimread.imshow.waitKeyfunction
  • PILIn theopen.showfunction