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

透过摄像头捕捉图像,需要注意的是这里的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);
  
            
        }



 

你可能感兴趣的:(c,.net,object,C#,null,byte)