开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 11397|回复: 17
收起左侧

[技术专题] 海康工业相机 Opencv 调用获取图像(源码 中文注释)

[复制链接]
结帖率:89% (79/89)
发表于 2022-9-11 20:45:28 | 显示全部楼层 |阅读模式   江苏省苏州市
QQ图片20220911204222.png
[C++] 纯文本查看 复制代码
class MVS {
public:
        //opencv 图像
        cv::Mat Image;
private:
        //设备信息列表结构体  支持最多256个设备 
        MV_CC_DEVICE_INFO_LIST stDeviceList;
        //设备句柄
        void* Handle = NULL;
        //图像数据参数
        MVCC_INTVALUE stParam;
        //图片缓冲区
        unsigned char* pData = NULL;
        //输出帧的信息
        MV_FRAME_OUT_INFO_EX stImageInfo = { 0 };

        int RGB2BGR(unsigned char* pRgbData, unsigned int nWidth, unsigned int nHeight) {
                if (NULL == pRgbData) {
                        return MV_E_PARAMETER;
                }
                for (unsigned int j = 0; j < nHeight; j++) {
                        for (unsigned int i = 0; i < nWidth; i++) {
                                unsigned char red = pRgbData[j * (nWidth * 3) + i * 3];
                                pRgbData[j * (nWidth * 3) + i * 3] = pRgbData[j * (nWidth * 3) + i * 3 + 2];
                                pRgbData[j * (nWidth * 3) + i * 3 + 2] = red;
                        }
                }
                return MV_OK;
        }
        bool Convert2Mat(MV_FRAME_OUT_INFO_EX* pstImageInfo, unsigned char* pData) {
                cv::Mat srcImage;
                if (pstImageInfo->enPixelType == PixelType_Gvsp_Mono8) {
                        srcImage = cv::Mat(pstImageInfo->nHeight, pstImageInfo->nWidth, CV_8UC1, pData);
                } else if (pstImageInfo->enPixelType == PixelType_Gvsp_RGB8_Packed) {
                        if (RGB2BGR(pData, pstImageInfo->nWidth, pstImageInfo->nHeight) == MV_OK) { srcImage = cv::Mat(pstImageInfo->nHeight, pstImageInfo->nWidth, CV_8UC3, pData); } else {
                                return false;
                        }
                } else {
                        return false;
                }
                if (NULL == srcImage.data) {
                        return false;
                }
                Image = srcImage.clone();
                srcImage.release();
                return true;
        }

public:
        //枚举设备 成功返回设备数量   常用  USB 与 GIGE
        unsigned int Enumerate(unsigned int nTLayerType= MV_GIGE_DEVICE | MV_USB_DEVICE) {
                memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
                if (MV_CC_EnumDevices(nTLayerType, &stDeviceList) != MV_OK) { return  0; } else {return stDeviceList.nDeviceNum;}
        }
        //取主要版本
        unsigned short Get_device_MajorVer(unsigned int nID) {return stDeviceList.pDeviceInfo[nID]->nMajorVer;}
        //取次要版本
        unsigned short Get_device_MinorVer(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->nMinorVer; }
        //取高MAC地址
        unsigned int Get_device_MacAddrHigh(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->nMacAddrHigh; }
        //取低MAC地址 
        unsigned int Get_device_MacAddrLow(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->nMacAddrLow; }
        //取设备传输层协议类型
        unsigned int Get_device_TLayerType(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->nTLayerType; }
        //取GigE设备信息 IP配置选项
        unsigned int Get_device_GigE_IpCfgOption(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stGigEInfo.nIpCfgOption; }
        //取GigE设备信息 当前IP配置
        unsigned int Get_device_GigE_IpCfgCurrent(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stGigEInfo.nIpCfgCurrent; }
        //取GigE设备信息 当前IP地址 
        unsigned int Get_device_GigE_CurrentIp(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stGigEInfo.nCurrentIp;}
        //取GigE设备信息 当前子网掩码
        unsigned int Get_device_GigE_CurrentSubNetMask(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stGigEInfo.nCurrentSubNetMask; }
        //取GigE设备信息 当前网关
        unsigned int Get_device_GigE_DefultGateWay(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stGigEInfo.nDefultGateWay; }
        //取GigE设备信息 制造商名称
        unsigned char* Get_device_GigE_ManufacturerName(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stGigEInfo.chManufacturerName; }
        //取GigE设备信息 型号名称
        unsigned char* Get_device_GigE_ModelName(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stGigEInfo.chModelName; }
        //取GigE设备信息 设备版本
        unsigned char* Get_device_GigE_DeviceVersion(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stGigEInfo.chDeviceVersion; }
        //取GigE设备信息 制造商的具体信息
        unsigned char* Get_device_GigE_ManufacturerSpecificInfo(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stGigEInfo.chManufacturerSpecificInfo; }
        //取GigE设备信息 序列号
        unsigned char* Get_device_GigE_SerialNumber(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stGigEInfo.chSerialNumber; }
        //取GigE设备信息 用户自定义名称  
        unsigned char* Get_device_GigE_UserDefinedName(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stGigEInfo.chUserDefinedName; }
        //取GigE设备信息 网口IP地址    
        unsigned int Get_device_GigE_NetExport(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stGigEInfo.nNetExport; }
        //取USB设备信息 控制输入端点   
        unsigned char Get_device_USB_InEndPoint(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.CrtlInEndPoint; }
        //取USB设备信息 控制输出端点  
        unsigned char Get_device_USB_OutEndPoint(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.CrtlOutEndPoint; }
        //取USB设备信息 流端点
        unsigned char Get_device_USB_StreamEndPoint(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.StreamEndPoint; }
        //取USB设备信息 事件端点
        unsigned char Get_device_USB_EventEndPoint(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.EventEndPoint; }
        //取USB设备信息 供应商ID号   
        unsigned short Get_device_USB_idVendor(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.idVendor; }
        //取USB设备信息 产品ID号   
        unsigned short Get_device_USB_idProduct(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.idProduct; }
        //取USB设备信息 设备索引号   
        unsigned int Get_device_USB_DeviceNumber(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.nDeviceNumber; }
        //取USB设备信息 设备GUID号  
        unsigned char* Get_device_USB_DeviceGUID(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.chDeviceGUID; }
        //取USB设备信息 供应商名字  
        unsigned char* Get_device_USB_VendorName(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.chVendorName; }
        //取USB设备信息  型号名字  
        unsigned char* Get_device_USB_ModelName(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.chModelName; }
        //取USB设备信息  家族名字
        unsigned char* Get_device_USB_FamilyName(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.chFamilyName; }
        //取USB设备信息  设备版本
        unsigned char* Get_device_USB_DeviceVersion(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.chDeviceVersion; }
        //取USB设备信息  制造商名字
        unsigned char* Get_device_USB_ManufacturerName(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.chManufacturerName; }
        //取USB设备信息  序列号
        unsigned char* Get_device_USB_SerialNumber(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.chSerialNumber;}
        //取USB设备信息 支持的USB协议
        unsigned int Get_device_USB_bcdUSB(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.nbcdUSB; }
        //取USB设备信息 设备地址
        unsigned int Get_device_USB_DeviceAddress(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stUsb3VInfo.nDeviceAddress; }
        //取CameraLink设备信息 端口号
        unsigned char* Get_device_deviceCameraLink_PortID(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stCamLInfo.chPortID; }
        //取CameraLink设备信息 型号名字
        unsigned char* Get_device_deviceCameraLink_ModelName(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stCamLInfo.chModelName; }
        //取CameraLink设备信息 名称
        unsigned char* Get_device_deviceCameraLink_FamilyName(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stCamLInfo.chFamilyName; }
        //取CameraLink设备信息 设备版本
        unsigned char* Get_device_deviceCameraLink_DeviceVersion(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stCamLInfo.chDeviceVersion; }
        //取CameraLink设备信息 制造商名字
        unsigned char* Get_device_deviceCameraLink_ManufacturerName(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stCamLInfo.chManufacturerName; }
        //取CameraLink设备信息 制造商名字
        unsigned char* Get_device_deviceCameraLink_SerialNumber(unsigned int nID) { return stDeviceList.pDeviceInfo[nID]->SpecialInfo.stCamLInfo.chSerialNumber; }
        //创建设备句柄
        bool Create(unsigned int nID=0) { if (MV_CC_CreateHandle(&Handle, stDeviceList.pDeviceInfo[nID]) != MV_OK) { return  false; } else { return true; } }
        //打开设备
        bool Open() {if( MV_CC_OpenDevice(Handle) != MV_OK) { return  false; } else { return true; }}
        //关闭设备
        bool Close() { if (MV_CC_CloseDevice(Handle) != MV_OK) { return  false; } else { return true; }}
        //设置曝光 15 - 9999500
        bool SetExposureTime(float value) { if (MV_CC_SetFloatValue(Handle, "ExposureTime", value) != MV_OK) { return  false; } else { return true; }}
        //像素格式  Opencv支持  Mono8(黑白)  或   RGB8Packed(彩色)
        bool SetPixelFormat( bool value = false) {
                if (value) {
                        if (MV_CC_SetEnumValueByString(Handle, "PixelFormat", "Mono8") != MV_OK) { return  false; } else { return true; }
                } else {
                        if (MV_CC_SetEnumValueByString(Handle, "PixelFormat", "RGB8Packed") != MV_OK) { return  false; } else { return true; }
                }
        }
        //检测网络最优封装尺寸(仅适用于GigE摄像头)
        bool OptimizedSize(unsigned int nID) {
        
                if (stDeviceList.pDeviceInfo[nID]->nTLayerType == MV_GIGE_DEVICE) {
                        int nPacketSize = MV_CC_GetOptimalPacketSize(Handle);
                        if (nPacketSize > 0) {
                                if (MV_CC_SetIntValue(Handle, "GevSCPSPacketSize", nPacketSize) == MV_OK) {
                                        return  true;
                                }
                        } 
                } 
                return  false;
        }
        // 设置触发模式为关闭  0 是关闭
        bool SetTriggerMode( unsigned int nValue=0) { if(MV_CC_SetEnumValue(Handle, "TriggerMode", nValue) != MV_OK) { return  false; } else { return true; }}

        
        //创建图像数据缓冲区
        bool StartGrabbing() {
                memset(&stParam, 0, sizeof(MVCC_INTVALUE));
                if (MV_CC_GetIntValue(Handle, "PayloadSize", &stParam) == MV_OK) {
                        if (MV_CC_StartGrabbing(Handle) != MV_OK) { return  false; } else {
                                memset(&stImageInfo, 0, sizeof(MV_FRAME_OUT_INFO_EX));
                                pData = (unsigned char*)malloc(sizeof(unsigned char) * (stParam.nCurValue));
                                if (pData == NULL) { return  false; }
                                return true;
                        }
                } else {
                        { return  false; }
                }


        }
        
