import numpy as np
import torch
a = np.array([1, 2, 3, 4, 5, 6])#定义一个数组
b = np.array([8, 7, 6, 5, 4, 3])
print(a.shape, b.shape)#维度信息
# 结果:(6,) (6,),一维,长度为6的数组
# 把a变成3行2列,
aa = np.reshape(a, (3, 2))
print(aa, aa.shape)
# 结果:
# [[1 2]
# [3 4]
# [5 6]] (3, 2)
# 把b变成1x1x1x6的四维数组
bb = np.reshape(b, (1, 1, 1, 6))
print(bb, bb.shape)
# 结果:
# [[[[8 7 6 5 4 3]]]] (1, 1, 1, 6)
# 解释:只会有6个值,1,1,1只是它维度的一个表示
法一:squeeze()
# 四维转一维,降维
b1 = np.squeeze(bb)
print(b1, b1.shape)
# 结果:
# [8 7 6 5 4 3] (6,)
法二:reshape(,-1)
a2 = np.reshape(aa, -1) #-1,不管有多少个维度,都变成一维的
print(a2, a2.shape)
# 结果
# [1 2 3 4 5 6] (6,)
transpose和reshape差不多
transpose((1, 0))
#数组的交换(3,2)->(2,3)
aaa = aa.transpose((1, 0))
print(aaa, aaa.shape)
# 结果:
# [[1 3 5]
# [2 4 6]] (2, 3)
index = np.argmax(bb)
print("find max:",index, bb, bb[0][0][0][index])
#结果:
# find max: 0 [[[[8 7 6 5 4 3]]]] 8
m1 = np.zeros((6, 6), dtype=np.uint8)#6x6,初始化为0的
m2 = np.linspace(6, 10, 100)#从6到10分成100个等份
# 结果:
# [[0 0 0 0 0 0]
# [0 0 0 0 0 0]
# [0 0 0 0 0 0]
# [0 0 0 0 0 0]
# [0 0 0 0 0 0]
# [0 0 0 0 0 0]] [ 6. 6.04040404 6.08080808 6.12121212 6.16161616 6.2020202
# 6.24242424 6.28282828 6.32323232 6.36363636 6.4040404 6.44444444
# 6.48484848 6.52525253 6.56565657 6.60606061 6.64646465 6.68686869
# 6.72727273 6.76767677 6.80808081 6.84848485 6.88888889 6.92929293
# 6.96969697 7.01010101 7.05050505 7.09090909 7.13131313 7.17171717
# 7.21212121 7.25252525 7.29292929 7.33333333 7.37373737 7.41414141
# 7.45454545 7.49494949 7.53535354 7.57575758 7.61616162 7.65656566
# 7.6969697 7.73737374 7.77777778 7.81818182 7.85858586 7.8989899
# 7.93939394 7.97979798 8.02020202 8.06060606 8.1010101 8.14141414
# 8.18181818 8.22222222 8.26262626 8.3030303 8.34343434 8.38383838
# 8.42424242 8.46464646 8.50505051 8.54545455 8.58585859 8.62626263
# 8.66666667 8.70707071 8.74747475 8.78787879 8.82828283 8.86868687
# 8.90909091 8.94949495 8.98989899 9.03030303 9.07070707 9.11111111
# 9.15151515 9.19191919 9.23232323 9.27272727 9.31313131 9.35353535
# 9.39393939 9.43434343 9.47474747 9.51515152 9.55555556 9.5959596
# 9.63636364 9.67676768 9.71717172 9.75757576 9.7979798 9.83838384
# 9.87878788 9.91919192 9.95959596 10. ]
#读取一张图片,读过来的对象都是numpy的数组类型
image = cv.imread("D:/images/lena.jpg")
cv.waitKey(0)#等待键盘输入,解决图像一闪而过的现象(一二三木头人)
cv.destroyAllWindows()
#打印图片维度
h, w, c = image.shape # HWC
print(h, w, c)
# 结果:
# 261 222 3
blob = np.transpose(image, (2, 0, 1))
print(blob.shape) # CHW
# (3, 261, 222)
# 转化成浮点数,更要变成0~1之间的浮点数
fi = np.float32(image) / 255.0 # 0 ~1
# 彩色图转换为灰度图
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
print(gray.shape)
cv.imshow("gray", gray)
# 结果:
# (261, 222)
#只有1个通道
# 获取3个通道的数据
cv.imshow("gray",image[:,:,0])
cv.imshow("gray",image[:,:,1])
cv.imshow("gray",image[:,:,2])
zoom out 缩小
zoom in 放大
#512,512->224,224
dst = cv.resize(image, (224, 224))
cap = cv.VideoCapture(0)
while True:
ret, frame = cap.read()
if ret is not True:
break
cv.rectangle(frame, (50, 50), (100, 100), (0, 0, 255), 2, 8, 5)
cv.rectangle(frame, (50, 50), (100, 100), (255, 0, 255), 2, 8, 0)
cv.imshow("frame", frame)
c = cv.waitKey(0)
if c == 27: # ESC
break
cv.waitKey(0)
cv.destroyAllWindows()
m1 = np.zeros((512, 512, 3), dtype=np.uint8)
m1 = np.zeros((512, 512, 3), dtype=np.uint8)
m1[:,:] = (0, 0, 255), # BGR
cv.imshow("m1", m1) #红色
cv.COLOR_BGR2RGB
# 把框的内容截取出来
box = [200, 200, 100, 100] # x, y, w, h
roi = image[200:300, 200:300, :]#h,w,c
cv.imshow("roi", roi)
合并roi
#绘制一个,矩形的一个顶点(200,200),矩阵对角线上的另一个顶点(400,400),颜色绘制成红色,线宽选择2(取负值时,函数绘制填充了色彩的矩形),线条的类型
cv.rectangle(image, (200, 200), (400, 400), (0, 0, 255), 2, 8)
cv.imshow("input", image)
在pytorch中任何数据都是tensor
x = torch.empty(2,2)
x = torch.randn(2,2)
print(x)
# 初始化为零
y = torch.zeros(3, 3)
print(y)
# 结果:
# tensor([[0., 0., 0.],
# [0., 0., 0.],
# [0., 0., 0.]])
#定义一个自己的数组
a = torch.tensor([1,2,3,4,5,6,7,8,9,0])
print(a)
# 结果
# tensor([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
# 维度变换,numpy是reshape,这里是view
# 2x2->1x4
# 3x3->1x9
x = x.view(-1, 4)
y = y.view(-1, 9)
print(x, y, x.size(), y.size()) #numpy是shape()
# 结果:
# tensor([[-0.2702, 0.8843, 0.8897, 0.1764]])
# tensor([[0., 0., 0., 0., 0., 0., 0., 0., 0.]])
# torch.Size([1, 4]) torch.Size([1, 9])
pytorch里的数据都是tensor
# 数据转换
# tensor->numpy
nx = x.numpy()
ny = y.numpy()
print("nx :\n", nx, "\n ny:\n", ny)
# 结果
# tensor([[-0.2702, 0.8843, 0.8897, 0.1764]]) tensor([[0., 0., 0., 0., 0., 0., 0., 0., 0.]])-->
# nx :
# [[-0.27022818 0.88428843 0.8896737 0.17644913]]
# ny:
# [[0. 0. 0. 0. 0. 0. 0. 0. 0.]]
# numpy数组转换成张量
#numpty 1x4-->2x2 之后再from_numpy转为tensor
x2 = torch.from_numpy(nx.reshape((2, 2)))
print(x2)
# 结果:
# tensor([[-0.2702, 0.8843],
# [ 0.8897, 0.1764]])
# 定义随机初始化的2x2矩阵
x = torch.randn(2, 2)
print(x)
# 结果:
# tensor([[-0.2702, 0.8843],
# [ 0.8897, 0.1764]])
# 两个矩阵相加
b = torch.tensor([11,12,13,14,15,16,17,18,19,10])
c = a.add(b)
print(c)
# 结果
# tensor([12, 14, 16, 18, 20, 22, 24, 26, 28, 10])
import torch
if torch.cuda.is_available():
print("GPU Detected!")
#让a和b在GPU里做运算
result = a.cuda() + b.cuda()
print(result)
pytorch里的数据从CPU变成GPU版本
# using CUDA/GPU
if torch.cuda.is_available():
print("GPU Detected!")
gpu_num = torch.cuda.current_device()
print("GPU Name: %s \n" % torch.cuda.get_device_name(gpu_num))
device = torch.device("cuda:0")
result = a.cuda() + b.cuda()
print(result)