当前位置:网站首页>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')



边栏推荐
- Pipeline detection of UAV Based on gazebo
- Internet of things technology is widely used to promote intelligent water automation management
- 毕业论文中word的使用1-代码域标公式
- MATLAB小技巧(16)矩阵特征向量特征值求解一致性验证--层次分析
- Configuration and startup of Chang'an chain synchronization node
- Mavros sends a custom topic message to Px4
- 2022 examination summary of quality controller civil engineering direction post skills (quality controller) and reexamination examination of quality controller civil engineering direction post skills
- SPL-介绍(一)
- CPU设计实战-第四章实践任务一简单CPU参考设计调试
- Stack implementation calculator
猜你喜欢

使用beef劫持用戶瀏覽器

Vscode customize the color of each area

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

Internet of things technology is widely used to promote intelligent water automation management

01 numpy introduction

MATLAB小技巧(23)矩阵分析--模拟退火

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

Burpsuite -- brute force cracking of intruder

factory type_ Id:: create process resolution

TypeError: __init__() got an unexpected keyword argument ‘autocompletion‘
随机推荐
我想知道手机注册股票开户的流程?另外,手机开户安全么?
What is the material of 16mo3 steel plate? What is the difference between 16mo3 and Q345R?
【C】 Summary of wrong questions in winter vacation
Review of week 280 of leetcode
一文纵览主流 NFT 市场平台版税、服务费设计
Codeworks round 803 (Div. 2) VP supplement
Gateway-88
电脑小技巧
[deep analysis of C language] - data storage in memory
Yolov5 advanced 7 target tracking latest environment setup
The era of low threshold programmers is gone forever behind the sharp increase in the number of school recruitment for Internet companies
2022 ordinary scaffolder (special type of construction work) examination question bank and the latest analysis of ordinary scaffolder (special type of construction work)
网关gateway-88
Leetcode T39: 组合总和
【js逆向】md5加密参数破解
Thread safety analysis of [concurrent programming JUC] variables
Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
MD文档中插入数学公式,Typora中插入数学公式
Airsim radar camera fusion to generate color point cloud
避免按钮重复点击的小工具bimianchongfu.queren()