Python opencv 常用操作

Posted lduml

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python opencv 常用操作相关的知识,希望对你有一定的参考价值。

1 数据读取

1.1读取

import cv2 #opencv读取的格式是BGR
import matplotlib.pyplot as plt
import numpy as np 
%matplotlib inline 

img=cv2.imread(\'cat.jpg\')
img
array([[[142, 151, 160],
        [146, 155, 164],
        [151, 160, 170],
        ...,
        [156, 172, 185],
        [155, 171, 184],
        [154, 170, 183]],

       [[108, 117, 126],
        [112, 123, 131],
        [118, 127, 137],
        ...,
        [155, 171, 184],
        [154, 170, 183],
        [153, 169, 182]],

       [[108, 119, 127],
        [110, 123, 131],
        [118, 128, 138],
        ...,
        [156, 169, 183],
        [155, 168, 182],
        [154, 167, 181]],

       ...,

       [[162, 186, 198],
        [157, 181, 193],
        [142, 166, 178],
        ...,
        [181, 204, 206],
        [170, 193, 195],
        [149, 172, 174]],

       [[140, 164, 176],
        [147, 171, 183],
        [139, 163, 175],
        ...,
        [169, 187, 188],
        [125, 143, 144],
        [106, 124, 125]],

       [[154, 178, 190],
        [154, 178, 190],
        [121, 145, 157],
        ...,
        [183, 198, 200],
        [128, 143, 145],
        [127, 142, 144]]], dtype=uint8)

1.2 图像预览

#图像的显示,也可以创建多个窗口
cv2.imshow(\'image\',img) 
# 等待时间,毫秒级,0表示任意键终止
cv2.waitKey(0) 
cv2.destroyAllWindows()
import matplotlib.pyplot as plt

plt.imshow(img, \'gray\'), plt.title(\'ORIGINAL\')
(<matplotlib.image.AxesImage at 0x7fa5a832fda0>, Text(0.5,1,\'ORIGINAL\'))

png

plt.imshow(img), plt.title(\'ORIGINAL\')
(<matplotlib.image.AxesImage at 0x7fa5b8618080>, Text(0.5,1,\'ORIGINAL\'))

1.3 选择通道

# 灰色通道
img=cv2.imread(\'cat.jpg\', 0)
plt.imshow(img), plt.title(\'0\')
(<matplotlib.image.AxesImage at 0x7fa5b868ff28>, Text(0.5,1,\'1\'))

# 彩色通道
img=cv2.imread(\'cat.jpg\', 1)
plt.imshow(img), plt.title(\'1\')
(<matplotlib.image.AxesImage at 0x7fa5b8639400>, Text(0.5,1,\'1\'))

img=cv2.imread(\'cat.jpg\', 2)
plt.imshow(img), plt.title(\'2\')
(<matplotlib.image.AxesImage at 0x7fa5c90bba20>, Text(0.5,1,\'2\'))

img=cv2.imread(\'cat.jpg\', 3)
plt.imshow(img), plt.title(\'3\')
(<matplotlib.image.AxesImage at 0x7fa5f86f5550>, Text(0.5,1,\'3\'))

img=cv2.imread(\'cat.jpg\', 4)
plt.imshow(img), plt.title(\'4\')
(<matplotlib.image.AxesImage at 0x7fa5f8846080>, Text(0.5,1,\'4\'))

1.4 图像保存

#保存
cv2.imwrite(\'mycat.png\',img)

2 视频读取

  • cv2.VideoCapture可以捕获摄像头,用数字来控制不同的设备,例如0,1。
  • 如果是视频文件,直接指定好路径即可。

2.1 操作摄像头

# 打开摄像头
vc = cv2.VideoCapture(0)

# 检查是否打开正确
if vc.isOpened(): 
    open, frame = vc.read()
else:
    open = False
    
while open:
    ret, frame = vc.read()
    if frame is None:
        break
    if ret == True:
        gray = cv2.cvtColor(frame,  cv2.COLOR_BGR2GRAY)
        cv2.imshow(\'result\', gray)
        if cv2.waitKey(100) & 0xFF == 27:
            break
vc.release()
cv2.destroyAllWindows()

2.2 读取本地视频

# 读取视频流
vc = cv2.VideoCapture(\'test.mp4\')

# 检查是否打开正确
if vc.isOpened(): 
    # 返回 True 和 第一帧图像
    open, frame = vc.read()
else:
    open = False
# while循环 ,只要有视频流,循环就继续,视频一帧一帧输出
i = 0 
while open:
    # ret 是布尔变量
    ret, frame = vc.read()
    i = i + 1
    if frame is None:
        break
    if ret == True:
        # 将彩色图转换为灰色图
        gray = cv2.cvtColor(frame,  cv2.COLOR_BGR2GRAY)
        cv2.imshow(\'result\', gray)
        # 100 代表100ms以后处理下一帧 数字越大越卡,越小播放速度越快
        # 27代表任意键退出
        if cv2.waitKey(10) & 0xFF == 27:
            break
print(i)
vc.release()
cv2.destroyAllWindows()
615

一共有615帧图 25秒的时长 615/25 = 24.6 每秒 24帧

# 读取视频流
vc = cv2.VideoCapture(\'test.mp4\')

# 检查是否打开正确
if vc.isOpened(): 
    # 返回 True 和 第一帧图像
    open, frame = vc.read()
else:
    open = False
open
True
type(frame)
numpy.ndarray
# 每一帧的图像
frame.shape
(544, 960, 3)
# 获取一帧图像显示
plt.imshow(frame), plt.title(\'frame\')
(<matplotlib.image.AxesImage at 0x7fa5b86d2518>, Text(0.5,1,\'frame\'))

# 颜色转化工具
gray = cv2.cvtColor(frame,  cv2.COLOR_BGR2GRAY)
plt.imshow(gray), plt.title(\'frame\')
(<matplotlib.image.AxesImage at 0x7fa5b90ddda0>, Text(0.5,1,\'frame\'))

gray2color = cv2.cvtColor(gray,  cv2.COLOR_GRAY2BGR)
plt.imshow(gray2color), plt.title(\'frame\')
(<matplotlib.image.AxesImage at 0x7fa5c95779e8>, Text(0.5,1,\'frame\'))

gray2color.shape
(544, 960, 3)
!mkdir img
cv2.imwrite(\'./img/frame.png\',gray2color)
True
cv2.imwrite(\'./img/frame_gray.png\',gray)
True
gray2color = cv2.cvtColor(gray,  cv2.COLOR_GRAY2RGB)
plt.imshow(gray2color), plt.title(\'frame\')
(<matplotlib.image.AxesImage at 0x7fa5d8a7a710>, Text(0.5,1,\'frame\'))

3 简单图像操作

截取部分图像数据

def cv_show(name,img):
    cv2.imshow(name,img) 
    cv2.waitKey(0) 
    cv2.destroyAllWindows()
img=cv2.imread(\'cat.jpg\')
cat=img[0:50,0:200] 
plt.imshow(cat), plt.title(\'cat\')
(<matplotlib.image.AxesImage at 0x7fa5e84c5d30>, Text(0.5,1,\'cat\'))

cat=img[50:150,0:200] 
plt.imshow(cat), plt.title(\'cat\')
(<matplotlib.image.AxesImage at 0x7fa5a8865780>, Text(0.5,1,\'cat\'))

将三个通道的颜色提取出来

b,g,r=cv2.split(img)
plt.imshow(r), plt.title(\'r\')
(<matplotlib.image.AxesImage at 0x7fa5d97a4a90>, Text(0.5,1,\'r\'))

plt.imshow(b), plt.title(\'r\')
(<matplotlib.image.AxesImage at 0x7fa5e85252e8>, Text(0.5,1,\'r\'))

cv_show(\'B\',img)
cv_show(\'g\',g)

B G R

# 只保留 通道 R
cur_img = img.copy()
cur_img[:,:,0] = 0
cur_img[:,:,1] = 0
cv_show(\'R\',cur_img)
# 只保留 G
cur_img = img.copy()
cur_img[:,:,0] = 0
cur_img[:,:,2] = 0
cv_show(\'G\',cur_img)
# 只保留B
cur_img = img.copy()
cur_img[:,:,1] = 0
cur_img[:,:,2] = 0
cv_show(\'B\',cur_img)

边界填充

copyMakeBorder(图像,上,下,左,右,填充方法)
top_size,bottom_size,left_size,right_size = (50,50,50,50)

replicate = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_REPLICATE)
reflect = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size,cv2.BORDER_REFLECT)
reflect101 = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_REFLECT_101)
wrap = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_WRAP)
constant = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size,cv2.BORDER_CONSTANT, value=0)
plt.subplot(231), plt.imshow(img, \'gray\'), plt.title(\'ORIGINAL\')
plt.subplot(232), plt.imshow(replicate, \'gray\'), plt.title(\'REPLICATE\')
plt.subplot(233), plt.imshow(reflect, \'gray\'), plt.title(\'REFLECT\')
plt.subplot(234), plt.imshow(reflect101, \'gray\'), plt.title(\'REFLECT_101\')
plt.subplot(235), plt.imshow(wrap, \'gray\'), plt.title(\'WRAP\')
plt.subplot(236), plt.imshow(constant, \'gray\'), plt.title(\'CONSTANT\')

plt.show()

  • BORDER_REPLICATE:复制法,也就是复制最边缘像素。
  • BORDER_REFLECT:反射法,对感兴趣的图像中的像素在两边进行复制例如:fedcba|abcdefgh|hgfedcb
  • BORDER_REFLECT_101:反射法,也就是以最边缘像素为轴,对称,gfedcb|abcdefgh|gfedcba
  • BORDER_WRAP:外包装法cdefgh|abcdefgh|abcdefg
  • BORDER_CONSTANT:常量法,常数值填充。

数值计算

img_cat=cv2.imread(\'cat.jpg\')
plt.imshow(img_cat), plt.title(\'img_cat\')
(<matplotlib.image.AxesImage at 0x7fa5a88ff630>, Text(0.5,1,\'img_cat\'))

img_cat2= img_cat +10 
plt.imshow(img_cat2), plt.title(\'img_cat2\')
(<matplotlib.image.AxesImage at 0x7fa5e88a9128>, Text(0.5,1,\'img_cat2\'))

img_cat3= img_cat +60 
plt.imshow(img_cat3), plt.title(\'img_cat3\')
(<matplotlib.image.AxesImage at 0x7fa5e8995f60>, Text(0.5,1,\'img_cat3\'))

img_cat[:5,:,0]
array([[142, 146, 151, ..., 156, 155, 154],
       [108, 112, 118, ..., 155, 154, 153],
       [108, 110, 118, ..., 156, 155, 154],
       [139, 141, 148, ..., 156, 155, 154],
       [153, 156, 163, ..., 160, 159, 158]], dtype=uint8)
img_cat3[:5,:,0]
array([[202, 206, 211, ..., 216, 215, 214],
       [168, 172, 178, ..., 215, 214, 213],
       [168, 170, 178, ..., 216, 215, 214],
       [199, 201, 208, ..., 216, 215, 214],
       [213, 216, 223, ..., 220, 219, 218]], dtype=uint8)
#相当于% 256
img_cat4 = img_cat + img_cat2
(img_cat4)[:5,:,0] 
plt.imshow(img_cat4), plt.title(\'img_cat4\')
(<matplotlib.image.AxesImage at 0x7fa5c98a7208>, Text(0.5,1,\'img_cat4\'))

img_cat4[:5,:,0] 
array([[ 38,  46,  56, ...,  66,  64,  62],
       [226, 234, 246, ...,  64,  62,  60],
       [226, 230, 246, ...,  66,  64,  62],
       [ 32,  36,  50, ...,  66,  64,  62],
       [ 60,  66,  80, ...,  74,  72,  70]], dtype=uint8)

图像add操作以及,方法的不同

img_cat5 = cv2.add(img_cat,img_cat2)
img_cat5[:5,:,0]
array([[255, 255, 255, ..., 255, 255, 255],
       [226, 234, 246, ..., 255, 255, 255],
       [226, 230, 246, ..., 255, 255, 255],
       [255, 255, 255, ..., 255, 255, 255],
       [255, 255, 255, ..., 255, 255, 255]], dtype=uint8)
plt.imshow(img_cat5), plt.title(\'img_cat5\')
(<matplotlib.image.AxesImage at 0x7fa5c9e41860>, Text(0.5,1,\'img_cat5\'))

img_cat.shape
(414, 500, 3)

图像融合

img_dog=cv2.imread(\'dog.jpg\')
img_dog.shape
plt.imshow(img_dog), plt.title(\'img_dog\')
(<matplotlib.image.AxesImage at 0x7fa5c94ff828>, Text(0.5,1,\'img_dog\'))

# resize操作
img_dog = cv2.resize(img_dog, (500, 414))
img_dog.shape
(414, 500, 3)
plt.imshow(img_dog), plt.title(\'img_dog\')
(<matplotlib.image.AxesImage at 0x7fa5c977c240>, Text(0.5,1,\'img_dog\'))

# 倍数缩放 fx fy 分别扩大几倍
res = cv2.resize(img_dog, (0, 0), fx=2, fy=2)
plt.imshow(res), plt.title(\'img_dog\')
(<matplotlib.image.AxesImage at 0x7fa5a8afaa58>, Text(0.5,1,\'img_dog\'))

addWeighted融合

# result =  0.4*cat + 0.6*dog + b
# b是偏置项
res = cv2.addWeighted(img_cat, 0.4, img_dog, 0.6, 0)
plt.imshow(res), plt.title(\'cat_dog\')
(<matplotlib.image.AxesImage at 0x7fa5e8c0b470>, Text(0.5,1,\'cat_dog\'))

img_dog=cv2.imread(\'dog.jpg\')
img_dog.shape
# plt.imshow(img_dog), plt.title(\'img_dog\')
img_dog = cv2.resize(img_dog, (500, 414))
cat_dog = img_cat + img_dog
plt.imshow(cat_dog), plt.title(\'cat_dog\')
(<matplotlib.image.AxesImage at 0x7fa5f9073ef0>, Text(0.5,1,\'cat_dog\'))

4 PIL 使用

from PIL import Image
import matplotlib.pyplot as plt
img = Image.open(\'dog.jpg\')
plt.imshow(img)   #根据数组绘制图像
plt.show()

以上是关于Python opencv 常用操作的主要内容,如果未能解决你的问题,请参考以下文章

常用python日期日志获取内容循环的代码片段

python常用代码片段总结

opencv-python常用知识速查

python+opencv实现简单的人脸识别

OpenCV—Python PyLibTiff_psd 图像基本操作以及图像格式转换

常用opencv调用摄像头代码(python)