查看opencv的版本
确认opencv版本的重要性:因为opencv版本不同,内部封装宏不同。比如opencv 2.x和opencv 3.x的imread函数入参就不同:
#img = cv2.imread(sys.argv[1], cv2.CV_LOAD_IMAGE_UNCHANGED) #opencv 2.X时用cv2.CV_LOAD_IMAGE_UNCHANGED
img = cv2.imread(sys.argv[1], cv2.IMREAD_ANYCOLOR) #opencv 3.X时用cv.IMREAD_ANCOLOR
import cv2
cv2.__version__
代码
1)网盘下载:链接:https://pan.baidu.com/s/1S8r1ZBrJPXyRWSpOmhByyg 密码:l2kb
代码说明
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 18 18:57:44 2018
@author: tom
"""
import cv2
import sys
#main function
if __name__ =='__main__':
if len(sys.argv) > 1:
#input image
#img = cv2.imread(sys.argv[1], cv2.CV_LOAD_IMAGE_UNCHANGED) #opencv 2.X时用cv2.CV_LOAD_IMAGE_UNCHANGED
img = cv2.imread(sys.argv[1], cv2.IMREAD_ANYCOLOR) #opencv 3.X时用cv.IMREAD_ANCOLOR
else:
print('Usage:python ex2_first_opencv2.py imageFile')
#show image
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
waitKe(0)阻塞等待用户输入任意键
destroyAllWindows()关闭所有窗口
执行
1)代码py文件所在文件夹上shift+鼠标右键---->“在此处打开命令行窗口(W)”--->在打开的cmd窗口上输入python--->输入空格---->将py文件拖入窗口内--->输入空格---->将lena.jpg拖入窗口--->然后按下enter键。
进过以上步骤就会打开lena.jpg文件,如下图所示,在英文模式下然后按下任意键退出窗口
(end)