本文主要介绍numpy在数字图像处理中的应用,其中包括:矩阵创建、矩阵转换、基本操作、矩阵运算、元素获取、读取显示图像、简单绘图、
文章目录
三个重要属性
A.dtype, A.shape, A.ndim
首先写一个读取灰色or彩色图像的函数
def show(img):
if img.ndim == 2:
plt.imshow(img, cmap='gray')
else:
plt.imshow(cv.cvtColor(img,cv.COLOR_RGB2BGR))
plt.plot()
apple = cv.imread('./pic/apple.jpg')
show(apple)

apple = cv.imread('./pic/apple.jpg',0)
show(apple)

创建矩阵
np.array([[1]])
np.uint8([1])
np.arange(2,10,2)
np.linspace(0, 2*pi, 100)
A = np.zeros((4,3), dtype=np.uint8)
B = np.ones((2,2), dtype=np.float32)
I = np.eye(4)
I2 = np.identity(6)
C = np.random.randint(0, 10, (4,4))
矩阵转换
A.reshape()
A.flatten(), A.ravel()
A.T
A.transpose()
np.hstack([A, B])
最大值、最小值、平均值
A.max()
A.min()
A.mean()
np.max()
np.min()
np.mean()
数学运算
np.power(A, 2)
np.sqrt()
np.log(), np.log2(), np.log10()
A.dot(x)
图片读写及显示
cv.imread(path)
cv.imwrite(path, img)
cv.imshow("roi", img)
cv.waitKey(0)
cv.destroyAllWindows()
plt.imshow(img)
plt.show()
简单绘图
plt.plot(x,y)
plt.hist(array)
import numpy as np
在矩阵中重要的三个属性
A = np.random.randint(0,9,(3,3))
print('A.dtype =', A.dtype)
print('A.ndim =', A.shape)
print('A.ndim =', A.ndim)
A1 = A.reshape((1,9))
print(A1)
A1 = A1.astype(np.float32)
A.dtype = int32
A.ndim = (3, 3)
A.ndim = 2
[[2 2 0 7 8 1 5 7 4]]
矩阵创建
np.array([[1]])
np.uint8([1])
np.arange(2,10,2)
np.linspace(0, 2*pi, 100)
np.zeros((4,3), dtype=np.uint8)
np.ones((2,2), dtype=np.float32)
np.eye(4)
np.identity(6)
np.random.randint(0, 10, (4,4))
让我们以一种简单的方式使用这些函数。
[En]
Let’s use these functions in a simple way.
x = np.array([1,2,3],dtype=float)
print(x)
print(x.dtype)
[1. 2. 3.]
float64
y = np.uint8([1,1,1])
print(y)
print(y.dtype)
[1 1 1]
uint8
np.arange(2,10,2)
array([2, 4, 6, 8])
np.linspace(0,10,11)
array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
创建全0矩阵
np.zeros((2,3),dtype = int)
array([[0, 0, 0],
[0, 0, 0]])
创建全1矩阵
np.ones((3,3),dtype=np.float32)
array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=float32)
创建单位矩阵
np.identity(4)
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
创建随机数
np.random.randint(0,256,(10,10),dtype=np.uint8)
array([[ 38, 240, 221, 175, 205, 32, 142, 207, 183, 215],
[ 23, 200, 108, 171, 101, 213, 182, 141, 220, 144],
[103, 112, 187, 29, 20, 107, 173, 236, 192, 160],
[ 76, 196, 71, 3, 217, 169, 7, 127, 74, 223],
[ 15, 75, 177, 181, 175, 17, 148, 82, 48, 246],
[228, 41, 161, 228, 5, 154, 205, 0, 103, 5],
[240, 227, 231, 217, 213, 218, 28, 0, 90, 156],
[118, 140, 93, 107, 43, 90, 54, 78, 233, 233],
[ 92, 16, 111, 253, 137, 87, 55, 166, 72, 63],
[127, 95, 148, 7, 70, 56, 91, 26, 184, 112]], dtype=uint8)
矩阵转换
A.reshape()
A.flatten(), A.ravel()
A.T
A.transpose()
np.hstack([A, B])
A = np.ones((3,3),dtype=np.uint8)
print(A)
[[1 1 1]
[1 1 1]
[1 1 1]]
reshape函数是numpy中一个很常用的函数,作用是在不改变矩阵的数值的前提下修改矩阵的形状
print(A.reshape((1,9)))
print(A.reshape((-1,9)))
[[1 1 1 1 1 1 1 1 1]]
[[1 1 1 1 1 1 1 1 1]]
A.T or A.transpose()将这个矩阵装置
B = np.random.randint(0,10,(3,3))
print(B)
print(B.T)
print(B.transpose())
[[4 8 5]
[0 5 9]
[3 1 1]]
[[4 0 3]
[8 5 1]
[5 9 1]]
[[4 0 3]
[8 5 1]
[5 9 1]]
hstack()是横向拉伸,源于horizontal,横向这个词 vstack()是纵向排列,源于vertical纵向
print(np.hstack([A,B]))
[[1 1 1 4 8 5]
[1 1 1 0 5 9]
[1 1 1 3 1 1]]
print(np.vstack([A,B]))
[[1 1 1]
[1 1 1]
[1 1 1]
[4 8 5]
[0 5 9]
[3 1 1]]
基本操作
B = A + 2
C = A / 2
print(A)
print(B)
print(C)
[[1 1 1]
[1 1 1]
[1 1 1]]
[[3 3 3]
[3 3 3]
[3 3 3]]
[[0.5 0.5 0.5]
[0.5 0.5 0.5]
[0.5 0.5 0.5]]
B = np.power(A, 0.5)
C = np.sqrt(A)
D = np.log(A)
print(B)
print(C)
print(D)
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
axis的重点在于方向,而不是行和列。1表示横轴,方向从左到右;0表示纵轴,方向从上到下。
ma1 = A.max()
ma2 = np.max(A)
mi1 = A.min(axis=0)
mi2 = np.min(A, axis=0)
me1 = A.mean(axis=1)
me2 = np.mean(A, axis=1)
print(A)
print(A.T)
print(A.transpose())
print(np.transpose)
[[1 1 1]
[1 1 1]
[1 1 1]]
[[1 1 1]
[1 1 1]
[1 1 1]]
[[1 1 1]
[1 1 1]
[1 1 1]]
<function transpose at 0x00000193da314940>
</function>
矩阵运算
x = np.array([1, 1, 2])
z1 = A * x
z2 = A @ x
z3 = A.dot(x)
print(z1)
print(z2)
print(z3)
[[1 1 2]
[1 1 2]
[1 1 2]]
[4 4 4]
[4 4 4]
B = np.ones((3,3), dtype=int)
Z1 = A * B
Z2 = A @ B
Z3 = A.dot(B)
print(Z1)
print(Z2)
print(Z3)
[[1 1 1]
[1 1 1]
[1 1 1]]
[[3 3 3]
[3 3 3]
[3 3 3]]
[[3 3 3]
[3 3 3]
[3 3 3]]
元素获取
u = np.arange(2, 10, 2)
print(u)
print(u[0], u[1:4])
[2 4 6 8]
2 [4 6 8]
print(A)
print(A[0])
print(A[0, :])
[[1 1 1]
[1 1 1]
[1 1 1]]
[1 1 1]
[1 1 1]
print(A[:, 0])
[1 1 1]
E = np.random.randint(0, 10, (5,5), dtype=int)
E
array([[5, 4, 9, 1, 7],
[9, 8, 6, 2, 1],
[1, 5, 2, 1, 1],
[5, 4, 6, 0, 4],
[4, 1, 4, 2, 5]])
E1 = E[0:3, 0:3].copy()
print(E1)
[[5 4 9]
[9 8 6]
[1 5 2]]
e1 = E[0][0]
e2 = E[0, 0]
print(e1, e2)
5 5
E1[0,0] = 0
print(E1)
[[0 4 9]
[9 8 6]
[1 5 2]]
E1[0,:] = np.array([1,1,1])
print(E1)
[[1 1 1]
[9 8 6]
[1 5 2]]
E1 > 2
array([[False, False, False],
[ True, True, True],
[False, True, False]])
E1.flatten()
array([1, 1, 1, 9, 8, 6, 1, 5, 2])
读取显示图像
import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
img1 = cv.imread('./pic/cat500x480.jpg')
cv.imshow("bear", img1)
cv.waitKey(0)
cv.destroyAllWindows()
plt.imshow(img1)
plt.show()

