C# OpenCV学习笔记三之图像捕捉及其灰度转换方法

来源:互联网 发布:微观数据的弊端 编辑:程序博客网 时间:2024/06/06 18:06
透过摄像头捕捉图像,需要注意的是这里的captureImageBox是Emgu.CV.UI.ImageBox,而不是.NET的PictureBox
        private Capture capture;        private bool captureInProcess;        private void captureButton_Click(object sender, EventArgs e)        {            if (capture != null)//摄像头不为空               {                if (captureInProcess)                   {                    Application.Idle -= new EventHandler(processframe);                    captureButton.Text = "Stop!";                   }                   else                  {                    Application.Idle += new EventHandler(processframe);                    captureButton.Text = "Start!";                   }                captureInProcess = !captureInProcess;               }               else//摄像头为空则通过Capture()方法调用               {                   try                  {                       capture = new Capture();                    if (captureInProcess)                    {                        Application.Idle -= new EventHandler(processframe);                        captureButton.Text = "Stop!";                    }                    else                    {                        Application.Idle += new EventHandler(processframe);                        captureButton.Text = "Start!";                    }                    captureInProcess = !captureInProcess;                 }                   catch (NullReferenceException excpt)                   {                       MessageBox.Show(excpt.Message);                   }               }           }        private void processframe(object sender, EventArgs arg)        {            Image<Bgr, Byte> frame = capture.QueryFrame();            captureImageBox.Image = frame;                    }

图像灰度转换方法

private void btnGray_Click(object sender, EventArgs e)        {            Rectangle cr = CvInvoke.cvGetImageROI(captureImageBox.Image.Ptr);            int width = cr.Width;            int height = cr.Height;            IntPtr GrayImg1 = CvInvoke.cvCreateImage(cr.Size, Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 1);            CvInvoke.cvCvtColor(captureImageBox.Image.Ptr, GrayImg1, Emgu.CV.CvEnum.COLOR_CONVERSION.CV_BGR2GRAY);            CvInvoke.cvSaveImage("c:\\gray.bmp", GrayImg1);                      }



	
				
		
原创粉丝点击