大家好,又见面了,我是你们的朋友全栈君。
文章目录- 使用opencv打开摄像头预备知识
- 打开笔记本自带摄像头代码实战
- opencv打开jeston tx2板载摄像头
- 附录、GitHub打开摄像头的源码
- 附录、c 打开摄像头
- 附录、c 打开图片
- 附录c 、边缘检测
使用opencv打开摄像头预备知识
- VideoCapture的官网api
- opencv io示意图
- OpenCV 视频I / O模块是一组用于读取和写入视频或图像序列的类和功能。
打开笔记本自带摄像头代码实战
- 按q退出
import cv2
if __name__ == '__main__':
cap = cv2.VideoCapture(0) # 设置摄像头 0是默认的摄像头 如果你有多个摄像头的话呢,可以设置1,2,3....
while True: # 进入无限循环
ret, frame = cap.read() # 将摄像头拍到的图像作为frame值
cv2.imshow('frame', frame) # 将frame的值显示出来 有两个参数 前一个是窗口名字,后面是值
c = cv2.waitKey(1) # 判断退出的条件 当按下'Q'键的时候呢,就退出
if c == ord('q'):
break
cap.release() # 常规操作
cv2.destroyAllWindows()
- esc退出
import cv2 as cv
def video_demo():
# 0是代表摄像头编号,只有一个的话默认为0
capture = cv.VideoCapture(0)
while (True):
ref, frame = capture.read()
cv.imshow("1", frame)
# 等待30ms显示图像,若过程中按“Esc”退出
c = cv.waitKey(30) & 0xff
if c == 27:
capture.release()
break
video_demo()
cv.waitKey()
cv.destroyAllWindows()
代码注释
1、cv2.VideoCapture()函数: cap = cv2.VideoCapture(0) VideoCapture()中参数是0,表示打开笔记本的内置摄像头。 cap = cv2.VideoCapture(“…/1.avi”) VideoCapture(“…/1.avi”),表示参数是视频文件路径则打开视频。
2、cap.isOpened()函数: 返回true表示成功,false表示不成功
3、ret,frame = cap.read()函数: cap.read()按帧读取视频,ret,frame是获cap.read()方法的两个返回值。其中ret是布尔值,如果读取帧是正确的则返回True,如果文件读取到结尾,它的返回值就为False。frame就是每一帧的图像,是个三维矩阵。
4、cv2.waitKey()函数: 参数是1,表示延时1ms切换到下一帧图像,参数过大如cv2.waitKey(1000),会因为延时过久而卡顿感觉到卡顿。 参数为0,如cv2.waitKey(0)只显示当前帧图像,相当于视频暂停。
5、cap.release()与destroyAllWindows()函数: cap.release()释放视频,调用destroyAllWindows()关闭所有图像窗口。
6、c得到的是键盘输入的ASCII码,esc键对应的ASCII码是27,即当按esc键是if条件句成立。
- 使用第三方驱动程序或照相机注意事项
许多工业相机或某些视频I / O设备不为操作系统提供标准的驱动程序接口。因此,您不能在这些设备上使用VideoCapture或VideoWriter。
为了访问他们的设备,制造商提供了自己的C API和库,您必须包括它们并与OpenCV应用程序链接。
这是一种常见的情况,该库从/向存储器缓冲区读取/写入图像。如果是这样,Mat则可以为内存缓冲区(用户分配的数据)创建标头,并使用OpenCV函数对其进行就地处理。有关更多详细信息,请参见cv :: Mat :: Mat()
。
opencv打开jeston tx2板载摄像头
因为查询到tx1和tx2打开板载摄像头的命令不一样。其中arg1函数
是打开 tx1 的参数。arg2函数
是打开 tx2 的参数。这里参考了一篇国外的原文附带的GitHub地址查询到的。找不到原文了。我将GitHub的源码附录在最后以表达对原作者的尊重(带有作者个人信息)
import cv2
def arg1(width, height):
gst_str = ('nvcamerasrc ! '
'video/x-raw(memory:NVMM), '
'width=(int)2592, height=(int)1458, '
'format=(string)I420, framerate=(fraction)30/1 ! '
'nvvidconv ! '
'video/x-raw, width=(int){}, height=(int){}, '
'format=(string)BGRx ! '
'videoconvert ! appsink').format(width, height)
return gst_str
def arg2(width,height):
gst_str = ('nvarguscamerasrc ! '
'video/x-raw(memory:NVMM), '
'width=(int)1920, height=(int)1080, '
'format=(string)NV12, framerate=(fraction)30/1 ! '
'nvvidconv flip-method=2 ! '
'video/x-raw, width=(int){}, height=(int){}, '
'format=(string)BGRx ! '
'videoconvert ! appsink').format(width, height)
return gst_str
def open_1():
path = arg1(100, 100)
print("11111path",path)
cap =cv2.VideoCapture(path)
if not cap.isOpened():
print('Failed to open camera!111111111111111111111')
return cap
def open_2():
path = arg2(100, 100)
print("2222path",path)
cap = cv2.VideoCapture(path)
if not cap.isOpened():
print('Failed to open camera!2222222222222222')
return cap
def openVideo(cap):
while True: # 进入无限循环
ret, frame = cap.read() # 将摄像头拍到的图像作为frame值
cv2.imshow('frame', frame) # 将frame的值显示出来 有两个参数 前一个是窗口名字,后面是值
c = cv2.waitKey(1) # 判断退出的条件 当按下'Q'键的时候呢,就退出
if c == ord('q'):
break
cap.release() # 常规操作
cv2.destroyAllWindows()
if __name__ == '__main__':
cap_1 = open_1()
cap_2 = open_2()
openVideo(cap_1)
# openVideo(cap_2)
附录、GitHub打开摄像头的源码
代码语言:javascript复制# --------------------------------------------------------
# Camera sample code for Tegra X2/X1
#
# This program could capture and display video from
# IP CAM, USB webcam, or the Tegra onboard camera.
# Refer to the following blog post for how to set up
# and run the code:
# https://jkjung-avt.github.io/tx2-camera-with-python/
#
# Written by JK Jung <jkjung13@gmail.com>
# --------------------------------------------------------
import sys
import argparse
import subprocess
import cv2
WINDOW_NAME = 'CameraDemo'
def parse_args():
# Parse input arguments
desc = 'Capture and display live camera video on Jetson TX2/TX1'
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('--rtsp', dest='use_rtsp',
help='use IP CAM (remember to also set --uri)',
action='store_true')
parser.add_argument('--uri', dest='rtsp_uri',
help='RTSP URI, e.g. rtsp://192.168.1.64:554',
default=None, type=str)
parser.add_argument('--latency', dest='rtsp_latency',
help='latency in ms for RTSP [200]',
default=200, type=int)
parser.add_argument('--usb', dest='use_usb',
help='use USB webcam (remember to also set --vid)',
action='store_true')
parser.add_argument('--vid', dest='video_dev',
help='device # of USB webcam (/dev/video?) [1]',
default=1, type=int)
parser.add_argument('--width', dest='image_width',
help='image width [1920]',
default=1920, type=int)
parser.add_argument('--height', dest='image_height',
help='image height [1080]',
default=1080, type=int)
args = parser.parse_args()
return args
def open_cam_rtsp(uri, width, height, latency):
gst_str = ('rtspsrc location={} latency={} ! '
'rtph264depay ! h264parse ! omxh264dec ! '
'nvvidconv ! '
'video/x-raw, width=(int){}, height=(int){}, '
'format=(string)BGRx ! '
'videoconvert ! appsink').format(uri, latency, width, height)
return cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER)
def open_cam_usb(dev, width, height):
# We want to set width and height here, otherwise we could just do:
# return cv2.VideoCapture(dev)
gst_str = ('v4l2src device=/dev/video{} ! '
'video/x-raw, width=(int){}, height=(int){} ! '
'videoconvert ! appsink').format(dev, width, height)
return cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER)
def open_cam_onboard(width, height):
gst_elements = str(subprocess.check_output('gst-inspect-1.0'))
if 'nvcamerasrc' in gst_elements:
# On versions of L4T prior to 28.1, add 'flip-method=2' into gst_str
gst_str = ('nvcamerasrc ! '
'video/x-raw(memory:NVMM), '
'width=(int)2592, height=(int)1458, '
'format=(string)I420, framerate=(fraction)30/1 ! '
'nvvidconv ! '
'video/x-raw, width=(int){}, height=(int){}, '
'format=(string)BGRx ! '
'videoconvert ! appsink').format(width, height)
elif 'nvarguscamerasrc' in gst_elements:
gst_str = ('nvarguscamerasrc ! '
'video/x-raw(memory:NVMM), '
'width=(int)1920, height=(int)1080, '
'format=(string)NV12, framerate=(fraction)30/1 ! '
'nvvidconv flip-method=2 ! '
'video/x-raw, width=(int){}, height=(int){}, '
'format=(string)BGRx ! '
'videoconvert ! appsink').format(width, height)
else:
raise RuntimeError('onboard camera source not found!')
return cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER)
def open_window(width, height):
cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_NORMAL)
cv2.resizeWindow(WINDOW_NAME, width, height)
cv2.moveWindow(WINDOW_NAME, 0, 0)
cv2.setWindowTitle(WINDOW_NAME, 'Camera Demo for Jetson TX2/TX1')
def read_cam(cap):
show_help = True
full_scrn = False
help_text = '"Esc" to Quit, "H" for Help, "F" to Toggle Fullscreen'
font = cv2.FONT_HERSHEY_PLAIN
while True:
if cv2.getWindowProperty(WINDOW_NAME, 0) < 0:
# Check to see if the user has closed the window
# If yes, terminate the program
break
_, img = cap.read() # grab the next image frame from camera
if show_help:
cv2.putText(img, help_text, (11, 20), font,
1.0, (32, 32, 32), 4, cv2.LINE_AA)
cv2.putText(img, help_text, (10, 20), font,
1.0, (240, 240, 240), 1, cv2.LINE_AA)
cv2.imshow(WINDOW_NAME, img)
key = cv2.waitKey(10)
if key == 27: # ESC key: quit program
break
elif key == ord('H') or key == ord('h'): # toggle help message
show_help = not show_help
elif key == ord('F') or key == ord('f'): # toggle fullscreen
full_scrn = not full_scrn
if full_scrn:
cv2.setWindowProperty(WINDOW_NAME, cv2.WND_PROP_FULLSCREEN,
cv2.WINDOW_FULLSCREEN)
else:
cv2.setWindowProperty(WINDOW_NAME, cv2.WND_PROP_FULLSCREEN,
cv2.WINDOW_NORMAL)
def main():
args = parse_args()
print('Called with args:')
print(args)
print('OpenCV version: {}'.format(cv2.__version__))
if args.use_rtsp:
cap = open_cam_rtsp(args.rtsp_uri,
args.image_width,
args.image_height,
args.rtsp_latency)
elif args.use_usb:
cap = open_cam_usb(args.video_dev,
args.image_width,
args.image_height)
else: # by default, use the Jetson onboard camera
cap = open_cam_onboard(args.image_width,
args.image_height)
if not cap.isOpened():
sys.exit('Failed to open camera!')
open_window(args.image_width, args.image_height)
read_cam(cap)
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()
附录、c 打开摄像头
代码语言:javascript复制#include <opencv.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main() {
// 显示opencv的版本
cout << "the version of opencv is : " << CV_VERSION << endl;
Mat image;
image = imread("lena.jpg");
if (image.empty()) {
printf("src is empty");
return -1;
}
//VideoCapture capture("1.mp4");
VideoCapture capture;
capture.open("1.mp4");
while (true)
{
Mat frame;
capture >> frame;
if (frame.empty())
{
break;
}
else {
imshow("frame by frame", frame);
waitKey(10);
}
}
return 0;
}
附录、c 打开图片
代码语言:javascript复制#include <opencv.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
#include<opencv4/opencv2/core.hpp>
using namespace cv;
using namespace std;
int main(){
Mat src = imread("lena.jpg");
// 非空判断
if (src.empty()) {
printf("src is empty");
return -1;
}
// opencv的UI窗口,由opencv自己创建和释放
// WINDOW_AUTOSIZE 自适应文件大小,不支持窗口大小调整
// WINDOW_NORMAL 可以在qt上使用,用来调增大小
namedWindow("show image windos", WINDOW_AUTOSIZE);
imshow("show image windos", src);
// 色彩空间转换
namedWindow("change_image");
Mat out_image;
cvtColor(src,out_image, COLOR_BGR2GRAY);
imshow("change_image", out_image);
waitKey(0);
}
附录c 、边缘检测
代码语言:javascript复制#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main() {
Mat image;
image = imread("lena.jpg");
if (image.empty()) {
printf("src is empty");
return -1;
}
// 调用摄像头
VideoCapture capture(0);
while (true)
{
Mat frame;
capture>>frame;
// 图像转为灰度
cvtColor(frame, frame, COLOR_BGR2GRAY);
// 3*3内核降噪
blur(frame, frame, Size(7, 7));
// canny边缘检测
Canny(frame,
frame, 0, 30, 3);
imshow("frame by frame", frame);
waitKey(10);
}
return 0;
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/126244.html原文链接:https://javaforall.cn