基本操作
首先加载相应的包:cv2
是OpenCV的包,numpy
用于数组操作,matplotlib
用于显示。
import cv2
import numpy as np
import matplotlib.pyplot as plt
# To enable the inline backend for usage with the IPython Notebook
%matplotlib inline
图像文件读写
OpenCV按照BGR通道顺序读写和显示彩色图像,plt.imshow
等程序按照RGB通道操作。因此,若要让plt
正确显示图像,需将通道顺序从BGR转为RGB。读入时指定参数cv2.IMREAD_GRAYSCALE
可以将彩色图转为灰度图。
im = cv2.imread('../../temp/cat.jpg')
print type(im), im.shape, im.dtype, im.min(), im.max()
im_gray = cv2.imread("../../temp/cat.jpg", cv2.IMREAD_GRAYSCALE)
print type(im_gray), im_gray.shape, im_gray.dtype, im_gray.min(), im_gray.max()
fig, axes = plt.subplots(1, 3, figsize = (8, 8))
fig.tight_layout()
axes = axes.ravel()
axes[0].imshow(im) #BGR
axes[1].imshow(im[:,:,::-1]) #RGB
axes[2].imshow(im_gray, cmap = plt.cm.gray) # GRAYSCALE
cv2.imwrite("../../temp/cat_1.jpg", im) # BGR is OK!
cv2.imwrite("../../temp/cat_2.jpg", im[:,:,::-1]) # RGB
<type 'numpy.ndarray'> (612, 950, 3) uint8 15 255
<type 'numpy.ndarray'> (612, 950) uint8 18 255
True
摄像头读取图像1
通过VideoCapture
读取摄像头,流程为:打开(open
) –> 采集(read
) –> 关闭(release
)。open
也可以操作图像和视频文件。
capture = cv2.VideoCapture()
capture.open(0)
# capture.open("../../temp/cat.jpg") # It's OK!
grabbed, frame = capture.read()
print('Frame grabbed: {0}'.format(grabbed)), frame.shape
plt.imshow(frame)
capture.release()
Frame grabbed: True (720, 1280, 3)
处理图像数据
图像以矩阵(数组)的形式存储,可以按numpy
操作数组的方式操作图像数据。Python的中变量的赋值相当于绑定,不会分配新的空间,为了避免相互影响,需要拷贝图像数据。
im = im[:,:,::-1] # BGR -->RGB
imm1 = im.copy()
imm1[:, :, 2] = 255
imm2 = im.copy()
imm2[:200, :100] = (255, 0, 0) # red up-left corner
imm3 = im.copy()
cv2.line(imm3, (0, 0), (imm3.shape[1], imm3.shape[0]), (255, 0, 0), thickness = 5)
cv2.circle(imm3, (imm3.shape[1]/2, imm3.shape[0]/2), 100, color = (0, 0, 255), thickness=5)
cv2.rectangle(imm3, (100, 50), (200, 300), color = (0, 255, 0), thickness = -1)
fig, axes = plt.subplots(1, 3, figsize = (8, 8))
fig.tight_layout()
axes = axes.ravel()
axes[0].imshow(imm1)
axes[1].imshow(imm2)
axes[2].imshow(imm3)
<matplotlib.image.AxesImage at 0x138c6f190>