计算机-->右键属性-->高级系统设置-->环境变量-->系统变量-->编辑path-->添加 F:\Program Files (x86)\opencv-3.2.0-vc14\build\x64\vc14\bin
opencv/build/python/2.7/x64/cv2.pyd
到 Anaconda2/Lib/Site-packages/
python/2.7
能够看出,opencv 官方的 python 接口只支持 Anaconda2的版本 ,若是你装的是 Anaconda3 的话,能够打开cmd,而后执行conda install -c https://conda.anaconda.org/menpo opencv3
; 也能够参考此博客进行 Anaconda3 的配置import cv2 print(cv2.__version__)
import cv2 import matplotlib.pyplot as plt # 读取图像,第二个参数能够为1(默认读入彩图, 可省略), 0(以灰度图读入) im = cv2.imread('empire.jpg', 1) # 函数imread()返回图像为一个标准的 NumPy 数组 height, weight. channel = im.shape print height, weight. channel # 注意:height 和 width 分别对用图像坐标系的 y 轴 和 x 轴 # cropped_im = im[ny: ny + size, nx: nx + size, :],height(对应y轴)在前 # 显示图像,第一个参数是窗口的名字,其次才是咱们的图像,窗口会自动调整为图像大小。 cv2.imshow('image', img) cv2.waitKey(0) # 为防止图像一闪而过,无限期的等待键盘输入 cv2.destroyAllWindows() # 关闭全部图像 # 保存图像(必须设置保存图像的路径和扩展名) cv2.imwrite('result.png', im) # 使用 plt 显示图像(可显示像素坐标及像素值)、保存图像 # 使用 plt 显示图像时,必须先把图像转换为 RGB 格式 im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB) plt.imshow(im, cmap='gray', interpolation='bicubic') plt.savefig('figpath.png', bbox_inches='tight') plt.show()
在OpenCV 中,图像不是按传统的RGB 颜色通道,而是按BGR 顺序(即RGB 的倒序)存储的。读取图像时默认的是BGR,可是还有一些可用的转换函数。颜色空间的转换能够用函数cvtColor() 来实现。python
# 1.使用opencv读取并建立灰度图像,按 BGR 顺序 im = cv2.imread('empire.jpg') gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) # 2.使用matplotlib.image 读入并建立灰度图像,按 RGB 顺序 import matplotlib.image as mpl_img im = mpl_img.imread('empire.jpg') gray = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY) # Note: 注意1和2的区别在颜色转换代码 # 经常使用:cv2.COLOR_BGR2RGB、cv2.COLOR_GRAY2BGR、cv2.COLOR_BGR2HSV
注意:
传入的坐标必须为int
型数组
cv2.line()
import cv2 # 读取图像,按 BGR 顺序 img = cv2.imread('empire.jpg') # 传入图像、起点坐标、终点坐标、线的颜色(color)、线的厚度(thickness) # color : Color of the shape. for BGR, pass it as a tuple, eg: (255,0,0) for blue. For grayscale, just pass the scalar value. # thickness : if -1 is passed for closed figures like circles, it will fill the shape, default thickness = 1. img = cv2.line(img, (0, 0), (511, 511), (255, 0, 0), 5)
cv2.rectangle()
# 须要传入图像、左上角顶点坐标、右下角顶点坐标、颜色、线宽 img = cv2.rectangle(img, (384, 0), (510, 128), (0, 255, 0), 3)
cv2.circle()
# 须要传入图像、圆的中心点坐标、半径、颜色、线宽 img = cv2.circle(img, (447, 63), 63, (0, 0, 255), -1) # If -1 is passed for closed figures like circles, it will fill the shape. default thickness = 1
cv2.polylines()
# 数组的数据类型必须为int32,若知道曲线方程,能够生成一堆点,就能够画出曲线来啦 pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32) # 第一个参数为-1, 代表这一维的长度(点的数量)是根据后面的维度的计算出来的 pts = pts.reshape((-1,1,2)) # 若是第三个参数是False,咱们获得的多边形是不闭合的(首尾不相连) img = cv2.polylines(img, [pts], True, (0, 255, 255))
cv2.putText()
font = cv2.FONT_HERSHEY_SIMPLEX # 第 3~6 个参数为:bottom-left corner where data starts、font size、color、thickness cv2.putText(img,'OpenCV',(10,500), font, 4, (255, 255, 255), 2, cv2.LINE_AA)
import cv2 import numpy as np img = cv2.imread('messi5.jpg') px = img[100, 100] print px [57 63 68] # accessing only blue pixel blue = img[100, 100, 0] print blue 57 # modify the pixel img[100, 100] = [255, 255, 255] print img[100, 100] [255 255 255] # channel 2 全部值置为0 img[:, :, 2] = 0
img = cv2.imread('messi5.jpg') print img.shape (960L, 1280L, 3L) print img.size 3686400 print img.dtype uint8
img = cv2.imread('messi5.jpg') # select the ball and copy it to another region ball = img[280:340, 330:390] # 注意:340和390取不到 img[273:333, 100:160] = ball
参考文档:函数
1.经过 Python 接口使用 OpenCV :https://blog.csdn.net/mzpmzk/article/details/68952160测试