海康相機顯示ROI
阿新 • • 發佈:2021-01-16
技術標籤:c#
1.畫ROI
#region ROI設定 //畫矩形框 Point start; Point end; bool blnDraw; //在MouseMove事件中判斷是否繪製矩形框 private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { start = e.Location; blnDraw = true; } } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (blnDraw) { if (e.Button != MouseButtons.Left) return; end = e.Location; pictureBox1.Invalidate(); } } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { PictureBox pic = sender as PictureBox; if (e.Button == MouseButtons.Left) { end = e.Location; blnDraw = false; } if (e.Button == MouseButtons.Right) { } } reletiveRoi _roi= new reletiveRoi(); public reletiveRoi roi { get { return _roi; } } private void pictureBox1_Paint(object sender, PaintEventArgs e) { PictureBox pic = sender as PictureBox; Pen pen = new Pen(Color.Red, 3); pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot; //繪製線的格式 if (blnDraw) { //此處是為了在繪製時可以由上向下繪製,也可以由下向上 e.Graphics.DrawRectangle(pen, Math.Min(start.X, end.X), Math.Min(start.Y, end.Y), Math.Abs(start.X - end.X), Math.Abs(start.Y - end.Y)); _roi.width = (Math.Abs(start.X - end.X) / (double)pic.Width); _roi.height = (Math.Abs(start.Y - end.Y) / (double)pic.Height); _roi.offsetX = (Math.Min(start.X, end.X) / (double)pic.Width); _roi.offsetY = (Math.Min(start.Y, end.Y) / (double)pic.Height); } pen.Dispose(); } #endregion
2.寫入相機
得到相機最大尺寸
private ImageSize GetMaxSize()
{
ImageSize imgMaxSize = new ImageSize();
int nRet = MyCamera.MV_OK;
//_isDrawROI = false;
//ch:獲取包大小||en: Get Payload Size
MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
//ch:獲取高||en: Get Height
nRet = m_pMyCamera.MV_CC_GetIntValue_NET(“HeightMax”, ref stParam);
{
ShowErrorMsg(“Get Height Fail!”, nRet);
}
imgMaxSize.height = stParam.nCurValue;
//ch:獲取寬||en:Get Width nRet = m_pMyCamera.MV_CC_GetIntValue_NET("WidthMax", ref stParam); if (MyCamera.MV_OK != nRet) { ShowErrorMsg("Get Width Fail!", nRet); } imgMaxSize.width = stParam.nCurValue; return imgMaxSize; } 按比列寫入 public void SetRoi() { int nRet = MyCamera.MV_OK; ImageSize imgMaxSize = GetMaxSize(); uint nWidthMax = imgMaxSize.width; uint nHeightMax = imgMaxSize.height; nRet = m_pMyCamera.MV_CC_SetIntValue_NET("OffsetX", 0); if (MyCamera.MV_OK != nRet) { ShowErrorMsg("Set OffsetX Fail!", nRet); } nRet = m_pMyCamera.MV_CC_SetIntValue_NET("OffsetY", 0); if (MyCamera.MV_OK != nRet) { ShowErrorMsg("Set OffsetY Fail!", nRet); } imgWidth = (uint)(_roi.width * nWidthMax); uint nVal = (imgWidth / 32) * 32; nRet = m_pMyCamera.MV_CC_SetIntValue_NET("Width", nVal); if (MyCamera.MV_OK != nRet) { 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) { ShowErrorMsg("Set Height Fail!", nRet); } offsetX = (uint)(_roi.offsetX * nWidthMax); nVal = (offsetX / 16) * 16; nRet = m_pMyCamera.MV_CC_SetIntValue_NET("OffsetX", nVal); if (MyCamera.MV_OK != nRet) { ShowErrorMsg("Set OffsetX Fail!", nRet); } offsetY = (uint)(_roi.offsetY * nHeightMax); nVal = (offsetY / 16) * 16; nRet = m_pMyCamera.MV_CC_SetIntValue_NET("OffsetY", nVal); if (MyCamera.MV_OK != nRet) { ShowErrorMsg("Set OffsetY Fail!", nRet); } }
3.呼叫
相機先關閉、再開啟
private void btnGetRoi_Click(object sender, EventArgs e)
{
ReadRoi();
ucCamera2.closeCamera();
ucCamera2.openCamera();
ucCamera2.roi = this.roi;
ucCamera2.SetRoi();
ucCamera2.StartGrab(pictureBox1);
}