OpenCV:如何从以太网摄像机捕获帧 [英] OpenCV: How to capture frames from an Ethernet camera

查看:304
本文介绍了OpenCV:如何从以太网摄像机捕获帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前已经对USB网络摄像头进行了编程,其唯一目的是从摄像机获取实时帧并显示在窗口中.为此,我使用了cvCaptureFromCAM,它对USB摄像头效果很好(请参见下面的代码).

I have earlier programmed USB webcam, where the sole aim is to get the live frames from the camera and display in a window. I used cvCaptureFromCAM for that purpose, which worked fine for USB Camera(see code below).

我想知道如何从千兆位以太网摄像机捕获帧?我想我需要使用一些API从某些默认IP地址捕获帧.有人可以指出我正确的方向吗?

I want to know how do I capture frames from a Gigabit Ethernet camera? I guess I need to capture frames from some default IP address using some API. Can some one point me to right direction?

我将在Intel i3处理器的Windows 7上将C ++与OpenCV一起使用.

I will be using C++ with OpenCV on Windows 7 on an Intel i3 processor.

#include "cv.h"
#include "highgui.h"
#include <stdio.h>

// A Simple Camera Capture Framework 
int main() {
    CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
    if ( !capture ) {
        fprintf( stderr, "ERROR: capture is NULL \n" );
        getchar();
        return -1;
    }

    // Create a window in which the captured images will be presented
    cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );

    // Show the image captured from the camera in the window and repeat
    while ( 1 ) {
        // Get one frame
        IplImage* frame = cvQueryFrame( capture );

        if ( !frame ) {
            fprintf( stderr, "ERROR: frame is null...\n" );
            getchar();
            break;
        }

        cvShowImage( "mywindow", frame );

        // Do not release the frame!
        // If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
        // remove higher bits using AND operator
        if ( (cvWaitKey(10) & 255) == 27 ) break;
    }

    // Release the capture device housekeeping
    cvReleaseCapture( &capture );
    cvDestroyWindow( "mywindow" );
    return 0;
}

更新

因此,现在我可以在供应商提供的软件GUI中显示实时图像.但是我仍然想使用摄像机的IP地址显示图像(可能还有视频).

So now I am able to display the live images in the vendor provided software GUI. But still I want to display the image (and possibly video) using the IP address of the camera.

当我知道摄像机的IP地址时,为什么不能访问摄像机发送并在浏览器中显示的数据(图像)?我尝试在浏览器(192.169.2.4)上输入摄像机的ip地址(即192.169.2.3),但显示找不到页面".这是什么意思?

When I know the IP address of the camera, why can't I access the data (images) sent by the camera and display on browser? I tried typing the ip address of the camera (i.e 192.169.2.3) on my browser (192.169.2.4), but it say "page not found". What does it mean?

推荐答案

您可以使用genIcam API来做到这一点. genIcam是相机的通用接口(USB,GigE,CameraLink等).它由多个模块组成,但是您最关心的是GenTL(传输层).您可以阅读有关GenTL文档的更多信息 HERE .为了简化过程,我建议使用Basler API或Baumer API,它们是GenTL使用者(生产者和使用者在GenTL文档中进行了描述).我使用了堡盟API,但两者都可以使用.

You can do this by using the genIcam API. genIcam is a generic interface for cameras (USB, GigE, CameraLink, etc.). It consists of multiple modules but what you are most concerned with is GenTL (transport layer). You can read more on GenTL documentation HERE. To make the process easier, I recommend using either the Basler API or the Baumer API, which are GenTL consumers (producers and consumers are described in the GenTL documentation). I used the Baumer API, but both will work.

注意:我正在使用堡盟HXG20单反相机.

NOTE: I'm using a Baumer HXG20 mono camera.

要下载和安装的内容

  1. Visual Studios社区版本(我使用了2015 LINK )
  2. Baumer GAPI SDK, LINK
  3. openCV(这是一个YouTube教程,用于为c ++构建openCV 3)此处
  1. Visual Studios Community eddition (I used 2015 LINK)
  2. Baumer GAPI SDK, LINK
  3. openCV (here is a youtube tutorial to build openCV 3 for c++) HERE

带有CAMERA EXPLORER的测试相机

最好使用Camera Explorer程序检查您的网络接口卡(NIC)和GigE相机是否正常工作并与相机一起玩.您可能需要在NIC上启用巨型数据包.您也可以使用IPconfig程序配置摄像机IP.我使用DHCP设置,但您也可以为相机和NIC使用静态IP.

It's a good idea to check that your network interface card (NIC) and GigE camera are working and play around with the camera by using the Camera Explorer program. You may need to enable Jumbo Packets on your NIC. You can configure the camera IP using the IPconfig program as well. I use the DHCP setting but you can also use a static IP for your camera and NIC.

设置视觉工作室

《堡盟GAPI SDK程序员指南》(第4章)中介绍了设置系统环境变量和配置Visual Studio的步骤,该指南位于以下目录中

The steps for setting your system environment variables and configuring Visual studios are described in the Baumer GAPI SDK programmers guide (chapter 4), which is located in the following directory