        //获取一帧图片
        bool GetimagData(unsigned int nMsec=1000) {
                if (MV_CC_GetOneFrameTimeout(Handle, pData, stParam.nCurValue, &stImageInfo, nMsec) != MV_OK)
                {
                        return  false;
                } else {
                        return Convert2Mat(&stImageInfo, pData);
                }
        }

        //析函数 释放资源
        ~MVS() {
                //停止数据流
                MV_CC_StopGrabbing(Handle);
                //关闭设备
                MV_CC_CloseDevice(Handle);
                //销毁设备句柄
                if (Handle != NULL) {
                        MV_CC_DestroyHandle(Handle);
                }
                if (pData != NULL) {
                        free(pData);
                }
                Image.release();
        }
};


int main()
{
        //创建海康相机对象
        MVS 海康相机;
        //枚举相机 如果没有返回-1
        if (!海康相机.Enumerate()) {
                return -1;
        }
        //创建  相机0 的对象  失败返回 -1
        if (!海康相机.Create(0)) {
                return -1;
        }

        //打开 相机0  失败返回 -1
        if (!海康相机.Open()) {
                return -1;
        }
        //设置相机曝光时间  1000
        海康相机.SetExposureTime(85930);
        //设置像素 黑白  默认黑白
        海康相机.SetPixelFormat();

        //优化网口相机 封装尺寸  USB  忽略
        //海康相机.OptimizedSize(0);

        //关闭触发模式  失败返回 -1
        if (!海康相机.SetTriggerMode()) {
                return -1;
        }
        //创建图像数据缓冲区 用于接收来自相机图像数据 识别返回-1;
        if (!海康相机.StartGrabbing()) {
                return -1;
        }
        cv::namedWindow("小白鼠", cv::WINDOW_NORMAL);
        while (海康相机.GetimagData()) {
        
                //显示相机图像
                cv::imshow("小白鼠", 海康相机.Image);
                //等待按键输入

                if (cv::waitKey(1)!=-1)  break;

        }

        //相机一定要要正常退出  要不会显示被占用
        //自动析结构 释放资源
}


