【Python】-003 OpenCV-Python显示图片
OpenCV提供了python绑定,能够很方便的进行图像处理。
1、OpenCV-Python的安装
安装好Python之后,通过pip即可进行OpenCV-Python的安装。命令如下:
pip install --upgrade setuptools
pip install numpy Matplotlib
pip install opencv-python
2、使用
通过Python脚本调用OpenCV,打开图片,显示图片。
#导入cv模块
import cv2 as cv
#读取图像,支持 bmp、jpg、png、tiff 等常用格式
img = cv.imread("test.png")
def GetImgWidth(image):
shape = image.shape;
return shape[0];
def GetImgHeight(image):
shape = image.shape;
return shape[1];
def GetImgChannels(image):
shape = image.shape;
return shape[2];
sha = GetImgWidth(img);
print(GetImgWidth(img),GetImgHeight(img),GetImgChannels(img));
#将彩色图像转换成灰度图
grayImg = cv.cvtColor(img,cv.COLOR_BGR2GRAY);
cv.imshow("gray",grayImg);
#创建窗口并显示图像
cv.namedWindow("Image")
cv.imshow("Image",img)
cv.waitKey(0)
#释放窗口
cv.destroyAllWindows()