68、ubunut/window使用海康彩色工业相机

基本思想:刚买了一块海康工业相机,需要在jetson上调用使用,所以记录一下配置方法,然后结合开发使用

一、先使用window软件调用一下,是否可用,进入官网 海康机器人-让机器更智能,让智能更普惠

下载桌面端软件 海康机器人-机器视觉-下载中心

二、首先使用visual studio 2022进行调用

配置vs2022

main.cpp

#include <stdio.h>
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <opencv2/video/video.hpp>
#include <opencv2/opencv.hpp>
#include "camera_class.h"

int main()
{
    Mat img;
    int key;
    int camera_width = 2600;
    int camera_height = 2160;
    camera *cam=new camera( camera_width,  camera_height);
 
    cam->start_cam( );
    while (1)
    {
        cam->get_pic(&img);
        imshow("test", img);
        key = waitKey(1);
        if (key == 27)
        {
            cam->close_cam();
            break;
        }

    }
    delete cam;
}

camera_class.h

#pragma once
#ifndef CAMERA_CLASS_H_INCLUDED
#define CAMERA_CLASS_H_INCLUDED
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "MvCameraControl.h"
#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <opencv2/video/video.hpp>
using namespace std;
using namespace cv;

class camera {
private:
    void* handle;
    bool g_bExit;
    int nRet;
    unsigned int g_nPayloadSize;
    unsigned char* pDataForRGB;
    MV_CC_DEVICE_INFO* pDeviceInfo;
    MV_CC_DEVICE_INFO_LIST stDeviceList;
    MVCC_INTVALUE stParam;
    MV_FRAME_OUT stOutFrame;
    MV_CC_PIXEL_CONVERT_PARAM CvtParam;
public:
    camera(int camera_width, int camera_height);
    void PrintDeviceInfo();
    void close_cam();
    void start_cam();
    void get_pic(Mat* srcimg);
    void re_iso();
};
#endif // CAMERA_CLASS_H_INCLUDED

camera_class.cpp