img1 = cv.cvtColor(img1, cv.COLOR_BGR2RGB)
plt.imshow(img1)
plt.show()

cv.imread()读取灰度图
img2 = cv.imread('./pic/cat500x480.jpg', 0)
plt.imshow(img2)
plt.show()

img2 = cv.imread('./pic/cat500x480.jpg', 0)
plt.imshow(img2,cmap='gray')
plt.show()

print(img1.ndim, img2.ndim)
3 2
我们通过构造函数 show()
,通过判别图像的维度,来判别是灰色图像还是彩色图像,再进行输出
def show(img):
if img.ndim == 2:
plt.imshow(img, cmap='gray')
else:
plt.imshow(cv.cvtColor(img,cv.COLOR_RGB2BGR))
plt.plot()
apple = cv.imread('./pic/apple.jpg')
show(apple)

简单绘图
简单实用matplotlib来绘制数学图形
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(2,20)
y = 2 * x + np.random.randint(5,20,18)
plt.plot(x,y,'*-',color='r')
plt.show()

x = np.linspace(0,1,100)
y1 = np.power(x,0.5)
y2 = x
y3 = np.power(x,1.5)
plt.plot(x,y1,label='0.5')
plt.plot(x,y2,label='1.0')
plt.plot(x,y3,label='1.5')
plt.legend()
plt.xlabel('r')
plt.ylabel('s')
plt.grid()
plt.xlim([0,1])
plt.ylim([0,1])
plt.show()

