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

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




                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值