0/master/index.html
OpenCV-Python Tutorials
1/master/d6/d00/tutorial_py_root.html
opencv智能提示PTVS 2.2.6 VS /Microsoft/PTVS/releases/v2.2.6
下面所有代码来在opencv-python官方文档
读并显示一张图片
import numpy as npimport cv2 as cvimg = cv.imread('image.jpg',1)cv.imshow('image',img)k = cv.waitKey(0)if k == 27: # wait for ESC key to exitcv.destroyAllWindows()elif k == ord('s'): # wait for 's' key to save and exitcv.imwrite('messigray.png',img)cv.destroyAllWindows()
import numpy as npimport cv2 as cvfrom matplotlib import pyplot as pltimg = cv.imread('image.jpg', 0)imgColor = cv.ctplt.imshow(img, cmap = 'gray', interpolation = 'bicubic')plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axisplt.show()
如果用matplotlib显示彩色图片,需要颜色通道
import numpy as npimport cv2 as cvfrom matplotlib import pyplot as pltimg = cv.imread('image.jpg', 1)imgRGB = cv.cvtColor(img, cv.COLOR_RGB2BGR)plt.imshow(imgRGB, cmap = 'gray', interpolation = 'bicubic')plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axisplt.show()
如果导入pyplot错误则通过pip安装一下matplotlib即可
打开相机
import numpy as npimport cv2 as cvcap = cv.VideoCapture(0)while(True):# Capture frame-by-frameret, frame = cap.read()# Our operations on the frame come heregray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)# Display the resulting framecv.imshow('frame',gray)if cv.waitKey(1) & 0xFF == ord('q'):break# When everything done, release the capturecap.release()cv.destroyAllWindows()