Ejemplo n.º 1
0
      public Bitmap ToBitmap(Bitmap.Config config)
      {
         System.Drawing.Size size = Size;

         if (config == Bitmap.Config.Argb8888)
         {
            Bitmap result = Bitmap.CreateBitmap(size.Width, size.Height, Bitmap.Config.Argb8888);

            using (BitmapArgb8888Image bi = new BitmapArgb8888Image(result))
            using (Image<Rgba, Byte> tmp = ToImage<Rgba, Byte>())
            {
               tmp.Copy(bi, null);
            }
            return result;
         }
         else if (config == Bitmap.Config.Rgb565)
         {
            Bitmap result = Bitmap.CreateBitmap(size.Width, size.Height, Bitmap.Config.Rgb565);

            using (BitmapRgb565Image bi = new BitmapRgb565Image(result))
            using (Image<Bgr, Byte> tmp = ToImage<Bgr, Byte>())
               bi.ConvertFrom(tmp);
            return result;
         }
         else
         {
            throw new NotImplementedException("Only Bitmap config of Argb888 or Rgb565 is supported.");
         }
      }
Ejemplo n.º 2
0
      protected override void OnDraw(Android.Graphics.Canvas canvas)
      {
         base.OnDraw(canvas);

         lock (this)
         {
            Image<Bgr, byte> image = _bgrBuffers.GetBuffer(0);

            if (image != null && !_imageSize.IsEmpty && canvas != null)
            {
               Stopwatch w = Stopwatch.StartNew();

               if ((_bmpImage != null) && (!_imageSize.Equals(_bmpImage.Size)))
               {
                  _bmpImage.Dispose();
                  _bmpImage = null;
                  _bmp.Dispose();
                  _bmp = null;
               }

               if (_bmpImage == null)
               {
                  _bmp = Android.Graphics.Bitmap.CreateBitmap(_imageSize.Width, _imageSize.Height, Android.Graphics.Bitmap.Config.Rgb565);
                  _bmpImage = new BitmapRgb565Image(_bmp);
               }

               _bmpImage.ConvertFrom(image);

               canvas.DrawBitmap(_bmp, 0, 0, _paint);

               w.Stop();

               _watch.Stop();
               
#if DEBUG
               canvas.DrawText(String.Format("{0:F2} FPS; {1}x{2}; Render Time: {3} ms",
                  1.0 / _watch.ElapsedMilliseconds * 1000,
                  _imageSize.Width,
                  _imageSize.Height,
                  w.ElapsedMilliseconds), 20, 20, _paint);
#endif
               _watch.Reset();
               _watch.Start();
            }
         }
      }
Ejemplo n.º 3
0
      /// <summary>
      /// Convert the Mat to Bitmap
      /// </summary>
      /// <param name="config">The bitmap config type. If null, Argb8888 will be used</param>
      /// <returns>The Bitmap</returns>
      public Bitmap ToBitmap(Bitmap.Config config = null)
      {
         System.Drawing.Size size = Size;

         if (config == null || config == Bitmap.Config.Argb8888)
         {
            Bitmap result = Bitmap.CreateBitmap(size.Width, size.Height, Bitmap.Config.Argb8888);
            int channels = NumberOfChannels;
            using (BitmapArgb8888Image bi = new BitmapArgb8888Image(result))
            {
               if (channels == 1)
               {
                  CvInvoke.CvtColor(this, bi.Mat, ColorConversion.Gray2Rgba);  
               } else if (channels == 3)
               {
                  CvInvoke.CvtColor(this, bi, ColorConversion.Bgr2Rgba);
               } else if (channels == 4)
               {
                  CvInvoke.CvtColor(this, bi, ColorConversion.Bgra2Rgba);
               }
               else
               {
                  using (Image<Rgba, Byte> tmp = ToImage<Rgba, Byte>())
                  {
                     tmp.Copy(bi, null);
                  }

               }
            }
            return result;
         }
         else if (config == Bitmap.Config.Rgb565)
         {
            Bitmap result = Bitmap.CreateBitmap(size.Width, size.Height, Bitmap.Config.Rgb565);

            using (BitmapRgb565Image bi = new BitmapRgb565Image(result))
            using (Image<Bgr, Byte> tmp = ToImage<Bgr, Byte>())
               bi.ConvertFrom(tmp);
            return result;
         }
         else
         {
            throw new NotImplementedException("Only Bitmap config of Argb888 or Rgb565 is supported.");
         }
      }
