"""
视觉:基本API应用(OPENCV)
"""
import cv2 import numpy as np """ 图像读取方式3. 1.cv2.imread(filename or path, flags) flags=0:灰度图像;flags=1表示RGB图像;flags=-1表示alpha透明通道图像 """
import cv2 import numpy as np """ 图像读取方式3. 1.cv2.imread(filename or path, flags) flags=0:灰度图像;flags=1表示RGB图像;flags=-1表示alpha透明通道图像 """ img = cv2.imread('000000005620.jpg') # Gray是灰度图像;除以255是将像素转为0-1区间的值 Gray = img[:, :, 2]*0.3 + img[:, :, 1] * 0.59 + img[:, :, 0] * 0.11 gray = Gray/255 imgray = cv2.imread('000000005620.jpg', 0) # 加载透明通道图像 imalpha = cv2.imread('000000005620.jpg', -1) print(gray) if img is None: print('Image read error!') else: # 图像可视化 cv2.imshow('RGB of image', img) # 保存RGB图像 cv2.imwrite('RGB.png', img) cv2.imshow('Gray of image', imgray) # 保存灰度图像 cv2.imwrite('hd.png', imgray) cv2.imshow('alpha of image', imalpha) # 保存透明通道图像 cv2.imwrite('alpha.png', imalpha) # cv2.imshow('Gray of image', gray) print(type(imalpha), imalpha.shape) # 等待读者操作:让图像显示暂停delay毫秒,当delay秒设置为0的时候,表示永远,当键盘任意输入的时候,结束暂停 cv2.waitKey(0) # 窗口对象销毁 cv2.destroyAllWindows()
RGB:
Gray:
alpha(透明通道图像只有加载.png格式并带有净色的图像才会显示透明):
E:\myprogram\anaconda\envs\python3.6\python.exe E:/XXX/OPENCV/CV.py
[[0.96862745 0.96078431 0.96470588 ... 0.97254902 0.97254902 0.97254902]
[0.96862745 0.96078431 0.96078431 ... 0.94901961 0.95686275 0.96078431]
[0.97254902 0.96470588 0.96470588 ... 0.98431373 0.98431373 0.98431373]
...
[0.94117647 0.94117647 0.94117647 ... 0.95686275 0.95686275 0.95686275]
[0.94901961 0.94901961 0.94901961 ... 0.95294118 0.95294118 0.95294118]
[0.96078431 0.96078431 0.96078431 ... 0.9372549 0.9372549 0.9372549 ]]
<class 'numpy.ndarray'> (612, 612, 3)
Process finished with exit code 0
------------------------------------------------------------------------------------------------------------------
import matplotlib.pyplot as plt import cv2 import numpy as np """ 图像显示除了使用opencv,还可以采用matplotlib.pyplot """ img = cv2.imread('000000005620.jpg', 1) img2 = np.zeros_like(img, dtype=img.dtype) # 将opencv读取图像的方式转化为plt读取图像方式---> # BGR---RGB img2[:,:,0] = img[:,:,2] img2[:,:,1] = img[:,:,1] img2[:,:,2] = img[:,:,0] print(img2.shape) plt.imshow(img) plt.show()
RGB & BGR