参考我之前写的处理图片的文章:Python+OpenCV实现【图片】局部区域像素值处理(改进版)
开发环境:Python3.6.0 + OpenCV3.2.0
任务目标:摄像头采集图像(例如:480640),并对视频流每一帧(灰度图)特定矩形区域(48030)像素值进行行求和,得到一个480*1的数组,用这480个数据绘制条形图,即在逐帧采集视频流并处理后“实时”显示采集到的视频,并“实时”更新条形图。工作流程如下图:
源码:
# -*- coding:utf-8 -*-import cv2import numpy as npcamera = cv2.VideoCapture(0) # 参数0表示第一个摄像头# camera = cv2.VideoCapture(\"test.avi\") # 从文件读取视频# 判断视频是否打开if (camera.isOpened()):print \'Open\'else:print \'Fail to open!\'# # 测试用,查看视频size# size = (int(camera.get(cv2.CAP_PROP_FRAME_WIDTH)),# int(camera.get(cv2.CAP_PROP_FRAME_HEIGHT)))# print \'size:\'+repr(size)rectangleCols = 30while True:grabbed, frame_lwpCV = camera.read() # 逐帧采集视频流if not grabbed:breakgray_lwpCV = cv2.cvtColor(frame_lwpCV, cv2.COLOR_BGR2GRAY) # 转灰度图frame_data = np.array(gray_lwpCV) # 每一帧循环存入数组box_data = frame_data[:, 400:400+rectangleCols] # 取矩形目标区域pixel_sum = np.sum(box_data, axis=1) # 行求和qlength = len(gray_lwpCV)x = range(length)emptyImage = np.zeros((rectangleCols*10, length*2, 3), np.uint8)for i in x:cv2.rectangle(emptyImage, (i*2, (rectangleCols-pixel_sum[i]/255)*10), ((i+1)*2, rectangleCols*10), (255, 0, 0), 1)emptyImage = cv2.resize(emptyImage, (320, 240))# 画目标区域lwpCV_box = cv2.rectangle(frame_lwpCV, (400, 0), (430, length), (0, 255, 0), 2)cv2.imshow(\'lwpCVWindow\', frame_lwpCV) # 显示采集到的视频流cv2.imshow(\'sum\', emptyImage) # 显示画出的条形图key = cv2.waitKey(1) & 0xFFif key == ord(\'q\'):breakcamera.release()cv2.destroyAllWindows()
以上就是python环境下OPenCV处理视频流局部区域像素值的详细内容,更多关于OPenCV视频流局部区域像素处理的资料请关注脚本之家其它相关文章!
您可能感兴趣的文章:
- Python OpenCV图像指定区域裁剪的实现
- python用opencv批量截取图像指定区域的方法
- 教你如何用python操作摄像头以及对视频流的处理
- Python OpenCV处理图像之图像像素点操作
- Python+OpenCV图片局部区域像素值处理改进版详解
- Python+OpenCV图片局部区域像素值处理详解