当前位置:网站首页>Foundation: 2 The essence of image
Foundation: 2 The essence of image
2022-07-01 08:36:00 【Mission Hills for the rest of my life】
What is the essence of images ?–numpy And image Basics
Use jupyterLab Carry out preliminary study , First, install the software . As shown in the figure below :
Installation steps
1. Create a python A virtual environment
conda create --name demo_py3.8 python=3.8
2. Install according to the source change .

3. function jupyter-lab, open , Then the URL will pop up .
http://localhost:8888/lab
Learn to use numpy
# In order to understand the essence of image , We need to understand the concepts of arrays and matrices first
# First of all, remember , How to create a list
list_a = [1,2,3,4,5]
# Use type Command to view data types , yes list type
type(list_a)
# Create another list , Make the elements of this list still be lists
list_b = [ [1,2], [3,4], [5,6] ]
# Print list [[1, 2], [3, 4], [5, 6]]
list_b
# By looking for a numerical index , Print page list_b The first 2 The number of elements 1 Elements 3
list_b[1][0]
# This structure is also called array , such as list_a It's a one-dimensional array ,list_b Is a two-dimensional array
# For more efficient processing of arrays , We often use numpy In this bag
# First, import. numpy package , Rename it
import numpy as np
# that numpy How to create an array ?
# It can be used Python List direct conversion
# First create a Python list
list_c = [1,2,3,4]
# In the use of np.array() take Python The list is converted to numpy Array of numpy.ndarray
my_array = np.array(list_c)
# A print array([1, 2, 3, 4])
my_array
# that numpy There are also some built-in functions that can quickly create arrays
# For example, we use np.arange() You can quickly create an array of consecutive numbers
# For example, I create a 0~9 One dimensional array of array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.arange(0,10)
# jupyterlab Use in shift+tab You can view the help document of the function ( Check it out. , Input now np.arange(), Pop up corresponding function help document )
# You can see this arange() Function has start、stop and step Parameters , Each represents the starting value , Termination value , And step size
# If I want to create 0~10 An array of consecutive even numbers in , Just set the step size to 2( Input now np.arange(0,10,2))
# array([0, 2, 4, 6, 8])
np.arange(0,10,2)
# You can also use np.ones It's all about 1 Array of ,( When the input np.ones(), Pop up help notes )
# For example, I want to create a size of 3*3 Of all the 1 Array
np.ones(shape=(3,3))
# Create another one with the size 10*3 Of all the 1 Array
# You can see 10 It's the number of lines ,3 It's the number of columns
np.ones(shape=(10,3))
# Or use np.zeros() whole 0 Array
# For example, the creation size is 5*5 Of all the 0 Array
np.zeros(shape=(5,5))
# use first np.randint Function some random integers array([53, 86, 55, 17, 87, 12, 28, 95, 2, 44])
arr = np.random.randint(0,100,10)
# Use max Get the maximum
arr.max()
# Reuse argmax() Get the index of the maximum value
arr.argmax()
# Use min Function to get the minimum value
arr.min()
# Use argmin Get the minimum index
arr.argmin()
# Use mean() Method to get the average value
arr.mean()
# If you want to get humpy Size of array , Use numpy.shape, (10,)
arr.shape
# You can also use reshape Function to convert the shape of an array , For example, I will arr convert to 5*2 Array of
arr.reshape((5,2))
# And then change into 2 That's ok 5 Column
arr.reshape((2,5))
# If you try to deform to a size of 2*10 Well ? Will report a mistake :ValueError: cannot reshape array of size 10 into shape (2,10)
arr.reshape((2,10))
# That two-dimensional array , We also call it matrix in mathematics , Let's see numpy Operations on matrices
# First create a 10*10 Matrix
matrix = np.arange(0,100).reshape((10,10))
# Check the size :(10, 10)
matrix.shape
# Use the index method in brackets , Get the corresponding elements of the matrix , For example, I got the No 3 Xing di 5 Column elements
matrix[2,4]
# If you want to get all the elements in a row , We need to use numpy The section of :
# For example, I want to get the number 3 Row all elements , Just change the second position to a colon :
matrix[2,:]
# Allied , For example, I want to get the number 6 Column all elements , Just change the first position to a colon :
matrix[:,5]
# For example, I want to get the number 1~3 That's ok , The first 2~4 Column matrices , We can use numbers and colons to get
matrix[0:3,1:4]
# Of course, we can use the equal sign assignment statement , For example, I assign these positions 0
matrix[0:3,1:4] = 0
# good , That's all numpy Operation usage of arrays and matrices
A two-dimensional matrix is first and last , You can compare and understand why the image is calculated high first , In calculating the width , The essence is a three-dimensional matrix .
The cat figure understands the image
# First, import. numpy
import numpy as np
# In order to be in notebook Show pictures in , Import matplotlib library
import matplotlib.pyplot as plt
# Add this line to Notebook Display images
%matplotlib inline
# Use one more PIL library , For reading images
from PIL import Image
# I am here img There is a picture under the folder ( Show me )
# We use it PIL Library reads images , Pay attention to the correct path
img = Image.open('./img/cat.jpg')
# Display images
img

