Example #1
0
        private void ShowYolo(YoloCoords yoloCoords)
        {
            MouseHelper.SetWaitCursor();
            try
            {
                var img = _viewModel.CurrentBitmap;
                if (img == null)
                {
                    return;
                }

                if (yoloCoords.Height <= double.Epsilon || yoloCoords.Width <= double.Epsilon)
                {
                    imgPreview.Source = null;
                    txtCoords.Text    = "";
                    return;
                }

                // render how translating back from yolo coords will look
                var rwidth  = (int)(yoloCoords.Width * img.Width);
                var rheight = (int)(yoloCoords.Height * img.Height);
                var imgx    = (int)(yoloCoords.X * img.Width - rwidth / 2.0);
                var imgy    = (int)(yoloCoords.Y * img.Height - rheight / 2.0);

                if (imgy + rheight > img.Height)
                {
                    rheight = img.Height - imgy;
                }
                if (imgx + rwidth > img.Width)
                {
                    rwidth = img.Width - imgx;
                }

                var rect    = new Rectangle(imgx, imgy, rwidth, rheight);
                var newImg  = img.Clone(rect, img.PixelFormat);
                var hbitmap = newImg.GetHbitmap();

                var bmpsource = Imaging.CreateBitmapSourceFromHBitmap(
                    hbitmap,
                    IntPtr.Zero,
                    Int32Rect.Empty,
                    BitmapSizeOptions.FromWidthAndHeight(rwidth, rheight)
                    );

                BitmapCloner.DeleteObject(hbitmap);
                newImg.Dispose();

                txtCoords.Text = $"x: {imgx} y: {imgy} w: {rwidth}px h: {rheight}px";

                imgPreview.Source = bmpsource;
            }
            finally
            {
                MouseHelper.ResetCursor();
            }
        }
        public void CloneTest1()
        {
            BitmapCloner target = new BitmapCloner();
            var          width  = 2;
            var          height = 2;

            using (var bitmap = new Bitmap(width, height))
            {
                long distance = long.MaxValue;
                var  actual   = target.Clone(bitmap, distance, 1);
                Assert.IsTrue(actual.Vectors.Any());
            }
        }
        public void CloneTest2()
        {
            BitmapCloner target = new BitmapCloner();
            var          width  = 2;
            var          height = 2;

            using (var bitmap = new Bitmap(width, height))
            {
                long expectedDistance = 0L;
                var  actual           = target.Clone(bitmap, expectedDistance, 1);
                var  actualDistance   = VectorDrawingGenetics.Instance.CalculateFitness(actual, bitmap);
                Assert.IsTrue(actualDistance <= expectedDistance);
            }
        }
        public void CloneTest3()
        {
            BitmapCloner target = new BitmapCloner();
            var          width  = 2;
            var          height = 2;

            using (var bitmap = new Bitmap(width, height))
            {
                using (var graphics = Graphics.FromImage(bitmap))
                {
                    graphics.DrawLine(Pens.Orange, new Point(0, 0), new Point(1, 1));
                }
                long expectedDistance = 512;
                var  actual           = target.Clone(bitmap, expectedDistance, 1);
                var  actualDistance   = VectorDrawingGenetics.Instance.CalculateFitness(actual, bitmap);
                Assert.IsTrue(actualDistance <= expectedDistance);
            }
        }
        public void CloneTest5()
        {
            BitmapCloner target   = new BitmapCloner();
            var          fileName = Path.Combine(testContextInstance.TestDeploymentDir, "smile.png");

            using (var bitmap = new Bitmap(fileName))
            {
                long expectedDistance = 1024 * 32;
                var  stopwatch        = Stopwatch.StartNew();
                var  actual           = target.Clone(bitmap, expectedDistance, 2);
                stopwatch.Stop();
                var actualDistance = VectorDrawingGenetics.Instance.CalculateFitness(actual, bitmap);
                Assert.IsTrue(actualDistance <= expectedDistance);
                using (var cloneBitmap = new Bitmap(bitmap.Width, bitmap.Height))
                {
                    using (var graphics = Graphics.FromImage(cloneBitmap))
                    {
                        GraphicsExtensions.RasterizeVectorDrawing(graphics, actual);
                    }
                    cloneBitmap.Save("smile.bmp", ImageFormat.Bmp);
                }
            }
        }
 public void BitmapClonerConstructorTest()
 {
     BitmapCloner target = new BitmapCloner();
 }