python能用opencv吗

433次阅读
没有评论

python能用opencv吗

一、在 Anaconda2 中配置 OpenCV

解压 opencv,添加系统环境变量:计算机–>右键属性–>高级系统设置–>环境变量–>系统变量–>编辑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;

打开 ipython 测试一下:

import cv2
print(cv2.__version__)

二、OpenCV 基础知识

1. 读取、显示和写入图像

import cv2
import matplotlib.pyplot as plt
# 读取图像,第二个参数可以为1(默认读入彩图, 可省略), 0(以灰度图读入)
im = cv2.imread('empire.jpg', 1) # 函数imread()返回图像为一个标准的 NumPy 数组
h,w = im.shape[:2]
print h,w
# 显示图像,第一个参数是窗口的名字,其次才是我们的图像,窗口会自动调整为图像大小。
cv2.imshow('image', img)
cv2.waitKey(0) # 为防止图像一闪而过,无限期的等待键盘输入
cv2.destroyAllWindows() # 关闭所有图像
# 保存图像(必须设置保存图像的路径和扩展名)
cv2.imwrite('result.png', im)
# 使用 plt 显示图像(可显示像素坐标及像素值)、保存图像
plt.imshow(im, cmap='gray', interpolation='bicubic')
plt.show()
plt.savefig('figpath.png', bbox_inches='tight')

2. 颜色空间转换

在OpenCV 中,图像不是按传统的RGB 颜色通道,而是按BGR 顺序(即RGB 的倒序)存储的。读取图像时默认的是BGR,但是还有一些可用的转换函数。颜色空间的转换可以用函数cvtColor() 来实现。

# 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

3. 在图像上画直线、矩形、圆、多边形(曲线)

画直线: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)

4. 图像的基础操作

获取并修改像素值

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
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-06-27发表,共计3261字。
新手QQ群:570568346,欢迎进群讨论 Python51学习