# Look at the types of variables :PIL.JpegImagePlugin.JpegImageFile
type(img)
# You can see that this is not numpy Array format of , that numpy Can't handle it yet
# First we need to translate it into numpy Array , Use numpy.asarray() function
img_arr = np.asarray(img)
# View type :numpy.ndarray
type(img_arr)
# Let's look at the size :(1253, 1880, 3) Height , Width ,RGB Three channels
img_arr.shape
# Reuse matplot Of imshow() Method display Numpy An array of pictures
plt.imshow(img_arr)
# You can see that the abscissa and ordinate show that the length of the picture is 1800 many , Height is 1200 many

# Let's continue to operate on this picture , First use numpy Of copy Method to copy an original drawing
img_arr_copy = img_arr.copy()
# Check the size :(1253, 1880, 3)
img_arr_copy.shape
# use first numpy section , take R,G,B Of the three color channels R The red channel is displayed
plt.imshow(img_arr_copy[:,:,0])

# You will find this color very strange , They are all emerald green , Why does it appear like this ?
# We turn on matplot About the color table colormap Explanation :
# https://matplotlib.org/stable/gallery/color/colormap_reference.html
# You can see the default color : It's Emerald (viridis ). This color is convenient for color blind to watch
# We can also put cmap The color is set to volcanic magma style :magma
plt.imshow(img_arr_copy[:,:,0],cmap='magma')

# Let's print the red R An array of channels
img_arr_copy[:,:,0]
# good , We know , The computer can't tell which channel is red , Each color channel is actually a grayscale image , Let's start with cmap The color is set to gray Grayscale
# to glance at
plt.imshow(img_arr_copy[:,:,0],cmap='gray')

# We know , In the red channel 0 There is no red , It's pure black , And the closer it gets 255 Well , It means the more popular ,255 Just pure red
# Look at this grayscale image , The lighter the color , It means the more popular it is here
# We can take a look at the grayscale image of the red channel. The lightest color is this pendant ( Mouse indication )
# That goes back to the original color picture , You can see that this pendant is indeed the most popular
# Allied , We also show the green channel in grayscale mode
plt.imshow(img_arr_copy[:,:,1],cmap='gray')



边栏推荐
- Review of week 280 of leetcode
- Conception et mise en service du processeur - chapitre 4 tâches pratiques
- Yolov5进阶之六目标追踪环境搭建
- MATLAB【函数和图像】
- In depth learning training sample amplification and tag name modification
- 2022 mechanical fitter (primary) examination summary and mechanical fitter (primary) reexamination examination
- Leetcode t29: divide two numbers
- C语言指针的进阶(下)
- Centos7 shell脚本一键安装jdk、mongo、kafka、ftp、postgresql、postgis、pgrouting
- Yolov5 advanced six target tracking environment construction
猜你喜欢

【C】 Summary of wrong questions in winter vacation

Adding color blocks to Seaborn clustermap matrix

shardingSphere

5mo3 UHI HII HII 17mn4 19Mn6 executive standard

Agrometeorological environment monitoring system

机动目标跟踪——当前统计模型(CS模型)扩展卡尔曼滤波/无迹卡尔曼滤波 matlab实现

CPU設計實戰-第四章實踐任務一簡單CPU參考設計調試

3、Modbus通讯协议详解

Maneuvering target tracking -- current statistical model (CS model) extended Kalman filter / unscented Kalman filter matlab implementation

Using settoolkit to forge sites to steal user information
随机推荐
Leetcode t34: find the first and last positions of elements in a sorted array
《微机原理》—总线及其形成
yolov5训练可视化指标的含义
Qt的模型与视图
Yolov5 advanced six target tracking environment construction
Mavros sends a custom topic message to Px4
factory type_id::create过程解析
2022.2.15
串口转WIFI模块通信
MAVROS发送自定义话题消息给PX4
Introduction to R language
P4 installation bmv2 detailed tutorial
SPL installation and basic use (II)
《单片机原理及应用》—定时器、串行通信和中断系统
Internet of things technology is widely used to promote intelligent water automation management
1.jetson与摄像头的对接
你了解数据是如何存储的吗?(C整型和浮点型两类)
The use of word in graduation thesis
2022 ordinary scaffolder (special type of construction work) examination question bank and the latest analysis of ordinary scaffolder (special type of construction work)
Use threejs simple Web3D effect