设置ROI的目的
生产线中对相机读图速度的要求特别快 大范围的图对采集有很重的负担
所以要把图片的范围缩小
下面上代码
要注意的是要先关闭相机 再开相机 再设置roi 再实时采集
关闭相机
public void Close()
{
// ch:关闭设备 | en:Close Device
int nRet;
nRet = m_pMyCamera.MV_CC_CloseDevice_NET();
if (MyCamera.MV_OK != nRet)
{
return;
}
nRet = m_pMyCamera.MV_CC_DestroyDevice_NET();
if (MyCamera.MV_OK != nRet)
{
return;
}
// ch:控件操作 | en:Control Operation
//SetCtrlWhenClose();
// ch:取流标志位清零 | en:Reset flow flag bit
// m_bGrabbing = false;
}//关闭相机```
打开相机
public void dakaixiangji()
{
if (m_pDeviceList.nDeviceNum == 0 || comboBox1.SelectedIndex == -1)
{
cc.ShowErrorMsg("No device, please select", 0);
return;
}
int nRet = -1;
// ch:获取选择的设备信息 | en:Get selected device information
MyCamera.MV_CC_DEVICE_INFO device =
(MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_pDeviceList.pDeviceInfo[comboBox1.SelectedIndex],
typeof(MyCamera.MV_CC_DEVICE_INFO));
// ch:打开设备 | en:Open device
if (null == m_pMyCamera)
{
m_pMyCamera = new MyCamera();
if (null == m_pMyCamera)
{
return;
}
}
nRet = m_pMyCamera.MV_CC_CreateDevice_NET(ref device);
if (MyCamera.MV_OK != nRet)
{
return;
}
nRet = m_pMyCamera.MV_CC_OpenDevice_NET();
if (MyCamera.MV_OK != nRet)
{
m_pMyCamera.MV_CC_DestroyDevice_NET();
cc.ShowErrorMsg("Device open fail!", nRet);
return;
}
// ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
{
int nPacketSize = m_pMyCamera.MV_CC_GetOptimalPacketSize_NET();
if (nPacketSize > 0)
{
nRet = m_pMyCamera.MV_CC_SetIntValue_NET("GevSCPSPacketSize", (uint)nPacketSize);
if (nRet != MyCamera.MV_OK)
{
Console.WriteLine("Warning: Set Packet Size failed {0:x8}", nRet);
}
}
else
{
Console.WriteLine("Warning: Get Packet Size failed {0:x8}", nPacketSize);
}
}
}
设置ROI
public void SetRoi()
{
int nRet = MyCamera.MV_OK;
ImageSize imgMaxSize = /*new ImageSize();*/ GetMaxSize();
//imgMaxSize.width = 1024;
//imgMaxSize.height = 960;
uint nWidthMax = imgMaxSize.width;
uint nHeightMax = imgMaxSize.height;
nRet = m_pMyCamera.MV_CC_SetIntValue_NET("OffsetX", 0);
if (MyCamera.MV_OK != nRet)
{
cc.ShowErrorMsg("Set OffsetX Fail!", nRet);
}
nRet = m_pMyCamera.MV_CC_SetIntValue_NET("OffsetY", 0);
if (MyCamera.MV_OK != nRet)
{
cc.ShowErrorMsg("Set OffsetY Fail!", nRet);
}
imgWidth = (uint)(_roi.width * nWidthMax);
uint nVal = (imgWidth / 16) * 16;
nRet = m_pMyCamera.MV_CC_SetIntValue_NET("Width", nVal);
if (MyCamera.MV_OK != nRet)
{
cc.ShowErrorMsg("Set Width Fail!", nRet);
}
imgHeight = (uint)(_roi.height * nHeightMax);
nVal = (imgHeight / 16) * 16;
nRet = m_pMyCamera.MV_CC_SetIntValue_NET("Height", nVal);
if (MyCamera.MV_OK != nRet)
{
cc.ShowErrorMsg("Set Height Fail!", nRet);
}
offX = (uint)(_roi.offX * nWidthMax);
nVal = (offX / 16) * 16;
nRet = m_pMyCamera.MV_CC_SetIntValue_NET("OffsetX", nVal);
if (MyCamera.MV_OK != nRet)
{
cc.ShowErrorMsg("Set OffsetX Fail!", nRet);
}
offY = (uint)(_roi.offY * nHeightMax);
nVal = (offY / 16) * 16;
nRet = m_pMyCamera.MV_CC_SetIntValue_NET("OffsetY", nVal);
if (MyCamera.MV_OK != nRet)
{
cc.ShowErrorMsg("Set OffsetY Fail!", nRet);
}
//return true;/*GetPayloadSize();*/
}
开始采集
public void caiji()
{
int nRet;
// ch:开始采集 | en:Start Grabbing
nRet = m_pMyCamera.MV_CC_StartGrabbing_NET();
if (MyCamera.MV_OK != nRet)
{
cc.ShowErrorMsg("Trigger Fail!", nRet);
return;
}
// ch:控件操作 | en:Control Operation
//SetCtrlWhenStartGrab();
// ch:标志位置位true | en:Set position bit true
//m_bGrabbing = true;
// ch:显示 | en:Display
//Form1 fm = new Form1();
//if (fm.b)
//{
// SetRoi();
//}
//PointSetRoi point = new Point((int)offX,(int)offY);
//pictureBox1.Location = point;
//pictureBox1.Width = (int)imgWidth;
//pictureBox1.Height = (int)imgHeight;
nRet = m_pMyCamera.MV_CC_Display_NET(pictureBox1.Handle);
// nRet = m_pMyCamera.MV_CC_Display_NET(pictureBox1.Handle);
if (MyCamera.MV_OK != nRet)
{
cc.ShowErrorMsg("Display Fail!", nRet);
}
}