#include "camera_class.h"
camera::camera(int camera_width, int camera_height)
{
    nRet = MV_OK;
    handle = NULL;
    g_bExit = false;
    g_nPayloadSize = 0;
    pDataForRGB = (unsigned char*)malloc(camera_width * camera_height * 4 + 2048);
    memset(&stParam, 0, sizeof(MVCC_INTVALUE));
    CvtParam = { 0 };
    stOutFrame = { 0 };
    memset(&stOutFrame, 0, sizeof(MV_FRAME_OUT));
}
void camera::start_cam()
{

    memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
    nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, &stDeviceList);
    if (stDeviceList.nDeviceNum > 0)
    {
        for (unsigned int i = 0; i < stDeviceList.nDeviceNum; i++)
        {
            pDeviceInfo = stDeviceList.pDeviceInfo[i];
            if (NULL == pDeviceInfo)
            {
                break;
            }
            PrintDeviceInfo();
        }
    }
    else {
        cout << "Find no Device" << endl;
    }
    unsigned int nIndex = 0;
    MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
    MV_CC_OpenDevice(handle);
    if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE)
    {
        int nPacketSize = MV_CC_GetOptimalPacketSize(handle);
        if (nPacketSize > 0)
        {
            MV_CC_SetIntValue(handle, "GevSCPSPacketSize", nPacketSize);
        }
        else {
            cout << "Warning: Get Packet Size fail" << endl;
        }
    }
    MVCC_ENUMVALUE  p = { 0 };
    MVCC_STRINGVALUE st;
    MV_CC_GetStringValue(handle, "DeviceModelName", &st);
    cout << "DeviceModelName: " << st.chCurValue << endl;
    MV_CC_GetStringValue(handle, "DeviceVersion", &st);
    cout << "DeviceVersion:\t" << st.chCurValue << endl;
    MV_CC_GetEnumValue(handle, "DeviceScanType", &p);
    if (p.nCurValue == 0)
    {
        cout << "DeviceScanType:\t" << "Areascan" << endl;

    }
    else {
        cout << "DeviceScanType:\t" << "Linescan" << endl;
    }
    MV_CC_SetEnumValue(handle, "TriggerMode", 0);
    MV_CC_SetEnumValue(handle, "PixelFormat", 0x0210001F);
    MV_CC_SetEnumValue(handle, "GainAuto", 1);
    MV_CC_SetFloatValue(handle, "Gamma", 0.8);
    MV_CC_SetBoolValue(handle, "GammaEnable", 1);
    MV_CC_SetEnumValue(handle, "BalanceWhiteAuto", 2);
    MV_CC_SetEnumValue(handle, "ExposureAuto", 1);
    MV_CC_GetIntValue(handle, "PayloadSize", &stParam);
    g_nPayloadSize = stParam.nCurValue;
    nRet = MV_CC_StartGrabbing(handle);
    if (MV_OK == nRet)
        cout << "Start Grabbing !" << endl;
    cout << "\nPress ESC to exit.\n";
}
void camera::PrintDeviceInfo()
{
    if (NULL == pDeviceInfo)
    {
        cout << "null point" << endl;
    }
    if (pDeviceInfo->nTLayerType == MV_GIGE_DEVICE)
    {
        int nIp1 = ((pDeviceInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
        int nIp2 = ((pDeviceInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
        int nIp3 = ((pDeviceInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
        int nIp4 = (pDeviceInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
        cout << "IP:" << nIp1 << "." << nIp2 << "." << nIp3 << "." << nIp4 << endl;
    }
}
void camera::close_cam()
{
    int nRet = MV_CC_StopGrabbing(handle);
    if (MV_OK == nRet)
        cout << "Stopped Grabbing !" << endl;
}
void camera::get_pic(cv::Mat* srcimg)
{
    MV_CC_GetImageBuffer(handle, &stOutFrame, 400);
    CvtParam.enSrcPixelType = stOutFrame.stFrameInfo.enPixelType;
    CvtParam.enDstPixelType = PixelType_Gvsp_RGB8_Packed;
    CvtParam.nHeight = stOutFrame.stFrameInfo.nHeight;
    CvtParam.nWidth = stOutFrame.stFrameInfo.nWidth;
    CvtParam.nDstBufferSize = stOutFrame.stFrameInfo.nWidth * stOutFrame.stFrameInfo.nHeight * 4 + 2048;
    CvtParam.pSrcData = stOutFrame.pBufAddr;
    CvtParam.pDstBuffer = pDataForRGB;
    CvtParam.nSrcDataLen = stOutFrame.stFrameInfo.nFrameLen;
    MV_CC_ConvertPixelType(handle, &CvtParam);
    //printf(" %d %d \n", stOutFrame.stFrameInfo.nHeight, stOutFrame.stFrameInfo.nWidth);
    *srcimg = Mat(stOutFrame.stFrameInfo.nHeight, stOutFrame.stFrameInfo.nWidth, CV_8UC3, pDataForRGB);
    cvtColor(*srcimg, *srcimg, COLOR_RGB2BGR);
    if (NULL != stOutFrame.pBufAddr)
    {
        MV_CC_FreeImageBuffer(handle, &stOutFrame);
    }
}
void camera::re_iso()
{
    MV_CC_SetEnumValue(handle, "BalanceWhiteAuto", 2);
    MV_CC_SetEnumValue(handle, "ExposureAuto", 1);
}


画面

三、ubuntu系统调用

ubuntu@ubuntu:~/Downloads/MvCamCtrlSDK_STD_V4.1.2_231116$ sudo dpkg -i MvCamCtrlSDK_Runtime-4.1.2_x86_64_20231116.deb 
Selecting previously unselected package mvcamctrlsdk.
(Reading database ... 209219 files and directories currently installed.)
Preparing to unpack MvCamCtrlSDK_Runtime-4.1.2_x86_64_20231116.deb ...
Unpacking mvcamctrlsdk (2019-05-30) ...
Setting up mvcamctrlsdk (2019-05-30) ...
Please Input Install Path, Default Path is [/opt]: 


The install path is:/opt
Install MvCamCtrlSDK, Please wait...
Set up the SDK environment...

Adding rules for vendor ID 2bdf.
The /etc/udev/rules.d/80-drivers-SDK-2bdf.rules rule has been created.


Adding rules for virtual serial device.
The /etc/udev/rules.d/80-drivers-SDK-virtualserial.rules rule has been created.

/home/ubuntu/.profile
/home/ubuntu/.bashrc
Starting execute script...
Setting usbfs memory size to 2000
Setting socket maximum buffer size to 10485760
Configuration of the rp_filter.

For more information, read the RHEL knowledge note:
 https://access.redhat.com/knowledge/solutions/53031


Supported modes:
 0 - No source validation (recommended).
 1 - RFC3704 Strict Reverse Path.
 2 - RFC3704 Loose Reverse Path.

Setting the mode to No source validation.

The network stack will be restarted.


/opt/MVS/logserver
Install MvCamCtrlSDK complete!
Tips: You should be launch a new terminal or execute source command for the bash environment!

IDE

ubuntu@ubuntu:~/Downloads/MVS_STD_GML_V2.1.2_231116$ ls
MVS-2.1.2_aarch64_20231116.deb      MVS-2.1.2_i386_20231116.deb
MVS-2.1.2_aarch64_20231116.tar.gz   MVS-2.1.2_i386_20231116.tar.gz
MVS-2.1.2_armhf_20231116.deb        MVS-2.1.2_x86_64_20231116.deb
MVS-2.1.2_armhf_20231116.tar.gz     MVS-2.1.2_x86_64_20231116.tar.gz
MVS-2.1.2_arm-none_20231116.tar.gz  README
ubuntu@ubuntu:~/Downloads/MVS_STD_GML_V2.1.2_231116$ sudo dpkg -i MVS-2.1.2_x86_64_20231116.deb 
[sudo] password for ubuntu: 
Selecting previously unselected package mvs.
(Reading database ... 209233 files and directories currently installed.)
Preparing to unpack MVS-2.1.2_x86_64_20231116.deb ...
Unpacking mvs (2022-10-24) ...
Setting up mvs (2022-10-24) ...
Uninstall MVS,Please wait...
Install MVS,Please wait...
cp: cannot stat '/opt/MVS/bin/fonts/*': No such file or directory
Set up the SDK environment...

Adding rules for vendor ID 2bdf.
The /etc/udev/rules.d/80-drivers-SDK-2bdf.rules rule has been created.


Adding rules for virtual serial device.
The /etc/udev/rules.d/80-drivers-SDK-virtualserial.rules rule has been created.

/home/ubuntu/.profile
/home/ubuntu/.bashrc
create link to dynamic library
Unloading GigEVision Image Filter For Ethernet...
Starting execute script...
Setting usbfs memory size to 2000
Setting socket maximum buffer size to 10485760
Configuration of the rp_filter.

For more information, read the RHEL knowledge note:
 https://access.redhat.com/knowledge/solutions/53031


Supported modes:
 0 - No source validation (recommended).
 1 - RFC3704 Strict Reverse Path.
 2 - RFC3704 Loose Reverse Path.

Setting the mode to No source validation.

The network stack will be restarted.


Unloading MVFG gev Frame Grabber...
Unloading MVFG cxp Frame Grabber...
Unloading MVFG xof Frame Grabber...
Unloading MVFG cml Frame Grabber...
Unloading MVFG Virtual Serial...
Install MVS complete!
Tips: You should be launch a new terminal or execute source command for the bash environment!

测试图片

ubuntu@ubuntu:/opt/MVS/bin$ ./MVS
Must construct a QGuiApplication before accessing a platform theme hint.
Gtk-Message: 15:14:35.231: Failed to load module "canberra-gtk-module"

代码

cmakelists.txt

# cmake needs this line
cmake_minimum_required(VERSION 3.16)

project(untitled10)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
include_directories(/opt/MVS/include)

add_library(libMvCameraControl SHARED IMPORTED)
set_target_properties(libMvCameraControl PROPERTIES IMPORTED_LOCATION /opt/MVS/lib/64/libMvCameraControl.so)

add_compile_options(-std=c++11)

add_executable(untitled10 main.cpp camera_class.cpp)
target_link_libraries(untitled10 ${OpenCV_LIBS} libMvCameraControl  )

main.cpp

#include <stdio.h>
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <opencv2/video/video.hpp>
#include <opencv2/opencv.hpp>
#include "camera_class.h"

int main()
{
    Mat img;
    int key;
    int camera_width = 2600;
    int camera_height = 2160;
    camera *cam=new camera( camera_width,  camera_height);

    cam->start_cam( );
    while (1)
    {
        cam->get_pic(&img);
        imshow("test", img);
        key = waitKey(1);
        if (key == 27)
        {
            cam->close_cam();
            break;
        }

    }
    delete cam;
}

camera_class.h

#pragma once
#ifndef CAMERA_CLASS_H_INCLUDED
#define CAMERA_CLASS_H_INCLUDED
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "MvCameraControl.h"
#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <opencv2/video/video.hpp>
using namespace std;
using namespace cv;

class camera {
private:
    void* handle;
    bool g_bExit;
    int nRet;
    unsigned int g_nPayloadSize;
    unsigned char* pDataForRGB;
    MV_CC_DEVICE_INFO* pDeviceInfo;
    MV_CC_DEVICE_INFO_LIST stDeviceList;
    MVCC_INTVALUE stParam;
    MV_FRAME_OUT stOutFrame;
    MV_CC_PIXEL_CONVERT_PARAM CvtParam;
public:
    camera(int camera_width, int camera_height);
    void PrintDeviceInfo();
    void close_cam();
    void start_cam();
    void get_pic(Mat* srcimg);
    void re_iso();
};
#endif // CAMERA_CLASS_H_INCLUDED

camera_class.cpp


#include "camera_class.h"
camera::camera(int camera_width, int camera_height)
{
    nRet = MV_OK;
    handle = NULL;
    g_bExit = false;
    g_nPayloadSize = 0;
    pDataForRGB = (unsigned char*)malloc(camera_width * camera_height * 4 + 2048);
    memset(&stParam, 0, sizeof(MVCC_INTVALUE));
    CvtParam = { 0 };
    stOutFrame = { 0 };
    memset(&stOutFrame, 0, sizeof(MV_FRAME_OUT));
}
void camera::start_cam()
{

    memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
    nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, &stDeviceList);
    if (stDeviceList.nDeviceNum > 0)
    {
        for (unsigned int i = 0; i < stDeviceList.nDeviceNum; i++)
        {
            pDeviceInfo = stDeviceList.pDeviceInfo[i];
            if (NULL == pDeviceInfo)
            {
                break;
            }
            PrintDeviceInfo();
        }
    }
    else {
        cout << "Find no Device" << endl;
    }
    unsigned int nIndex = 0;
    MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
    MV_CC_OpenDevice(handle);
    if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE)
    {
        int nPacketSize = MV_CC_GetOptimalPacketSize(handle);
        if (nPacketSize > 0)
        {
            MV_CC_SetIntValue(handle, "GevSCPSPacketSize", nPacketSize);
        }
        else {
            cout << "Warning: Get Packet Size fail" << endl;
        }
    }
    MVCC_ENUMVALUE  p = { 0 };
    MVCC_STRINGVALUE st;
    MV_CC_GetStringValue(handle, "DeviceModelName", &st);
    cout << "DeviceModelName: " << st.chCurValue << endl;
    MV_CC_GetStringValue(handle, "DeviceVersion", &st);
    cout << "DeviceVersion:\t" << st.chCurValue << endl;
    MV_CC_GetEnumValue(handle, "DeviceScanType", &p);
    if (p.nCurValue == 0)
    {
        cout << "DeviceScanType:\t" << "Areascan" << endl;

    }
    else {
        cout << "DeviceScanType:\t" << "Linescan" << endl;
    }
    MV_CC_SetEnumValue(handle, "TriggerMode", 0);
    MV_CC_SetEnumValue(handle, "PixelFormat", 0x0210001F);
    MV_CC_SetEnumValue(handle, "GainAuto", 1);
    MV_CC_SetFloatValue(handle, "Gamma", 0.8);
    MV_CC_SetBoolValue(handle, "GammaEnable", 1);
    MV_CC_SetEnumValue(handle, "BalanceWhiteAuto", 2);
    MV_CC_SetEnumValue(handle, "ExposureAuto", 1);
    MV_CC_GetIntValue(handle, "PayloadSize", &stParam);
    g_nPayloadSize = stParam.nCurValue;
    nRet = MV_CC_StartGrabbing(handle);
    if (MV_OK == nRet)
        cout << "Start Grabbing !" << endl;
    cout << "\nPress ESC to exit.\n";
}
void camera::PrintDeviceInfo()
{
    if (NULL == pDeviceInfo)
    {
        cout << "null point" << endl;
    }
    if (pDeviceInfo->nTLayerType == MV_GIGE_DEVICE)
    {
        int nIp1 = ((pDeviceInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
        int nIp2 = ((pDeviceInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
        int nIp3 = ((pDeviceInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
        int nIp4 = (pDeviceInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
        cout << "IP:" << nIp1 << "." << nIp2 << "." << nIp3 << "." << nIp4 << endl;
    }
}
void camera::close_cam()
{
    int nRet = MV_CC_StopGrabbing(handle);
    if (MV_OK == nRet)
        cout << "Stopped Grabbing !" << endl;
}
void camera::get_pic(cv::Mat* srcimg)
{
    MV_CC_GetImageBuffer(handle, &stOutFrame, 400);
    CvtParam.enSrcPixelType = stOutFrame.stFrameInfo.enPixelType;
    CvtParam.enDstPixelType = PixelType_Gvsp_RGB8_Packed;
    CvtParam.nHeight = stOutFrame.stFrameInfo.nHeight;
    CvtParam.nWidth = stOutFrame.stFrameInfo.nWidth;
    CvtParam.nDstBufferSize = stOutFrame.stFrameInfo.nWidth * stOutFrame.stFrameInfo.nHeight * 4 + 2048;
    CvtParam.pSrcData = stOutFrame.pBufAddr;
    CvtParam.pDstBuffer = pDataForRGB;
    CvtParam.nSrcDataLen = stOutFrame.stFrameInfo.nFrameLen;
    MV_CC_ConvertPixelType(handle, &CvtParam);
    printf(" %d %d \n", stOutFrame.stFrameInfo.nHeight, stOutFrame.stFrameInfo.nWidth);
    *srcimg = Mat(stOutFrame.stFrameInfo.nHeight, stOutFrame.stFrameInfo.nWidth, CV_8UC3, pDataForRGB);
    cvtColor(*srcimg, *srcimg, COLOR_RGB2BGR);
    if (NULL != stOutFrame.pBufAddr)
    {
        MV_CC_FreeImageBuffer(handle, &stOutFrame);
    }
}
void camera::re_iso()
{
    MV_CC_SetEnumValue(handle, "BalanceWhiteAuto", 2);
    MV_CC_SetEnumValue(handle, "ExposureAuto", 1);
}

画面我就不放了,这里要注意ubuntu在安装完库之后,重启生效,才能运行代码,

参考:

C++下OPENCV驱动调用海康GigE工业相机_c#连个opencv连接海康gige工业相机-CSDN博客

相关推荐

  1. 115.工业相机SDK开发指南(阅读)

    2024-04-03 15:44:04       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-03 15:44:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-03 15:44:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-03 15:44:04       82 阅读
  4. Python语言-面向对象

    2024-04-03 15:44:04       91 阅读

热门阅读

  1. 七彩云转码系统v12.8二开正式版发布

    2024-04-03 15:44:04       34 阅读
  2. 宝塔面板CentOS Stream 8 x86 下如何安装openlitespeed

    2024-04-03 15:44:04       34 阅读
  3. 【Python BUG】局域网内远程连接mysql错误:1130

    2024-04-03 15:44:04       28 阅读
  4. AI大模型学习的理论基础

    2024-04-03 15:44:04       35 阅读
  5. 26.活锁、饥饿锁

    2024-04-03 15:44:04       32 阅读
  6. JVM为什么使用元空间替换了永久代

    2024-04-03 15:44:04       32 阅读
  7. android HAL层

    2024-04-03 15:44:04       35 阅读