C + + 版 C++版 C++版
MV_CC_GetOneFrameTimeout()
MV_CAMCTRL_API int __stdcall MV_CC_GetOneFrameTimeout ( IN void * handle, IN OUT unsigned char * pData, IN unsigned int nDataSize, IN OUT MV_FRAME_OUT_INFO_EX * pstFrameInfo, unsigned int nMsec )
采用超时机制获取一帧图片,SDK内部等待直到有数据时返回
参数
handle [IN] 设备句柄
pData [OUT] 图像数据接收指针
nDataSize [IN] 接收缓存大小
pstFrameInfo [OUT] 图像信息结构体
nMsec [IN] 等待超时时间
返回
成功,返回MV_OK;
失败,返回错误码
一、设备连接接口流程
对设备进行操作,实现图像采集、参数配置等功能,需要先连接设备(打开设备),具体流程如下图所示。
详细步骤
- (可选)调用 MV_CC_EnumDevices() 枚举子网内指定传输协议对应的所有设备。 可通过nTLayerType在结构 MV_CC_DEVICE_INFO() 中获取设备信息。
- (可选)在打开指定设备前,调用 MV_CC_IsDeviceAccessible() 检查指定设备是否可访问。
- 调用 MV_CC_CreateHandle() 创建设备句柄。
- 调用 MV_CC_OpenDevice() 打开设备。
- (可选)调用 MV_CC_GetAllMatchInfo()以获取设备信息。
- 调用 MV_CC_CloseDevice() 关闭设备。
7.调用 MV_CC_DestroyHandle() 销毁句柄并释放资源。
二、主动取流流程
三、回调取流流程
读图像指针转Mat格式
srcImage = cv::Mat(200, 200, CV_8UC3, pData);
取像测试1
malloc
1.获取海康相机图像的内存首地址 2.利用图像首地址转为OpenCV的
MVS相机: 1、包含目录: E:\software\MVS\Development\Includes 2、库目录: E:\software\MVS\Development\Libraries\win64 3、附加依赖项: E:\software\MVS\Development\Libraries\win64\MvCameraControl.lib
#include <stdio.h> #include <process.h> #include <conio.h> #include "windows.h" #include "MvCameraControl.h" HWND g_hwnd = NULL; bool g_bExit = false; unsigned int g_nPayloadSize = 0; void WaitForKeyPress(void) { while (!_kbhit()) { Sleep(10); } _getch(); } bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo) { if (NULL == pstMVDevInfo) { printf("The Pointer of pstMVDevInfo is NULL!\n"); return false; } if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE) { int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24); int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16); int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8); int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff); printf("CurrentIp: %d.%d.%d.%d\n", nIp1, nIp2, nIp3, nIp4); printf("UserDefinedName: %s\n\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName); } else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE) { printf("UserDefinedName: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName); printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chSerialNumber); printf("Device Number: %d\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.nDeviceNumber); } else { printf("Not support.\n"); } return true; } LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_DESTROY: PostQuitMessage(0); g_hwnd = NULL; break; } return DefWindowProc(hWnd, msg, wParam, lParam); } static unsigned int __stdcall CreateRenderWindow(void* pUser) { HINSTANCE hInstance = ::GetModuleHandle(NULL); WNDCLASSEX wc; wc.cbSize = sizeof(wc); wc.style = CS_HREDRAW | CS_VREDRAW; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = ::LoadIcon(NULL, IDI_APPLICATION); wc.hIconSm = ::LoadIcon(NULL, IDI_APPLICATION); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.hCursor = ::LoadCursor(NULL, IDC_ARROW); wc.lpfnWndProc = WndProc; wc.lpszMenuName = NULL; wc.lpszClassName = "RenderWindow"; if (!RegisterClassEx(&wc)) { return 0; } DWORD style = WS_OVERLAPPEDWINDOW; DWORD styleEx = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; RECT rect = { 0, 0, 640, 480 }; AdjustWindowRectEx(&rect, style, false, styleEx); HWND hWnd = CreateWindowEx(styleEx, "RenderWindow", "Display", style, 0, 0, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, hInstance, NULL); if (hWnd == NULL) { return 0; } ::UpdateWindow(hWnd); ::ShowWindow(hWnd, SW_SHOW); g_hwnd = hWnd; MSG msg = { 0 }; while (msg.message != WM_QUIT) { if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return 0; } static unsigned int __stdcall WorkThread(void* pUser) { int nRet = MV_OK; MV_FRAME_OUT_INFO_EX stImageInfo = { 0 }; MV_DISPLAY_FRAME_INFO stDisplayInfo = { 0 }; unsigned char* pData = (unsigned char*)malloc(sizeof(unsigned char) * (g_nPayloadSize)); if (pData == NULL) { return 0; } unsigned int nDataSize = g_nPayloadSize; while (1) { nRet = MV_CC_GetOneFrameTimeout(pUser, pData, nDataSize, &stImageInfo, 1000); if (nRet == MV_OK) { printf("Get One Frame: Width[%d], Height[%d], nFrameNum[%d]\n", stImageInfo.nWidth, stImageInfo.nHeight, stImageInfo.nFrameNum); if (g_hwnd) { stDisplayInfo.hWnd = g_hwnd; stDisplayInfo.pData = pData; stDisplayInfo.nDataLen = stImageInfo.nFrameLen; stDisplayInfo.nWidth = stImageInfo.nWidth; stDisplayInfo.nHeight = stImageInfo.nHeight; stDisplayInfo.enPixelType = stImageInfo.enPixelType; MV_CC_DisplayOneFrame(pUser, &stDisplayInfo); } } else { printf("No data[0x%x]\n", nRet); } if (g_bExit) { break; } } free(pData); return 0; } int main() { int nRet = MV_OK; void* handle = NULL; do { MV_CC_DEVICE_INFO_LIST stDeviceList = { 0 }; nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, &stDeviceList); if (MV_OK != nRet) { printf("Enum Devices fail! nRet [0x%x]\n", nRet); break; } if (stDeviceList.nDeviceNum > 0) { for (unsigned int i = 0; i < stDeviceList.nDeviceNum; i++) { printf("[device %d]:\n", i); MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i]; if (NULL == pDeviceInfo) { break; } PrintDeviceInfo(pDeviceInfo); } } else { printf("Find No Devices!\n"); break; } printf("Please Input camera index:"); unsigned int nIndex = 0; scanf_s("%d", &nIndex); if (nIndex >= stDeviceList.nDeviceNum) { printf("Input error!\n"); break; } nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]); if (MV_OK != nRet) { printf("Create Handle fail! nRet [0x%x]\n", nRet); break; } nRet = MV_CC_OpenDevice(handle); if (MV_OK != nRet) { printf("Open Device fail! nRet [0x%x]\n", nRet); break; } if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE) { int nPacketSize = MV_CC_GetOptimalPacketSize(handle); if (nPacketSize > 0) { nRet = MV_CC_SetIntValue(handle, "GevSCPSPacketSize", nPacketSize); if (nRet != MV_OK) { printf("Warning: Set Packet Size fail nRet [0x%x]!", nRet); } } else { printf("Warning: Get Packet Size fail nRet [0x%x]!", nPacketSize); } } nRet = MV_CC_SetEnumValue(handle, "TriggerMode", 0); if (MV_OK != nRet) { printf("Set Trigger Mode fail! nRet [0x%x]\n", nRet); break; } MVCC_INTVALUE stParam = { 0 }; nRet = MV_CC_GetIntValue(handle, "PayloadSize", &stParam); if (MV_OK != nRet) { printf("Get PayloadSize fail! nRet [0x%x]\n", nRet); break; } g_nPayloadSize = stParam.nCurValue; unsigned int nThreadID = 0; void* hCreateWindow = (void*)_beginthreadex(NULL, 0, CreateRenderWindow, handle, 0, &nThreadID); if (NULL == hCreateWindow) { break; } nRet = MV_CC_StartGrabbing(handle); if (MV_OK != nRet) { printf("Start Grabbing fail! nRet [0x%x]\n", nRet); break; } nThreadID = 0; void* hThreadHandle = (void*)_beginthreadex(NULL, 0, WorkThread, handle, 0, &nThreadID); if (NULL == hThreadHandle) { break; } printf("Press a key to stop grabbing.\n"); WaitForKeyPress(); g_bExit = true; WaitForSingleObject(hThreadHandle, INFINITE); CloseHandle(hThreadHandle); nRet = MV_CC_StopGrabbing(handle); if (MV_OK != nRet) { printf("Stop Grabbing fail! nRet [0x%x]\n", nRet); break; } nRet = MV_CC_CloseDevice(handle); if (MV_OK != nRet) { printf("ClosDevice fail! nRet [0x%x]\n", nRet); break; } nRet = MV_CC_DestroyHandle(handle); if (MV_OK != nRet) { printf("Destroy Handle fail! nRet [0x%x]\n", nRet); break; } } while (0); if (nRet != MV_OK) { if (handle != NULL) { MV_CC_DestroyHandle(handle); handle = NULL; } } printf("Press a key to exit.\n"); WaitForKeyPress(); return 0; }
// pch.h: 这是预编译标头文件。 // 下方列出的文件仅编译一次,提高了将来生成的生成性能。 // 这还将影响 IntelliSense 性能,包括代码完成和许多代码浏览功能。 // 但是,如果此处列出的文件中的任何一个在生成之间有更新,它们全部都将被重新编译。 // 请勿在此处添加要频繁更新的文件,这将使得性能优势无效。 #ifndef PCH_H #define PCH_H #include <stdio.h> #include <process.h> #include <conio.h> #include "MvCameraControl.h" #include "yolo.h" #pragma comment(lib, "opencv_world331.lib") // 添加要在此处预编译的标头 #include "framework.h" extern "C" __declspec(dllexport) void __stdcall loadModel(); extern "C" __declspec(dllexport) int __stdcall initDevice(); extern "C" __declspec(dllexport) int __stdcall detect(char* out_path); extern "C" __declspec(dllexport) int __stdcall getStr(char* resultStr); extern "C" __declspec(dllexport) int __stdcall closeDevice(); int RGB2BGR(unsigned char* pRgbData, unsigned int nWidth, unsigned int nHeight); bool Convert2Mat(MV_FRAME_OUT_INFO_EX* pstImageInfo, unsigned char* pData, cv::Mat& srcImage); bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo); int initDevice(); int detect(char* out_path); int getStr(char* resultStr); int closeDevice(); #endif //PCH_H
// pch.cpp: 与预编译标头对应的源文件 #include "pch.h" // 当使用预编译的头时,需要使用此源文件,编译才能成功。 void* g_handle = NULL; unsigned int g_nPayloadSize = 0; YoloV3 yolo; 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) { RGB2BGR(pData, pstImageInfo->nWidth, pstImageInfo->nHeight); srcImage = cv::Mat(pstImageInfo->nHeight, pstImageInfo->nWidth, CV_8UC3, pData); } else { printf("unsupported pixel format\n"); return false; } if (NULL == srcImage.data) { return false; } return true; } bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo) { if (NULL == pstMVDevInfo) { printf("The Pointer of pstMVDevInfo is NULL!\n"); return false; } if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE) { int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24); int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16); int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8); int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff); printf("Camera Ip: %d.%d.%d.%d\n", nIp1, nIp2, nIp3, nIp4); printf("User Defined Name: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName); printf("Serial Number: %s\n\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chSerialNumber); } else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE) { printf("User Defined Name: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName); printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chSerialNumber); printf("Device Number: %d\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.nDeviceNumber); } else { printf("Not support.\n"); } return true; } void loadModel() { yolo = YoloV3(26, ".\\ocr_26.pt", ".\\ocr_classes.txt", 416, 416, 0.2f, 0.1f, 30); //return 0; } // 找到相机并成功打开返回0,否则返回-1 int initDevice() { int nRet = MV_OK; do { // 1.1、枚举设备 MV_CC_DEVICE_INFO_LIST stDeviceList = { 0 }; nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, &stDeviceList); if (MV_OK != nRet) { printf("Enum Devices fail! nRet [0x%x]\n", nRet); break; } // 1.2、打印设备信息 if (stDeviceList.nDeviceNum > 0) { for (unsigned int i = 0; i < stDeviceList.nDeviceNum; i++) { printf("Device %d Info:\n", i); MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i]; if (NULL == pDeviceInfo) { break; } PrintDeviceInfo(pDeviceInfo); } } else { printf("Find No Devices!\n"); return -1; } unsigned int nIndex = 0; // 2、创建句柄 nRet = MV_CC_CreateHandle(&g_handle, stDeviceList.pDeviceInfo[nIndex]); if (MV_OK != nRet) { printf("Create Handle fail! nRet [0x%x]\n", nRet); break; } // 3、打开设备 nRet = MV_CC_OpenDevice(g_handle); if (MV_OK != nRet) { printf("Open Device fail! nRet [0x%x]\n", nRet); break; } if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE) { int nPacketSize = MV_CC_GetOptimalPacketSize(g_handle); // 获取最佳的packet size,该接口目前只支持GigE相机 if (nPacketSize > 0) { nRet = MV_CC_SetIntValue(g_handle, "GevSCPSPacketSize", nPacketSize); // 设置Integer型属性值 if (nRet != MV_OK) { printf("Warning: Set Packet Size fail nRet [0x%x]!", nRet); } } else { printf("Warning: Get Packet Size fail nRet [0x%x]!", nPacketSize); } } // 相机触发打开或关闭 nRet = MV_CC_SetEnumValue(g_handle, "TriggerMode", 0); // MV_TRIGGER_MODE_ON MV_TRIGGER_MODE_OFF if (MV_OK != nRet) { printf("Set Trigger Mode fail! nRet [0x%x]\n", nRet); break; } // 获取Int属性值 MVCC_INTVALUE stParam = { 0 }; nRet = MV_CC_GetIntValue(g_handle, "PayloadSize", &stParam); if (MV_OK != nRet) { printf("Get PayloadSize fail! nRet [0x%x]\n", nRet); break; } g_nPayloadSize = stParam.nCurValue; nRet = MV_CC_StartGrabbing(g_handle); if (MV_OK != nRet) { printf("Start Grabbing fail! nRet [0x%x]\n", nRet); break; } } while (0); if (nRet != MV_OK) { if (g_handle != NULL) { MV_CC_DestroyHandle(g_handle); g_handle = NULL; } return -1; } return 0; } // 成功检测返回字符串长度,其余情况(分配内存错误、取帧错误)返回-1 int detect(char* out_path) { unsigned char* pData = (unsigned char*)malloc(sizeof(unsigned char) * (g_nPayloadSize)); if (pData == nullptr) { return -1; } unsigned int nDataSize = g_nPayloadSize; MV_FRAME_OUT_INFO_EX stImageInfo = { 0 }; int nRet = MV_OK; nRet = MV_CC_GetOneFrameTimeout(g_handle, pData, nDataSize, &stImageInfo, 1000); if (nRet == MV_OK) { printf("Get One Frame: Width[%d], Height[%d], nFrameNum[%d]\n", stImageInfo.nWidth, stImageInfo.nHeight, stImageInfo.nFrameNum); cv::Mat srcImage; bool convertSuccess = Convert2Mat(&stImageInfo, pData, srcImage); assert(convertSuccess); cv::imwrite("srcImage.bmp", srcImage); double angle = -90.0; cv::Point2f center(srcImage.cols / 2, srcImage.rows / 2); cv::Mat rot = cv::getRotationMatrix2D(center, angle, 1); cv::Rect bbox = cv::RotatedRect(center, srcImage.size(), angle).boundingRect(); rot.at<double>(0, 2) += bbox.width / 2.0 - center.x; rot.at<double>(1, 2) += bbox.height / 2.0 - center.y; cv::Mat srcImage_rotated; cv::warpAffine(srcImage, srcImage_rotated, rot, bbox.size()); //cv::imwrite("srcImage_rotated.bmp", srcImage_rotated); //cv::namedWindow("source", cv::WINDOW_AUTOSIZE); //cv::imshow("source", srcImage); //cv::waitKey(0); //cv::destroyWindow("source"); //printf("开始检测...\n"); cv::Mat resultImage = yolo.YoloDetectImage(srcImage_rotated); //printf("完成检测...\n"); //cv::namedWindow("result", cv::WINDOW_AUTOSIZE); //cv::imshow("result", resultImage); //cv::waitKey(0); //cv::destroyWindow("result"); std::string img_path = "resultImage.bmp"; char out[1024]; strcpy(out, img_path.c_str()); memcpy(out_path, out, sizeof(out)); cv::imwrite("resultImage.bmp", resultImage); free(pData); pData = nullptr; return strlen(img_path.c_str()); } else { printf("No data[0x%x]\n", nRet); free(pData); pData = nullptr; return -1; } } // 未找到字符串返回-1,否则返回字符串长度 int getStr(char* resultStr) { std::cout << "开始获取字符数据..." << std::endl; std::vector<std::string> result = yolo.YoloGetResult(); if (!result.size()) { return -1; } std::string result_str = result[0]; for (int i = 1; i < result.size(); i++) { result_str += result[i]; } char temp[1024]; strcpy(temp, result_str.c_str()); memcpy(resultStr, temp, sizeof(temp)); return strlen(result_str.c_str()); } // 成功关闭相机返回0,关闭失败返回-1 int closeDevice() { int nRet = MV_OK; do { nRet = MV_CC_StopGrabbing(g_handle); if (MV_OK != nRet) { printf("Stop Grabbing fail! nRet [0x%x]\n", nRet); break; } nRet = MV_CC_CloseDevice(g_handle); if (MV_OK != nRet) { printf("Close Device fail! nRet [0x%x]\n", nRet); break; } nRet = MV_CC_DestroyHandle(g_handle); if (MV_OK != nRet) { printf("Destroy Handle fail! nRet [0x%x]\n", nRet); break; } } while (0); if (nRet != MV_OK) { if (g_handle != NULL) { MV_CC_DestroyHandle(g_handle); g_handle = NULL; } return -1; } return 0; }