a = np.random.randint(0, 10, 100)
print(a)
[5 7 9 5 1 6 4 0 4 5 0 8 2 0 2 4 8 2 2 2 0 0 2 8 9 7 7 9 0 1 3 8 1 2 2 0 3
8 2 7 5 9 1 5 4 1 2 8 6 0 7 4 3 1 7 8 6 0 1 4 0 4 6 2 3 0 6 7 9 3 3 9 2 5
7 8 1 5 9 1 8 1 0 2 5 5 7 1 9 3 2 1 9 2 0 4 6 0 0 2]
plt.hist(a, bins=10)
plt.show()

bins = np.arange(-0.5, 10, 1)
plt.hist(a, bins=bins, rwidth=0.8, color='green')
plt.xticks(range(0,10))
plt.show()

A1 = np.random.randint(0, 10, (10,10))
print(A1)
[[5 8 1 9 0 8 6 9 4 5]
[4 0 5 5 5 9 6 5 4 9]
[4 9 4 6 7 8 4 7 2 0]
[4 0 9 7 5 3 5 5 8 9]
[6 2 5 4 6 4 8 7 1 2]
[7 2 0 9 7 2 4 3 5 7]
[6 5 3 1 3 8 4 6 7 5]
[3 9 8 2 6 9 8 9 2 4]
[3 5 7 3 0 4 8 5 9 9]
[6 1 6 5 7 6 3 2 8 1]]
bins = np.arange(-0.5, 10, 1)
plt.hist(A1.flatten(), bins=bins, rwidth=0.8, color='green')
plt.xticks(range(0,10))
plt.show()

