目录
1、python 列表与迭代器的区别:
python 列表与迭代器的区别(https://www.jianshu.com/p/1319018dde31)
列表是可迭代对象,但不是迭代器。可以通过内置函数 iter 变成迭代器。
>>> a = [1,3,5,7]
>>> a_iter = iter(a)
>>> from collections.abc import Iterator
>>> isinstance(a_iter,Iterator)
>>> True #输出为True
2、python assert的用法:
python assert的用法介绍(附代码)(https://www.php.cn/python-tutorials-416728.html)
assert 用来捕获异常,使用 assert 可以在出现有异常的代码处直接终止运行。 而不用等到程序执行完毕之后抛出异常。
assert 语句的语法格式
assert 表达式,异常提示
附加说明:assert也可以用于多个表达式的: assert 表达式1,表达式2。
注意:表达式=false 时,则执行其后面的异常。
示例一
> assert 10<0, "表达式错误"
输出
File "C:/Users/张勇/Documents/untitled2.py", line 8, in <module>
assert 10<0, "表达式错误"
AssertionError: 表达式错误
示例二
> assert (1 > 0 and 10 < 0), "表达式错误"
print("right")
输出
File "C:/Users/张勇/Documents/untitled2.py", line 8, in <module>
assert (1 > 0 and 10 < 0), "表达式错误"
AssertionError: 表达式错误
示例三
> assert (1 > 0 or 10 < 0), "表达式错误"
print("right")
输出
right
3、python glob.glob使用:
python glob.glob使用(https://blog.csdn.net/mantoureganmian/article/details/47949101)
glob.glob函数的参数是字符串,字符串中可以包括*、? 等,其中 * 表示匹配任意字符串,? 表示匹配任意单个字符,glob.glob函数匹配所有的符合条件的文件,并将其以list的形式返回。
model = nn.DataParallel(model)
4、Python 文件 readlines() 方法:
Python 文件 readlines() 方法(https://www.w3school.com.cn/python/ref_file_readlines.asp)
作为列表返回文件中的所有行,其中每一行都是列表中的一个元素。
示例:
f = open("demofile.txt", "r")
print(f.readlines())
输出:
['Hello! Welcome to demofile.txt\n', 'This file is for testing purposes.\n', 'Good Luck!']
5、为什么要用numpy.array:
python中数组(numpy.array)的基本操作(https://blog.csdn.net/sinat_34474705/article/details/74458605)
为了计算更加高效
Python中提供了list容器,可以当作数组使用。但列表中的元素可以是任何对象,因此列表中保存的是对象的指针,这样一来,为了保存一个简单的列表[1,2,3]。就需要三个指针和三个整数对象。对于数值运算来说,这种结构显然不够高效。
Python虽然也提供了array模块,但其只支持一维数组,不支持多维数组,也没有各种运算函数。因而不适合数值运算。
NumPy的出现弥补了这些不足。
使用示例
import numpy as np
## 常规创建方法
a = np.array([2,3,4])
b = np.array([2.0,3.0,4.0])
c = np.array([[1.0,2.0],[3.0,4.0]])
d = np.array([[1,2],[3,4]],dtype=complex) # 指定数据类型
print a, a.dtype
print b, b.dtype
print c, c.dtype
print d, d.dtype
输出
[2 3 4] int32
[ 2. 3. 4.] float64
[[ 1. 2.]
[ 3. 4.]] float64
[[ 1.+0.j 2.+0.j]
[ 3.+0.j 4.+0.j]] complex128
6、numpy astype():
np.astype()(https://blog.csdn.net/qq_41621362/article/details/94405846)
作用:转换numpy数组的数据类型
使用示例
import numpy as np
arr = np.arange((10))
print(arr, arr.dtype, sep="\n")
print("===================================")
arr = arr.astype("float32")
print(arr, arr.dtype, sep="\n")
输出
[0 1 2 3 4 5 6 7 8 9]
int32 #可以看到,他的数据类型为 int32
===================================
[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
float32 #可以看到数据类型转换成了 float32
用法:arr.astype(“具体的数据类型”)
7、为什么用 PIL.Image 打开图片后要转换为 np.uint8 格式:
OpenCV显示图片—dtype=np.uint8(https://www.cnblogs.com/zyb993963526/p/13724691.html)
python-opencv图片类型注意需为dtype=“uint8“
(https://blog.csdn.net/wchasedream/article/details/107219641)
python中PIL.Image和OpenCV图像格式相互转换(https://blog.csdn.net/dcrmg/article/details/78147219)
因为要想用 OpenCV 显示图片,那么存储的数据格式必须为np.uint8,否则之后是显示不出来的。
除此之外,还需要把 Image 的RGB 格式转换为 OpenCV 的 BGR 格式才能用 cv2.imshow()正常显示,否则显示出来的颜色是相反的。
img_file = Image.open(filename)
rgb_png = np.array(img_file, dtype='uint8') # in the range [0,255]
img_file.close()
return rgb_png
8、np.float 和 np.float32 和 np.float64 的区别:
numpy和torch数据类型转化问题(https://blog.csdn.net/qq_37555071/article/details/107553416)
numpy 使用 astype 转化数据类型时,np.float 默认转化为64位,可以使用np.float32 指定为32位,即 np.float = np.float64。
#numpy转化float类型
a= np.array([1,2,3])
a = a.astype(np.float)
print(a)
print(a.dtype)
输出
[1. 2. 3.]
float64
9、PIL.Image 和OpenCV 读取的图像格式有什么不同:
OpenCV、Skimage、PIL图像处理的细节差异(https://www.jianshu.com/p/b33af419e20e)
python中PIL.Image和OpenCV图像格式相互转换(https://blog.csdn.net/dcrmg/article/details/78147219)
Image.open 打开的图片类型为PIL Image, 值为0-255,尺寸为 W * H * C,格式为 RGB,通过img=np.array(img) 转为 numpy 数组,尺寸为H * W * C,格式依然为 RGB。
此时用 cv2.imshow() 显示,图片颜色和原始图片颜色是相反的,必须要再用 cv2.COLOR_RGB2BGR 把 RGB 转换为 opencv 的 BGR,才能正常显示。
from PIL import Image
import numpy as np
image = Image.open('test.jpg') # 图片是400x300 宽x高
print type(image) # out: PIL.JpegImagePlugin.JpegImageFile
print image.size # out: (400,300)
print image.mode # out: 'RGB'
print image.getpixel((0,0)) # out: (143, 198, 201)
# resize w*h
image = image.resize((200,100),Image.NEAREST)
print image.size # out: (200,100)
image = np.array(image,dtype=np.float32) # image = np.array(image)默认是uint8
print image.shape # out: (100, 200, 3)
image=image.astype(np.float32)
cv2.imread 打开的图片类型为np数组, 值为0-255,尺寸为 HWC
import cv2
import numpy as np
image = cv2.imread('test.jpg')
print type(image) # out: numpy.ndarray
print image.dtype # out: dtype('uint8')
print image.shape # out: (300, 400, 3) (h,w,c) 和skimage类似
print image # 'BGR'
# resize w*h
image = cv2.resize(image,(100,200))
print image.dtype # out: dtype('uint8')
print image.shape # out: (200, 100, 3)
10、KITTI 深度图与真实距离:
相对深度与绝对深度,深度图与真实距离(https://blog.csdn.net/weixin_41423872/article/details/117522856)
KITTI 深度图是以 uint16 的格式存储的,要从深度图中读取真实距离,除以 256 之后以米为单位就可以了,深度图中为0的点,不是说距离为零,而是这些点的距离没有获取到。