打印
[上位机系统管理]

opencvsharp 物体边框检测

[复制链接]
2426|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
haylang|  楼主 | 2019-2-12 10:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
private void CaptureCameraCallback()
            {
               // capture = new VideoCapture();
               // capture.Open(0);//
                Mat frame = new Mat();
                Mat imageout = new Mat();
                Mat[] planes;
                //Vec3b color = new Vec3b();//新建vec3b的对象,
                var capture = new VideoCapture(0);
                //capture.Set(CaptureProperty.Settings, 1);
                //capture.Set(CaptureProperty.Brightness, 150);
                capture.Set(CaptureProperty.Exposure, -1000);//曝光 50  
                capture.Set(CaptureProperty.IOS_DeviceExposure, -1000);
   
                while(true)
                {
                   capture.Read(frame);         
                   //Mat source = new Mat("image.jpg", ImreadModes.Grayscale);                  
                   Invalidate();pictureBox1.Invalidate();                  
                   // Cv2.Canny(frame,imageout, 50, 200);        //edge get      
                   // for (int i = 240; i < 400; i++)
                   // { frame.Set(i, 320, 100); }
                   //空心圆
                   Cv2.Circle(frame,                 //目标图像
                           new OpenCvSharp.Point(320, 240),  //中心点坐标
                           5,                 //半径
                           Scalar.YellowGreen); //颜色         
                   //Cv2.CvtColor(frame, imageout, ColorConversionCodes.BGRA2GRAY);
                   Cv2.Split(frame, out planes);//通道分离
                   // cv2.split函数分离得到各个通道的灰度值(单通道图像)。cv2.merge函数是合并单通道成多通道(不能合并多个多通道图像)。
                   //byte gray = imageout.Get<byte>(320, 240);        //这里AT函数和GET一样只能获取值
                   //byte B = (byte)Math.Abs(frame.Get<Vec3b>(240, 320).Item0);//B读取原来的通道值 color.Item0
                   //byte G = (byte)Math.Abs(frame.Get<Vec3b>(240, 320).Item1);//G读取原来的通道值 color.Item1
                   //byte R = (byte)Math.Abs(frame.Get<Vec3b>(240, 320).Item2);//R读取原来的通道值 color.Item2   
                   byte B = (byte)Math.Abs(planes[0].Get<Vec3b>(240, 320).Item0);//B读取原来的通道值 color.Item0
                   byte G = (byte)Math.Abs(planes[1].Get<Vec3b>(240, 320).Item1);//G读取原来的通道值 color.Item1
                   byte R = (byte)Math.Abs(planes[2].Get<Vec3b>(240, 320).Item2);//R读取原来的通道值 color.Item2   
                   //byte colorvalue = (byte)Math.Abs(frame.Get<byte>(320, 240));//读取原来的通道值
                   Cv2.PutText(frame,
                             "B"+B.ToString() + " " + "G"+G.ToString() + " " + "R"+R.ToString(), //字符串
                            // gray.ToString(),
                             new OpenCvSharp.Point(0, 80),    //位置,注意这是字符串左下角的位置
                             HersheyFonts.HersheyDuplex,    //字体类型
                             2,                              //字体大小   
                             Scalar.Green);                  //颜色
                  // Cv2.Flip(frame, imageout, FlipMode.XY);//XY翻转图片source到dst中
                   // # 分别扩展B、G、R成为三通道。另外两个通道用上面的值为0的数组填充
                   // cv2.imshow("Blue", cv2.merge([B, zeros, zeros]))
                   // cv2.imshow("Green", cv2.merge([zeros, G, zeros]))
                   // cv2.imshow("Red", cv2.merge([zeros, zeros, R]))

                   //Cv2.BitwiseNot(planes[1], planes[1]); // Invert G plane               
                   image = BitmapConverter.ToBitmap(frame);//Mat conver to Bitmap                  
                  //image = BitmapConverter.ToBitmap(frame);
                  //Shown on pictureBox1
                  try {
                       pictureBox1.Image = image;
                      }
                  catch (Exception err)//一般情况下关闭串口不会出错,所以不需要加处理程序
                      {
                         MessageBox.Show(err.ToString());
                      }

                   image = null;        
                   Thread.Sleep(20);
                }  
 }

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

9

主题

32

帖子

0

粉丝