Basler多相机 千兆网采集图像转化成OpenCV图像代码(软件触发)
try
{
CBaslerGigEInstantCamera camera[c_maxCamerasToUse];
for (int i = 0;i < c_maxCamerasToUse;i++)
{
CDeviceInfo info;
info.SetDeviceClass( BaslerGigEDeviceClass );
char ch[12] = {0};
for (int t = 0; t < 8;t++)
{
ch[t] = pInfo->_strCam[i].GetAt(t);
}
String_t NO(ch);
info.SetSerialNumber(NO); //相机序列号
camera[i].Attach( CTlFactory::GetInstance().CreateDevice( info));
camera[i].RegisterConfiguration( new CConfigurationEventPrinter, RegistrationMode_Append, Cleanup_Delete); // Camera use.
camera[i].RegisterImageEventHandler( new CImageEventPrinter, RegistrationMode_Append, Cleanup_Delete); // Image grabbing.
camera[i].Open();// Open the camera.
// Set transmission type to "multicast"...
// In this case, the IP Address and the IP port must also be set.
camera[i].GetStreamGrabberParams().TransmissionType = TransmissionType_Multicast;
// camera.GetStreamGrabberParams().DestinationAddr = "239.0.0.1"; // These are default values.
// camera.GetStreamGrabberParams().DestinationPort = 49152;
// Maximize the image area of interest (Image AOI).
if (IsWritable(camera[i].OffsetX))
{
camera[i].OffsetX.SetValue(camera[i].OffsetX.GetMin());
}
if (IsWritable(camera[i].OffsetY))
{
camera[i].OffsetY.SetValue(camera[i].OffsetY.GetMin());
}
camera[i].Width.SetValue(camera[i].Width.GetMax());
camera[i].Height.SetValue(camera[i].Height.GetMax());
// Set the pixel data format.
camera[i].PixelFormat.SetValue(PixelFormat_Mono8);
camera[i].ExposureMode.SetValue(ExposureMode_Timed);
camera[i].ExposureTimeAbs.SetValue(pInfo->_Exp);
}
//判断信号是检测位置还是检测转角
CGrabResultPtr ptrGrabResult;
pInfo->_nOpt = 0;
while(p->m_bQuit == false)
{
//0---没有检测指令
//1---标准位置检测
//2---位置在线检测
//3---标准转角检测
//4---角度在线检测
if (pInfo->_nOpt == 0)
{
Sleep(100);
continue;
}
if (pInfo->_nOpt == 1 || pInfo->_nOpt == 2) //位置相机抓取一幅图像
II = 1;
else
II = 0; //角度相机抓取一幅图像
camera[II].StartGrabbing();
CPylonImage image;
CImageFormatConverter fc;
fc.OutputPixelFormat = PixelType_Mono8;
camera[II].RetrieveResult( 2000, ptrGrabResult, TimeoutHandling_ThrowException);
if (!ptrGrabResult->GrabSucceeded())
continue;
camera[II].StopGrabbing();
fc.Convert(image,ptrGrabResult);
Mat cv_img = cv::Mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(), CV_8UC1,(uint8_t*)image.GetBuffer());
pInfo->_pRaw = &(IplImage(cv_img));
CCommonTool::DrawImageToHDC(p,pInfo->_pRaw,IDC_STATIC_IMG,true);
if (pInfo->_pShow)
cvReleaseImage(&pInfo->_pShow);
pInfo->_pShow = cvCloneImage(pInfo->_pRaw); //拷贝原始图片 ,//根据检查指令进行相应的操作
bool bSuccess = false;
if (pInfo->_nOpt == 1 || pInfo->_nOpt == 2) //获得产品的标准位置或者检测位置
{
CString strFile;
strFile.Format("D:\\info\\0---%d.jpg",GetTickCount());
cvSaveImage(strFile,pInfo->_pShow);
bSuccess = p->GetPillarPos();
}
else
{
bSuccess = p->GetDotAngel(); //采集产品的标准转角
}
if (bSuccess == false)
{
p->SetDlgItemText(IDC_STATIC_ANG,"产品异常");
}
pInfo->_nOpt = 0;
}
for (int i = 0;i < c_maxCamerasToUse;i++)
{
camera[i].Close();
}
pInfo->_pRaw = NULL;
pInfo->_QuitEVT.SetEvent();
}
catch (GenICam::GenericException &e)
{
// Error handling
//TRACE( "\nAn exception occurred: %s",e.GetDescription());
}
评论