文章目录

目录

文章目录

前言

一、opencv接口

二、调用海康机器人工业相机

1.cv2.VideoCapture()接口使用海康机器人工业相机的前提

2.cv2.VideoCapture()调用海康机器人工业相机进行显示的完整过程

总结


 

前言

夭寿啦!夭寿啦!调用海康工业相机简直太方便了!!!

海康工业相机可以直接通过opencv的接口直接读取图像数据了,省掉了好多好多的调用相机的步骤,这对于我们只需要调用工业相机获取图像数据来说是一个太大的好消息了!!!


 

一、opencv接口

使用 opencv 的官方接口 VideoCapture() 接口直接调用海康工业相机,接下来简单介绍下  VideoCapture() 这个接口。(本人使用 python 所以使用 python 下的接口方式进行介绍)

接口:cv2.VideoCapture()

作用:获取本地目录下的视频文件或者使用本电脑的摄像头获取实时数据;

          本文需要使用该接口调用海康工业相机;

参数:

参数写入“0”,调用的是本地电脑摄像头

参数写入其他数值,调用的也是外接摄像头,并且是系统中directshow中枚举到的相机的顺序序号;

二、调用海康机器人工业相机

1.cv2.VideoCapture()接口使用海康机器人工业相机的前提

海康工业相机使用 cv2.VideoCapture()调用,有一定前提,就是需要安装 directshow 的插件,实现在 directshow 软件中调用海康工业相机,然后就可以使用  cv2.VideoCapture()接口调用海康工业相机,具体下载和安装海康工业相机 SDK 请参考本人博客: python调用海康工业相机并用opencv显示(整体实现)

directshow 插件的安装步骤如下:

1、本人安装海康工业相机SDK 是在默认目录下,故该第三方插件 directshow 在如下目录:

2、使用管理员权限运行 InstallDSSvc_x64.bat (本人使用的是64位系统,运行代码也是基于64位运行,故而使用该版本,32位系统在上一目录中也存在,大家可以自行安装,后续使用步骤相同)

运行后会有以上界面(命令行显示,无需操作)

2.cv2.VideoCapture()调用海康机器人工业相机进行显示的完整过程

安装完成后,可以直接调用啦,下面我们开始调用!

1、先确认连接相机的顺序;

使用directshow的运行客户端即可查看顺序,如下所示,使用 graphedt.exe 运行后有如下界面;

点击红色框图中的按钮,会有如下界面;

如图所示,找到上图中的 Video Capture Sources 点击,即可获取到可枚举到的相机顺序,依次是1——> 10排序,本次使用相机型号 MV-CA013-21UC该型号相机排序位于第7位,故而写入参数为7;该型号相机的分辨率是1280X1024,并且获取从该客户端获取到该相机当前帧率为15帧,使用 RGB 图像像素格式;

 

2、使用代码调用:

import cv2

cap = cv2.VideoCapture(7)  #调用摄像头‘0’一般是打开电脑自带摄像头,其他值是打开外部摄像头(只有一个摄像头的情况)

if False == cap.isOpened():
    print(0)
else:
    print(1)

以上代码中,如果调用成功,返回 1 , 如果调用失败,返回 0;

3、设置获取图像的大小和帧率

cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)  # 设置图像宽度
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1024)  # 设置图像高度
cap.set(cv2.CAP_PROP_FPS , 15)   # 设置帧率

因为directshow 获取到的图像大小均为 640X480 大小,所以需要我们自行设定图像大小

4、接下来需要对图像进行获取和显示了,代码如下:

# 显示图像
while True:
    ret, frame = cap.read()
    # print(ret)  #
    ########图像不处理的情况
    frame_1 = cv2.resize(frame , (640 , 512))
    cv2.imshow("frame", frame_1)

    input = cv2.waitKey(1)
    if input == ord('q'):
        break

cap.release()  # 释放摄像头
cv2.destroyAllWindows()  # 销毁窗口

5、运行代码之后,即可调用海康工业相机显示获取到的数据图像;

6、完整代码:

import cv2

cap = cv2.VideoCapture(1)  #调用摄像头‘0’一般是打开电脑自带摄像头,‘1’是打开外部摄像头(只有一个摄像头的情况)

if False == cap.isOpened():
    print(0)
else:
    print(1)

cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)  # 设置图像宽度
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1024)  # 设置图像高度
cap.set(cv2.CAP_PROP_FPS , 15)   # 设置帧率
# 显示图像
while True:
    ret, frame = cap.read()
    # print(ret)  #
    ########图像不处理的情况
    frame_1 = cv2.resize(frame , (640 , 512))
    cv2.imshow("frame", frame_1)

    input = cv2.waitKey(1)
    if input == ord('q'):
        break

cap.release()  # 释放摄像头
cv2.destroyAllWindows()  # 销毁窗口

 


总结

如上整个过程,即可完成使用 cv2.VideoCapture()接口对海康工业相机的调用,并通过 opencv 接口对图像数据进行显示!!! 

GitHub 加速计划 / opencv31 / opencv
145
15
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:3 个月前 )
d9a139f9 Animated WebP Support #25608 related issues #24855 #22569 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake 2 天前
09030615 V4l default image size #25500 Added ability to set default image width and height for V4L capture. This is required for cameras that does not support 640x480 resolution because otherwise V4L capture cannot be opened and failed with "Pixel format of incoming image is unsupported by OpenCV" and then with "can't open camera by index" message. Because of the videoio architecture it is not possible to insert actions between CvCaptureCAM_V4L::CvCaptureCAM_V4L and CvCaptureCAM_V4L::open so the only way I found is to use environment variables to preselect the resolution. Related bug report is [#25499](https://github.com/opencv/opencv/issues/25499) Maybe (but not confirmed) this is also related to [#24551](https://github.com/opencv/opencv/issues/24551) This fix was made and verified in my local environment: capture board AVMATRIX VC42, Ubuntu 20, NVidia Jetson Orin. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [X] I agree to contribute to the project under Apache 2 License. - [X] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [X] The PR is proposed to the proper branch - [X] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake 3 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