一、python语法问题
1.关于类的实例化问题
class A(object):
a=1
class B(object):
b1= A()
b2= A()
实例化:
b1_=B()
b2_=B()
我发现b1_和b2_里的b1地址相同,b2也地址相同:
答:b1和b2的赋值需要在init函数下进行
2.numpy高级索引注意事项:
>>>a = np.array([[1,2,3], [4,5,6],[7,8,9]])
>>>a[[0,1,2], [0,1,2]]
array([1, 5, 9])
>>>a[0:3, [0, 1,2]]
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>>a[:, [0, 1,2]]
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
可以看到每个索引如果是不带:或者…的、尺寸一致的数组,那么第一个数组索引得到的切块会按照第二个索引数组内的数分别索引
如果某个索引带:,第一个数组索引得到的切块会按照第二个索引数组进行整个索引
3.numpy用range作索引注意事项
用range类型索引后,会新建一个原数组的拷贝,接下来的处理均不会影响到原数组;但用普通的:索引就不会
例:
import numpy
s=numpy.array([[2,3],[3,4]])
s[0:1][:,[0,1]] = s[0:1][:,[1,0]] # 该操作可以成功交换数组元素
s[frame_range][:,[0,1]] = s[frame_range][:,[1,0]] # 该操作不可以交换数组元素
二、matplotlib 作图注意事项
1.当在python环境使用matplotlib画图时报以下错误:
QObject::moveToThread: Current thread (0x55c1a5a149e0) is not the object's thread (0x55c1a5adf670).
Cannot move to target thread (0x55c1a5a149e0)
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/dq/anaconda2/envs/dq/lib/python3.6/site-packages/cv2/qt/plugins" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: xcb, eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, webgl.
Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
或者:
Failed to enable GUI event loop integration for 'qt5'
解决办法:
加上:
import matplotlib
matplotlib.use("TkAgg")
2.plot图像转numpy
import numpy as np
import matplotlib as plt
import cv2
fig = plt.figure(1)#在画图前加上;1表示编号为1,再次运行这条语句时不会生成新的窗口,如果不填数字就会生成新的窗口
...#画图部分
fig.canvas.draw()
X = np.array(fig.canvas.renderer._renderer)
cv2.imshow('plot', X)
cv2.waitKey(1)
三、Windows下运行
1.当在用git bash 运行sh脚本文件来运行python文件时,没有任何返回直接结束
见以下链接
python修复
当电脑只安装了python3.7时,用python命令默认是调用python3.7,python3命令无法找到。可以找到python的安装路径:
例如C:\Users\Administrator\AppData\Local\Programs\Python\Python37
把python.exe复制一份,并命名为python3.exe
那么python3命令就可以用了