Beispiel #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();
            }
        }
Beispiel #2
0
        private void ShowSelectedRegionProperties()
        {
            var idx = _viewModel.SelectedRegionIndex;

            if (idx == null)
            {
                imgPreview.Source = null;
                txtCoords.Text    = "";
                return;
            }

            var region = _viewModel.ImageRegions[idx.Value];

            var yoloCoords = new YoloCoords
            {
                X      = region.X,
                Y      = region.Y,
                Height = region.Height,
                Width  = region.Width
            };

            ShowYolo(yoloCoords);
        }