opencv安装

 
pip install opencv-python-headless -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple

常见问题:ImportError: libGL.so.1: cannot open shared object file: No such file or directory

 
pip install opencv-python-headless
    

 
>>> import cv2
>>> cv2.__version__
'4.7.0' 

opencv读取图像

 
cv2读取图像的格式 [H,W,C], 其中C为 [B,G,R]
读取结果数据类型 np.ndarray 

cv2读取图像示例

 
import cv2
image = cv2.imread("K:/tmp/a.png")#image :是返回提取到的图片的值
print(type(image),image.shape)  # <class 'numpy.ndarray'> (95, 381, 3)

tmp=image[0,1]
tmp
array([208, 203, 194], dtype=uint8)

tmp = tmp/255
tmp.round(7)
array([0.8156863, 0.7960784, 0.7607843])

matplotlib读取图像示例

 
from matplotlib import pyplot as plt 
img = plt.imread("K:/tmp/a.png")
print(type(img),img.shape)  <class 'numpy.ndarray'> (458, 604, 4)
    
img[0,1]
array([0.7607843 , 0.79607844, 0.8156863 , 1.        ], dtype=float32)

cv2.resize

 
cv2.resize(src=img,dsize=(32,32))

 
import cv2

img = cv2.imread("lingmao.jpg")
print(type(img),img.shape)  #class 'numpy.ndarray' (940, 940, 3)

img = cv2.resize(img,dsize=(32,32))
print(type(img),img.shape)  #class 'numpy.ndarray' (32, 32, 3)

参考