np.arange(-0.5, 10, 1)
array([-0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5])
Original: https://blog.csdn.net/qq_45176548/article/details/126627128
Author: 北山啦
Title: numpy在数字图像处理中的应用
相关阅读
Title: DataFrame逻辑运算> <
import pandas as pd
# 读取文件
data = pd.read_csv("./data/stock_day.csv")
data
openhighcloselowvolumeprice_changep_changeturnover2018-02-2723.5325.8824.1623.5395578.030.632.682.392018-02-2622.8023.7823.5322.8060985.110.693.021.532018-02-2322.8823.3722.8222.7152914.010.542.421.322018-02-2222.2522.7622.2822.0236105.010.361.640.902018-02-1421.4921.9921.9221.4823331.040.442.050.58………………………2015-03-0613.1714.4814.2813.13179831.721.128.516.162015-03-0512.8813.4513.1612.8793180.390.262.023.192015-03-0412.8012.9212.9012.6167075.440.201.572.302015-03-0312.5213.0612.7012.52139071.610.181.444.762015-03-0212.2512.6712.5212.2096291.730.322.623.30
643 rows × 8 columns
data.head() #默认获取前五行
openhighcloselowvolumeprice_changep_changeturnover2018-02-2723.5325.8824.1623.5395578.030.632.682.392018-02-2622.8023.7823.5322.8060985.110.693.021.532018-02-2322.8823.3722.8222.7152914.010.542.421.322018-02-2222.2522.7622.2822.0236105.010.361.640.902018-02-1421.4921.9921.9221.4823331.040.442.050.58
- 例如筛选data[“open”] > 23的日期数据
- data[“open”] > 23返回逻辑结果
data["open"] > 23
'''
2018-02-27 True
2018-02-26 False
2018-02-23 False
2018-02-22 False
2018-02-14 False
...
2015-03-06 False
2015-03-05 False
2015-03-04 False
2015-03-03 False
2015-03-02 False
Name: open, Length: 643, dtype: bool
'''
- 逻辑判断的结果可以作为筛选的依据
# 逻辑判断的结果可以作为筛选的依据
data[data["open"] > 23].head()
openhighcloselowvolumeprice_changep_changeturnover2018-02-2723.5325.88123.5395578.030.632.682.392018-02-0123.7123.86122.2266414.64-1.30-5.481.662018-01-3123.8523.98123.3149155.02-0.11-0.461.232018-01-3023.7124.08123.7032420.430.050.210.812018-01-1623.4024.60123.30101295.420.964.102.54
- 多个逻辑判断
data[(data["open"] > 23) & (data["open"] < 24)].head()
openhighcloselowvolumeprice_changep_changeturnover2018-02-2723.5325.88123.5395578.030.632.682.392018-02-0123.7123.86122.2266414.64-1.30-5.481.662018-01-3123.8523.98123.3149155.02-0.11-0.461.232018-01-3023.7124.08123.7032420.430.050.210.812018-01-1623.4024.60123.30101295.420.964.102.54
- query(expr)
- expr:查询字符串
通过query使得刚才的过程更加方便简单
data.query("open23").head()
openhighcloselowvolumeprice_changep_changeturnover2018-02-2723.5325.88123.5395578.030.632.682.392018-02-0123.7123.86122.2266414.64-1.30-5.481.662018-01-3123.8523.98123.3149155.02-0.11-0.461.232018-01-3023.7124.08123.7032420.430.050.210.812018-01-1623.4024.60123.30101295.420.964.102.54
- isin(values)
例如判断’open’是否为23.53和23.85
# 可以指定值进行一个判断,从而进行筛选操作
data[data["open"].isin([23.53, 23.85])]
openhighcloselowvolumeprice_changep_changeturnover2018-02-2723.5325.88123.5395578.030.632.682.392018-01-3123.8523.98123.3149155.02-0.11-0.461.232017-07-2623.5323.92122.85110276.48-0.30-1.272.762015-12-1823.5324.66123.43109230.050.652.793.742015-11-2623.8524.08123.5051446.29-0.31-1.301.76
Original: https://blog.csdn.net/weixin_48135624/article/details/115438167
Author: 缘 源 园
Title: DataFrame逻辑运算> <
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/319530/
转载文章受原作者版权保护。转载请注明原作者出处!