签到天数: 6 天

发表于 2023-9-19 20:24:41 | 显示全部楼层   内蒙古自治区呼和浩特市
6666666666666666666666666666
回复 支持 反对

使用道具 举报

发表于 2023-3-14 23:13:04 | 显示全部楼层   四川省眉山市
谢谢分享
回复 支持 反对

使用道具 举报

结帖率:0% (0/2)
发表于 2022-9-26 10:02:34 | 显示全部楼层   湖南省怀化市
大神分享辛苦了!
回复 支持 反对

使用道具 举报

结帖率:0% (0/2)
发表于 2022-9-26 09:55:56 | 显示全部楼层   湖南省怀化市
大神分享辛苦了!
回复 支持 反对

使用道具 举报

签到天数: 6 天

发表于 2022-9-26 09:53:33 | 显示全部楼层   湖北省武汉市
大神分享辛苦了!
回复 支持 反对

使用道具 举报

结帖率:0% (0/1)

签到天数: 2 天

发表于 2022-9-23 18:39:07 | 显示全部楼层   山东省菏泽市
大神分享辛苦了!
回复 支持 反对

使用道具 举报

发表于 2022-9-21 11:51:17 高大上手机用户 | 显示全部楼层   甘肃省酒泉市
6666666666666
回复 支持 反对

使用道具 举报

结帖率:80% (4/5)

签到天数: 10 天

发表于 2022-9-16 11:05:43 | 显示全部楼层   广东省东莞市
哪里可以下载支持库或dll?还没见过海康工业相机易语言调用的例子。

点评

群里寻   江苏省苏州市  发表于 2022-9-16 13:44
回复 支持 反对

使用道具 举报

结帖率:0% (0/3)
发表于 2022-9-15 03:50:39 | 显示全部楼层   四川省广元市
老大 日志DLL怎么调用  给个例子  谢谢

点评

其实官方 提供了个手册 的   江苏省苏州市  发表于 2022-9-15 16:52
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 793400750,邮箱:wp@125.la
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表