整了两天终于搞定了,还未优化,投影仪500帧/s投编码图案,触发相机拍照,连续不断拍,实现动态三维重建。代码还未整理,先记录一下
#include
#include
#include
#include "MvCameraControl.h"
#include
#include "bmpLoader.h"
int nRet = MV_OK;
void* handle = NULL;
int expose1 = 1000;
std::string st2 = "D:\\siat_reconstruction\\projectreconstruction\\1_picture";
unsigned int g_nPayloadSize = 0;
unsigned char* m_pBufForSaveImage; // ch:用于保存图像的缓存 | en:Buffer to save image
unsigned int m_nBufSizeForSaveImage;
// ch:等待按键输入 | en:Wait for key press
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);
// ch:打印当前相机ip和用户自定义名字 | en:print current ip and user defined name
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\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
}
else
{
printf("Not support.\n");
}
return true;
}
int i = 0;
void __stdcall ImageCallBackEx(unsigned char * pData, MV_FRAME_OUT_INFO_EX* pFrameInfo, void* pUser)
{
if (pFrameInfo)
{
printf("Get One Frame: Width[%d], Height[%d], nFrameNum[%d]\n",
pFrameInfo->nWidth, pFrameInfo->nHeight, pFrameInfo->nFrameNum);
}
// ch:设置对应的相机参数 | en:Set camera parameter
if (NULL == m_pBufForSaveImage)
{
// ch:BMP图片大小:width * height * 3 + 2048(预留BMP头大小)
// en:// BMP image size: width * height * 3 + 2048 (Reserved BMP header size)
m_nBufSizeForSaveImage = pFrameInfo->nWidth * pFrameInfo->nHeight * 3 + 2048;
m_pBufForSaveImage = (unsigned char*)malloc(m_nBufSizeForSaveImage);
}
//MV_SAVE_IMAGE_PARAM_EX stParam = { 0 };
//MV_SAVE_IAMGE_TYPE m_nSaveImageType; // ch:保存图像格式 | en:Save Image Type
//m_nSaveImageType = MV_Image_Bmp;
//stParam.enImageType = m_nSaveImageType; // ch:需要保存的图像类型 | en:Image format to save
//stParam.enPixelType = pFrameInfo->enPixelType; // ch:相机对应的像素格式 | en:Camera pixel type
//stParam.nWidth = pFrameInfo->nWidth; // ch:相机对应的宽 | en:Width
//stParam.nHeight = pFrameInfo->nHeight; // ch:相机对应的高 | en:Height
//stParam.nDataLen = pFrameInfo->nFrameLen;
//stParam.pData = pData;
//stParam.pImageBuffer = m_pBufForSaveImage;
//stParam.nBufferSize = m_nBufSizeForSaveImage; // ch:存储节点的大小 | en:Buffer node size
//stParam.nJpgQuality = 80; // ch:jpg编码,仅在保存Jpg图像时有效。保存BMP时SDK内忽略该参数
//MV_CC_SaveImageEx2(handle, &stParam); // en:jpg encoding, only valid when saving as Jpg. SDK ignore this parameter when saving as BMP
//
// //将转换之后图片数据保存成文件
//char chImageName[100] = { 0 };
//std::string s = std::to_string(i);
//std::string ch1 = st2 + s +"\\%d.bmp";
//char p[100];
//int i = 0;
//for (i = 0; i < ch1.length(); i++)
// p[i] = ch1[i];
//p[i] = '\0';
// sprintf_s(chImageName, 100, p, pFrameInfo->nFrameNum);
// //std::string chImageName = "C:\\Users\\Administrator\\Desktop\\mianjiegouguangchongjian\\kls\\deviceone_picture\\"+std::to_string(stImageInfo.nFrameNum)+".bmp";
//FILE* fp = fopen(chImageName, "wb+");
//fwrite(m_pBufForSaveImage, 1, stParam.nImageLen, fp);
//fclose(fp);
//free(m_pBufForSaveImage);
//i++;
////...其他图像数据处理
// //for (int i = 0; i < 100; i++)
//// //保存图片
////{
//// printf("%d \n", pData[i]);
////}
////SaveBMPFile("d:\\kls.bmp", pData, pFrameInfo->nWidth, pFrameInfo->nHeight);
// ch:设置对应的相机参数 | en:Set camera parameter
MV_SAVE_IAMGE_TYPE m_nSaveImageType; // ch:保存图像格式 | en:Save Image Type
m_nSaveImageType = MV_Image_Bmp;
MV_SAVE_IMAGE_PARAM_EX stParam = { 0 };
stParam.enImageType = m_nSaveImageType; // ch:需要保存的图像类型 | en:Image format to save
stParam.enPixelType = pFrameInfo->enPixelType; // ch:相机对应的像素格式 | en:Camera pixel type
stParam.nWidth = pFrameInfo->nWidth; // ch:相机对应的宽 | en:Width
stParam.nHeight = pFrameInfo->nHeight; // ch:相机对应的高 | en:Height
stParam.nDataLen = pFrameInfo->nFrameLen;
stParam.pData = pData;
stParam.pImageBuffer = m_pBufForSaveImage;
stParam.nBufferSize = m_nBufSizeForSaveImage; // ch:存储节点的大小 | en:Buffer node size
stParam.nJpgQuality = 80; // ch:jpg编码,仅在保存Jpg图像时有效。保存BMP时SDK内忽略该参数
nRet = MV_CC_SaveImageEx2(handle, &stParam);
if (0 == nRet)
{
printf(("write image failed, maybe you have no privilege"), 0);
}
char chImageName[64] = { 0 };
sprintf_s(chImageName, 64, "D:\\siat_reconstruction\\1_picture\\%01d.bmp", pFrameInfo->nFrameNum);
FILE* fp = fopen(chImageName, "wb");
if (NULL == fp)
{
printf(("write image failed, maybe you have no privilege"), 0);
}
fwrite(m_pBufForSaveImage, 1, stParam.nImageLen, fp);
fclose(fp);
}
int main()
{
do
{
// ch:枚举设备 | Enum device
MV_CC_DEVICE_INFO_LIST stDeviceList;
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
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 Intput camera index:");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
if (nIndex >= stDeviceList.nDeviceNum)
{
printf("Intput error!\n");
break;
}
// ch:选择设备并创建句柄 | Select device and create handle
nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
if (MV_OK != nRet)
{
printf("Create Handle fail! nRet [0x%x]\n", nRet);
break;
}
// ch:打开设备 | Open device
nRet = MV_CC_OpenDevice(handle);
if (MV_OK != nRet)
{
printf("Open Device fail! nRet [0x%x]\n", nRet);
break;
}
// ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
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);
}
}
// ch:设置触发模式为off
nRet = MV_CC_SetEnumValue(handle, "TriggerMode", MV_TRIGGER_MODE_OFF);
if (MV_OK != nRet)
{
printf("Set Trigger Mode fail! nRet [0x%x]\n", nRet);
break;
}
//设置相机曝光时间
nRet = MV_CC_SetEnumValue(handle, "ExposureAuto", MV_EXPOSURE_AUTO_MODE_OFF);
if (MV_OK != nRet)
{
printf("Set ExposureAuto fail! nRet [0x%x]\n", nRet);
break;
}
//Sleep(10);//如果不加这个sleep,曝光时间的设置会失败。
nRet = MV_CC_SetFloatValue(handle, "ExposureTime", expose1);
if (MV_OK != nRet)
{
printf("Set ExposureTime fail! nRet [0x%x]\n", nRet);
break;
}
// ch:设置触发模式为on
nRet = MV_CC_SetEnumValue(handle, "TriggerMode", MV_TRIGGER_MODE_ON);
if (MV_OK != nRet)
{
printf("Set Trigger Mode fail! nRet [0x%x]\n", nRet);
break;
}
//设置Enum型参数
unsigned int enMode = MV_TRIGGER_SOURCE_LINE0; //设置触发源为软触发
// ch:设置为硬触发模式 | en:Set trigger mode as hardware trigger
nRet = MV_CC_SetEnumValue(handle, "TriggerSource", enMode);
if (MV_OK != nRet)
{
printf("Set Trigger Source fail! nRet [0x%x]\n", nRet);
break;
}
// ch:注册抓图回调 | en:Register image callback
nRet = MV_CC_RegisterImageCallBackEx(handle, ImageCallBackEx, handle);
if (MV_OK != nRet)
{
printf("Register Image CallBack fail! nRet [0x%x]\n", nRet);
break;
}
// ch:获取数据包大小 | en:Get payload size
MVCC_INTVALUE stParama1;
memset(&stParama1, 0, sizeof(MVCC_INTVALUE));
nRet = MV_CC_GetIntValue(handle, "PayloadSize", &stParama1);
if (MV_OK != nRet)
{
printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
break;
}
g_nPayloadSize = stParama1.nCurValue;
// ch:开始取流 | en:Start grab image
nRet = MV_CC_StartGrabbing(handle);
if (MV_OK != nRet)
{
printf("Start Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
printf("Press a key to stop grabbing.\n");
WaitForKeyPress();
// ch:停止取流 | en:Stop grab image
nRet = MV_CC_StopGrabbing(handle);
if (MV_OK != nRet)
{
printf("Stop Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
// ch:关闭设备 | en:Close device
nRet = MV_CC_CloseDevice(handle);
if (MV_OK != nRet)
{
printf("Close Device fail! nRet [0x%x]\n", nRet);
break;
}
// ch:销毁句柄 | en:Destroy handle
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;
}
直接存入硬盘有点慢,有些图片来不及触发,还是写入内存再保存到硬盘里靠谱
#include
#include
#include
#include "MvCameraControl.h"
#include
#include "bmpLoader.h"
//#include
//#include
#include
int nRet = MV_OK;
void* handle = NULL;
int expose1 = 1000;
unsigned int g_nPayloadSize = 0;
unsigned int m_nBufSizeForSaveImage;
unsigned int save_imagesze= NULL;
// ch:等待按键输入 | en:Wait for key press
std::vector image;
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);
// ch:打印当前相机ip和用户自定义名字 | en:print current ip and user defined name
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\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
}
else
{
printf("Not support.\n");
}
return true;
}
int i = 0;
void __stdcall ImageCallBackEx(unsigned char * pData, MV_FRAME_OUT_INFO_EX* pFrameInfo, void* pUser)
{
unsigned char* m_pBufForSaveImage= NULL; // ch:用于保存图像的缓存 | en:Buffer to save image
if (pFrameInfo)
{
printf("Get One Frame: Width[%d], Height[%d], nFrameNum[%d]\n",
pFrameInfo->nWidth, pFrameInfo->nHeight, pFrameInfo->nFrameNum);
}
// ch:设置对应的相机参数 | en:Set camera parameter
if (NULL == m_pBufForSaveImage)
{
// ch:BMP图片大小:width * height * 3 + 2048(预留BMP头大小)
// en:// BMP image size: width * height * 3 + 2048 (Reserved BMP header size)
m_nBufSizeForSaveImage = pFrameInfo->nWidth * pFrameInfo->nHeight * 3 + 2048;
m_pBufForSaveImage = (unsigned char*)malloc(m_nBufSizeForSaveImage);
}
//MV_SAVE_IMAGE_PARAM_EX stParam = { 0 };
//MV_SAVE_IAMGE_TYPE m_nSaveImageType; // ch:保存图像格式 | en:Save Image Type
//m_nSaveImageType = MV_Image_Bmp;
//stParam.enImageType = m_nSaveImageType; // ch:需要保存的图像类型 | en:Image format to save
//stParam.enPixelType = pFrameInfo->enPixelType; // ch:相机对应的像素格式 | en:Camera pixel type
//stParam.nWidth = pFrameInfo->nWidth; // ch:相机对应的宽 | en:Width
//stParam.nHeight = pFrameInfo->nHeight; // ch:相机对应的高 | en:Height
//stParam.nDataLen = pFrameInfo->nFrameLen;
//stParam.pData = pData;
//stParam.pImageBuffer = m_pBufForSaveImage;
//stParam.nBufferSize = m_nBufSizeForSaveImage; // ch:存储节点的大小 | en:Buffer node size
//stParam.nJpgQuality = 80; // ch:jpg编码,仅在保存Jpg图像时有效。保存BMP时SDK内忽略该参数
//MV_CC_SaveImageEx2(handle, &stParam); // en:jpg encoding, only valid when saving as Jpg. SDK ignore this parameter when saving as BMP
//
// //将转换之后图片数据保存成文件
//char chImageName[100] = { 0 };
//std::string s = std::to_string(i);
//std::string ch1 = st2 + s +"\\%d.bmp";
//char p[100];
//int i = 0;
//for (i = 0; i < ch1.length(); i++)
// p[i] = ch1[i];
//p[i] = '\0';
// sprintf_s(chImageName, 100, p, pFrameInfo->nFrameNum);
// //std::string chImageName = "C:\\Users\\Administrator\\Desktop\\mianjiegouguangchongjian\\kls\\deviceone_picture\\"+std::to_string(stImageInfo.nFrameNum)+".bmp";
//FILE* fp = fopen(chImageName, "wb+");
//fwrite(m_pBufForSaveImage, 1, stParam.nImageLen, fp);
//fclose(fp);
//free(m_pBufForSaveImage);
//i++;
////...其他图像数据处理
// //for (int i = 0; i < 100; i++)
//// //保存图片
////{
//// printf("%d \n", pData[i]);
////}
////SaveBMPFile("d:\\kls.bmp", pData, pFrameInfo->nWidth, pFrameInfo->nHeight);
// ch:设置对应的相机参数 | en:Set camera parameter
MV_SAVE_IAMGE_TYPE m_nSaveImageType; // ch:保存图像格式 | en:Save Image Type
m_nSaveImageType = MV_Image_Bmp;
MV_SAVE_IMAGE_PARAM_EX stParam = { 0 };
stParam.enImageType = m_nSaveImageType; // ch:需要保存的图像类型 | en:Image format to save
stParam.enPixelType = pFrameInfo->enPixelType; // ch:相机对应的像素格式 | en:Camera pixel type
stParam.nWidth = pFrameInfo->nWidth; // ch:相机对应的宽 | en:Width
stParam.nHeight = pFrameInfo->nHeight; // ch:相机对应的高 | en:Height
stParam.nDataLen = pFrameInfo->nFrameLen;
stParam.pData = pData;
stParam.pImageBuffer = m_pBufForSaveImage;
stParam.nBufferSize = m_nBufSizeForSaveImage; // ch:存储节点的大小 | en:Buffer node size
stParam.nJpgQuality = 80; // ch:jpg编码,仅在保存Jpg图像时有效。保存BMP时SDK内忽略该参数
nRet = MV_CC_SaveImageEx2(handle, &stParam);
image.push_back(m_pBufForSaveImage);
if (NULL == save_imagesze)
{
save_imagesze = stParam.nImageLen;
}
}
int main()
{
do
{
// ch:枚举设备 | Enum device
MV_CC_DEVICE_INFO_LIST stDeviceList;
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
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 Intput camera index:");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
if (nIndex >= stDeviceList.nDeviceNum)
{
printf("Intput error!\n");
break;
}
// ch:选择设备并创建句柄 | Select device and create handle
nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
if (MV_OK != nRet)
{
printf("Create Handle fail! nRet [0x%x]\n", nRet);
break;
}
// ch:打开设备 | Open device
nRet = MV_CC_OpenDevice(handle);
if (MV_OK != nRet)
{
printf("Open Device fail! nRet [0x%x]\n", nRet);
break;
}
// ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
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);
}
}
// ch:设置触发模式为off
nRet = MV_CC_SetEnumValue(handle, "TriggerMode", MV_TRIGGER_MODE_OFF);
if (MV_OK != nRet)
{
printf("Set Trigger Mode fail! nRet [0x%x]\n", nRet);
break;
}
//设置相机曝光时间
nRet = MV_CC_SetEnumValue(handle, "ExposureAuto", MV_EXPOSURE_AUTO_MODE_OFF);
if (MV_OK != nRet)
{
printf("Set ExposureAuto fail! nRet [0x%x]\n", nRet);
break;
}
//Sleep(10);//如果不加这个sleep,曝光时间的设置会失败。
nRet = MV_CC_SetFloatValue(handle, "ExposureTime", expose1);
if (MV_OK != nRet)
{
printf("Set ExposureTime fail! nRet [0x%x]\n", nRet);
break;
}
// ch:设置触发模式为on
nRet = MV_CC_SetEnumValue(handle, "TriggerMode", MV_TRIGGER_MODE_ON);
if (MV_OK != nRet)
{
printf("Set Trigger Mode fail! nRet [0x%x]\n", nRet);
break;
}
//设置Enum型参数
unsigned int enMode = MV_TRIGGER_SOURCE_LINE0; //设置触发源为软触发
// ch:设置为硬触发模式 | en:Set trigger mode as hardware trigger
nRet = MV_CC_SetEnumValue(handle, "TriggerSource", enMode);
if (MV_OK != nRet)
{
printf("Set Trigger Source fail! nRet [0x%x]\n", nRet);
break;
}
// ch:注册抓图回调 | en:Register image callback
nRet = MV_CC_RegisterImageCallBackEx(handle, ImageCallBackEx, handle);
if (MV_OK != nRet)
{
printf("Register Image CallBack fail! nRet [0x%x]\n", nRet);
break;
}
// ch:获取数据包大小 | en:Get payload size
MVCC_INTVALUE stParama1;
memset(&stParama1, 0, sizeof(MVCC_INTVALUE));
nRet = MV_CC_GetIntValue(handle, "PayloadSize", &stParama1);
if (MV_OK != nRet)
{
printf("Get PayloadSize fail! nRet [0x%x]\n", nRet);
break;
}
g_nPayloadSize = stParama1.nCurValue;
// ch:开始取流 | en:Start grab image
nRet = MV_CC_StartGrabbing(handle);
if (MV_OK != nRet)
{
printf("Start Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
printf("Press a key to stop grabbing.\n");
WaitForKeyPress();
// ch:停止取流 | en:Stop grab image
nRet = MV_CC_StopGrabbing(handle);
if (MV_OK != nRet)
{
printf("Stop Grabbing fail! nRet [0x%x]\n", nRet);
break;
}
printf("开始保存图片\n");
//保存图片
for (int i = 0; i