Ejemplo n.º 4
0
        public void ImagePreview(Object sender, ProcessedCameraPreview.ImagePreviewEventArgs e)
        {
            Image <Gray, Byte> yuv420sp = e.Yuv420sp;
            Image <Bgr, Byte>  image    = e.Result;

            #region get the thumbnail size buffer
            Size s            = image.Size;
            int  maxDimension = 96;
            int  width        = maxDimension;
            int  height       = maxDimension;
            if (s.Width > s.Height)
            {
                height = maxDimension * s.Height / s.Width;
            }
            else
            {
                width = maxDimension * s.Width / s.Height;
            }
            s.Width  = width;
            s.Height = height;
            Mat thumbnail = _bgrBuffers.GetBuffer(s, 1);
            #endregion

            if (_imageFilter == null)
            {
                CvInvoke.CvtColor(yuv420sp, image, Emgu.CV.CvEnum.ColorConversion.Yuv420Sp2Bgr);
                CvInvoke.Resize(image, thumbnail, s, 0, 0, Emgu.CV.CvEnum.Inter.Nearest);
            }
            else
            {
                Mat bgr = _bgrBuffers.GetBuffer(image.Size, 0);
                CvInvoke.CvtColor(yuv420sp, bgr, Emgu.CV.CvEnum.ColorConversion.Yuv420Sp2Bgr);
                lock (typeof(ImageFilter))
                    _imageFilter.ProcessData(bgr, image.Mat);
                CvInvoke.Resize(bgr, thumbnail, s, 0, 0, Emgu.CV.CvEnum.Inter.Nearest);
            }

            {
                Android.Graphics.Bitmap thumbnailBmp = _previewBitmapBuffers.GetBuffer(s, 0);
                using (BitmapRgb565Image img565 = new BitmapRgb565Image(thumbnailBmp))
                    img565.ConvertFrom(thumbnail);
                _previewButtons[0].SetImageBitmap(thumbnailBmp);

                int startBufferIndex = 2;
                for (int i = 1; i < _previewFilters.Length; i++)
                {
                    Mat buffer = _bgrBuffers.GetBuffer(s, startBufferIndex + i - 1);
                    _previewFilters[i].ProcessData(thumbnail, buffer);

                    Android.Graphics.Bitmap bmp = _previewBitmapBuffers.GetBuffer(s, i);
                    using (BitmapRgb565Image tmp = new BitmapRgb565Image(bmp))
                    {
                        tmp.ConvertFrom(buffer);
                    }
                    _previewButtons[i].SetImageBitmap(bmp);
                }
            } /*
               * catch (Exception excpt)
               * {
               * while (excpt.InnerException != null)
               *  excpt = excpt.InnerException;
               * Android.Util.Log.Debug("Emgu.CV", String.Format("Failed to generate preview: {0}", excpt.Message));
               * }*/
        }
Ejemplo n.º 5
0
      public void ImagePreview(Object sender, ProcessedCameraPreview.ImagePreviewEventArgs e)
      {
         Image<Gray, Byte> yuv420sp = e.Yuv420sp;
         Image<Bgr, Byte> image = e.Result;

#region get the thumbnail size buffer
         Size s = image.Size;
         int maxDimension = 96;
         int width = maxDimension;
         int height = maxDimension;
         if (s.Width > s.Height)
         {
            height = maxDimension * s.Height / s.Width;
         }
         else
         {
            width = maxDimension * s.Width / s.Height;
         }
         s.Width = width;
         s.Height = height;
         Mat thumbnail = _bgrBuffers.GetBuffer(s, 1);
#endregion

         if (_imageFilter == null)
         {
            CvInvoke.CvtColor(yuv420sp, image, Emgu.CV.CvEnum.ColorConversion.Yuv420Sp2Bgr);
            CvInvoke.Resize(image, thumbnail, s, 0, 0, Emgu.CV.CvEnum.Inter.Nearest);
         }
         else
         {
            Mat bgr = _bgrBuffers.GetBuffer(image.Size, 0);
            CvInvoke.CvtColor(yuv420sp, bgr, Emgu.CV.CvEnum.ColorConversion.Yuv420Sp2Bgr);
            lock (typeof(ImageFilter))
               _imageFilter.ProcessData(bgr, image.Mat);
            CvInvoke.Resize(bgr, thumbnail, s, 0, 0, Emgu.CV.CvEnum.Inter.Nearest);
         }

         {
            Android.Graphics.Bitmap thumbnailBmp = _previewBitmapBuffers.GetBuffer(s, 0);
            using (BitmapRgb565Image img565 = new BitmapRgb565Image(thumbnailBmp))
               img565.ConvertFrom(thumbnail);
            _previewButtons[0].SetImageBitmap(thumbnailBmp);
  
            int startBufferIndex = 2;
            for (int i = 1; i < _previewFilters.Length; i++)
            {
               Mat buffer = _bgrBuffers.GetBuffer(s, startBufferIndex + i - 1);
               _previewFilters[i].ProcessData(thumbnail, buffer);

               Android.Graphics.Bitmap bmp = _previewBitmapBuffers.GetBuffer(s, i);
               using (BitmapRgb565Image tmp = new BitmapRgb565Image(bmp))
               {
                  tmp.ConvertFrom(buffer);
               }
               _previewButtons[i].SetImageBitmap(bmp);
            }
         } /*
         catch (Exception excpt)
         {
            while (excpt.InnerException != null)
               excpt = excpt.InnerException;
            Android.Util.Log.Debug("Emgu.CV", String.Format("Failed to generate preview: {0}", excpt.Message));
         }*/

      }