C:\Program Files\Baumer\Baumer GAPI SDK\Docs\Programmers_Guide

  • 检查是否具有以下系统变量(如果使用64位版本),或者根据需要创建变量(请参阅程序员指南中的4.3.1节).

  • Check that you have the following system variable (if using 64 bit version), or create the variable if needed (refer to section 4.3.1 in the programmer's guide).

  • 名称= GENICAM_GENTL64_PATH
  • 值= C:\Program Files\Baumer\Baumer GAPI SDK\Components\Bin\x64\
  • name = GENICAM_GENTL64_PATH
  • value = C:\Program Files\Baumer\Baumer GAPI SDK\Components\Bin\x64\

在Visual Studio中,创建一个新的C ++项目并更新以下属性(请参阅程序员指南中的4.4.1节).

In visual Studios, create a new C++ project and update the following properties (refer to section 4.4.1 in the programmer's guide).

  • C/C ++>常规>其他包含目录= C:\Program Files\Baumer\Baumer GAPI SDK\Components\Dev\C++\Inc
  • 链接器>常规>其他库目录= C:\Program Files\Baumer\Baumer GAPI SDK\Components\Dev\C++\Lib\x64
  • 链接器>输入>其他依赖关系= bgapi2_genicam.lib
  • 构建事件>构建后事件>命令行= copy "C:\Program Files\Baumer\Baumer GAPI SDK\Components\Bin\x64"\*.* .\
  • C/C++ > General > Additional Include Directories = C:\Program Files\Baumer\Baumer GAPI SDK\Components\Dev\C++\Inc
  • Linker > General > Additional Library Directories = C:\Program Files\Baumer\Baumer GAPI SDK\Components\Dev\C++\Lib\x64
  • Linker > Input > Additional Dependencies = bgapi2_genicam.lib
  • Build Events > Post-Build Event > Command Line = copy "C:\Program Files\Baumer\Baumer GAPI SDK\Components\Bin\x64"\*.* .\

创建一个.CPP文件以在OPENCV窗口中显示图像流

最简单的入门方法是使用堡盟GAPI SDK中提供的示例代码之一,并对其进行修改以添加openCV功能.使用的示例代码是005_PixelTransformation,位于此处

The simplest way to get started is to use one of the example codes provided in the Baumer GAPI SDK and modify it to add the openCV functionality. The example code to use is 005_PixelTransformation, which is located here

C:\Program Files\Baumer\Baumer GAPI SDK\Components\Examples\C++\src\0_Common\005_PixelTransformation

将此.cpp文件复制并粘贴到项目源目录中,并确保可以生成和编译.它应该捕获8张图像,并为每张图像从前6行打印出前6个像素值.

Copy and paste this .cpp file into your project source directory and make sure you can build and compile. It should capture 8 images and print out the first 6 pixel values from the first 6 lines for each image.

将以下这些#include语句添加到.cpp源文件中:

Add these #include statements to the .cpp source file:

#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\video\video.hpp>

main()函数的开头添加这些变量声明

Add these variable declarations at the beginning of the main() function

// OPENCV VARIABLE DECLARATIONS
cv::VideoWriter cvVideoCreator;                 // Create OpenCV video creator
cv::Mat openCvImage;                            // create an OpenCV image
cv::String videoFileName = "openCvVideo.avi";   // Define video filename
cv::Size frameSize = cv::Size(2048, 1088);      // Define video frame size (frame width x height)
cvVideoCreator.open(videoFileName, CV_FOURCC('D', 'I', 'V', 'X'), 20, frameSize, true); // set the codec type and frame rate

在原始005_PixelTransformation.cpp文件中,第569行具有for循环,该循环循环显示8张图像,显示为for(int i = 0; i < 8; i++).我们希望将其更改为连续运行.我是通过将其更改为一个while循环

In the original 005_PixelTransformation.cpp file, line 569 has a for loop that loops over 8 images, which says for(int i = 0; i < 8; i++). We want to change this to run continuously. I did this by changing it to a while loop that says

while (pDataStream->GetIsGrabbing())

在我们新的while循环中,有一个if语句,用于检查Pixel格式是'Mono'(灰度)还是彩色.在原始文件中,它从第619行开始,在692处结束.紧接ifelse语句花括号之后,并且在pImage->Release();语句之前,我们需要添加openCV部分以将图像显示到一个窗口.添加以下代码行

Within our new while loop, there's an if statement that checks if the Pixel format is 'Mono' (greyscale) or color. In the original file, it starts at line 619 and ends at 692. Directly after the if and else statement braces are closed, and before the pImage->Release(); statement, we need to add the openCV portion to display the images to a window. Add the following lines of code

}   // This is the closing brace for the 'else color' statement 

// OPEN CV STUFF
openCvImage = cv::Mat(pTransformImage->GetHeight(), pTransformImage->GetWidth(), CV_8U, (int *)pTransformImage->GetBuffer());

// create OpenCV window ----
cv::namedWindow("OpenCV window: Cam", CV_WINDOW_NORMAL);

//display the current image in the window ----
cv::imshow("OpenCV window : Cam", openCvImage);
cv::waitKey(1);

要注意的一件事是openCvImage对象中的像素格式.我的相机是单声道8位,所以我需要指定CV_8U.如果您的相机是RGB或10位像素,则需要提供正确的格式(请参见openCV文档

One thing to note is the pixel format in the openCvImage object. My camera is a mono 8 bit, so I need to specify CV_8U. If your camera is RGB or 10-bit pixels, you need to provide the correct format (see the openCV documentation HERE).

您可以参考其他示例来调整相机参数.

you can refer to the other examples for adjusting camera parameters.

现在,一旦构建和编译,就应该打开一个openCV窗口来显示摄像机图像!

now once you build and compile, you should have an openCV window open that displays the camera images!

为更多人投票而欢呼雀跃!!!! (花了很多时间开始工作,所以让我忙起来!!)

THUMPS UP FOR MORE UP VOTES PEOPLE!!!! (This took a lot of time to get working so hook me up!!!)

这篇关于OpenCV:如何从以太网摄像机捕获帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