Beispiel #1
0
        private void OnLoadColorButtonClick(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "Images (*.png;*.jpeg;*.bmp;*.jpg)|*.png;*.jpeg;*.bmp;*.jpg";
            if (openFileDialog.ShowDialog() == true)
            {
                string filepath = openFileDialog.FileName;
                if (filepath != null && filepath.Length > 0)
                {
                    BitmapImage colBitmap  = new BitmapImage(new Uri(filepath));
                    double      scaleX     = Board.ActualWidth / colBitmap.PixelWidth;
                    double      scaleY     = Board.ActualHeight / colBitmap.PixelHeight;
                    var         colBitmap2 = new TransformedBitmap(colBitmap, new ScaleTransform(scaleX, scaleY));
                    colBitmapStride = ((colBitmap2.PixelWidth * colBitmap2.Format.BitsPerPixel + 7) / 8);
                    int colBitmapSize = colBitmapStride * colBitmap2.PixelHeight;
                    ColBitmap = new byte[colBitmapSize];
                    colBitmap2.CopyPixels(ColBitmap, colBitmapStride, 0);
                }
                else
                {
                    MessageBox.Show("No file chosen. Applying default color texture");
                    LoadColBitmap(Properties.Resources.bloom_blooming_bright_1131407);
                }
            }
            else
            {
                MessageBox.Show("No file chosen. Applying default color texture");
                LoadColBitmap(Properties.Resources.bloom_blooming_bright_1131407);
            }
        }
        /// <summary>
        /// Converts a value.
        /// </summary>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        /// <param name="value">The value produced by the binding source.</param><param name="targetType">The type of the binding target property.</param><param name="parameter">The converter parameter to use.</param><param name="culture">The culture to use in the converter.</param>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Image img = value as Image;

            if (img != null)
            {
                BitmapSource src = img.Source as BitmapSource;
                if (src != null)
                {
                    if (src.Format != PixelFormats.Rgb24)
                    {
                        // Если изображение не в формате RGB24, то переводим его в этот формат
                        src = new FormatConvertedBitmap(src, PixelFormats.Rgb24, null, 1);
                    }
                    // Ужимаем иконку до размера 1x1
                    TransformedBitmap bmp = new TransformedBitmap(src, new ScaleTransform(1.0 / src.Width, 1.0 / src.PixelHeight));
                    // Достаём цвет пиксела
                    byte[] pixels = new byte[3];
                    bmp.CopyPixels(pixels, 3, 0);
                    // Возвращаём цвет полученного пиксела
                    return(Color.FromArgb(255, pixels[0], pixels[1], pixels[2]));
                }
            }
            return(Colors.Yellow);
        }
Beispiel #3
0
        public static WriteableBitmap Scale(this WriteableBitmap image, double scale)
        {
            var s = new ScaleTransform(scale, scale);

            var source = new TransformedBitmap(image, s);

            // Calculate stride of source
            int stride = source.PixelWidth * (source.Format.BitsPerPixel / 8);

            // Create data array to hold source pixel data
            byte[] data = new byte[stride * source.PixelHeight];

            // Copy source image pixels to the data array
            source.CopyPixels(data, stride, 0);

            // Create WriteableBitmap to copy the pixel data to.
            WriteableBitmap target = new WriteableBitmap(source.PixelWidth
                                                         , source.PixelHeight, source.DpiX, source.DpiY
                                                         , source.Format, null);

            // Write the pixel data to the WriteableBitmap.
            target.WritePixels(new Int32Rect(0, 0
                                             , source.PixelWidth, source.PixelHeight)
                               , data, stride, 0);

            return(target);
        }
Beispiel #4
0
        private void OnLoadNButtonClick(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "Images (*.png;*.jpeg;*.bmp;*.jpg)|*.png;*.jpeg;*.bmp;*.jpg";
            if (openFileDialog.ShowDialog() == true)
            {
                string filepath = openFileDialog.FileName;
                if (filepath != null && filepath.Length > 0)
                {
                    BitmapImage nBitmap  = new BitmapImage(new Uri(filepath));
                    double      scaleX   = Board.ActualWidth / nBitmap.PixelWidth;
                    double      scaleY   = Board.ActualHeight / nBitmap.PixelHeight;
                    var         nBitmap2 = new TransformedBitmap(nBitmap, new ScaleTransform(scaleX, scaleY));
                    nBitmapStride = ((nBitmap2.PixelWidth * nBitmap2.Format.BitsPerPixel + 7) / 8);
                    int nBitmapSize = nBitmapStride * nBitmap2.PixelHeight;
                    NBitmap = new byte[nBitmapSize];
                    nBitmap2.CopyPixels(NBitmap, nBitmapStride, 0);
                }
                else
                {
                    MessageBox.Show("No file chosen. Applying default normal map");
                    LoadNBitmap(Properties.Resources.Carpet_01_NRM);
                }
            }
            else
            {
                MessageBox.Show("No file chosen. Applying default normal map");
                LoadNBitmap(Properties.Resources.Carpet_01_NRM);
            }
        }
        private static Image ScaleImage(BitmapSource bitmapSource, Size scale)
        {
            // Scale the image so that it will display similarly to the WPF Image.
            var newWidthRatio  = (scale.Width != -1 ? scale.Width / (double)bitmapSource.PixelWidth : 1.0);
            var newHeightRatio = (scale.Height != -1 ? ((scale.Width * bitmapSource.PixelHeight) / (double)bitmapSource.PixelWidth) / bitmapSource.PixelHeight : 1.0);

            BitmapSource transformedBitmapSource = new TransformedBitmap(bitmapSource, new ScaleTransform(newWidthRatio, newHeightRatio));

            var width  = transformedBitmapSource.PixelWidth;
            var height = transformedBitmapSource.PixelHeight;
            var stride = width * ((transformedBitmapSource.Format.BitsPerPixel + 7) / 8);

            var bits = new byte[height * stride];

            transformedBitmapSource.CopyPixels(bits, stride, 0);

            unsafe
            {
                fixed(byte *pBits = bits)
                {
                    var bitmap = new Bitmap(width, height, stride, PixelFormat.Format32bppPArgb, new IntPtr(pBits));

                    return(bitmap);
                }
            }
        }
Beispiel #6
0
        public WriteableBitmap ResizeWritableBitmap(WriteableBitmap wBitmap, int reqWidth, int reqHeight)
        {
            int    OriWidth  = (int)wBitmap.PixelWidth;
            int    OriHeight = (int)wBitmap.PixelHeight;
            double nXFactor  = (double)reqWidth / OriWidth;
            double nYFactor  = (double)reqHeight / OriHeight;
            var    s         = new ScaleTransform(nXFactor, nYFactor);

            var res = new TransformedBitmap(wBitmap, s);

            int stride = res.PixelWidth * (wBitmap.Format.BitsPerPixel / 8);

            // Create data array to hold source pixel data
            byte[] data = new byte[stride * res.PixelHeight];

            // Copy source image pixels to the data array
            res.CopyPixels(data, stride, 0);

            // Create WriteableBitmap to copy the pixel data to.
            WriteableBitmap target = new WriteableBitmap(res.PixelWidth
                                                         , res.PixelHeight, res.DpiX, res.DpiY
                                                         , res.Format, null);

            // Write the pixel data to the WriteableBitmap.
            target.WritePixels(new Int32Rect(0, 0
                                             , res.PixelWidth, res.PixelHeight)
                               , data, stride, 0);

            return(target);
        }
        /// <summary> Flips image horizontally </summary>
        /// <param name="inputImagePath">path to image that will be flipped</param>
        /// <param name="inputImagePath">path to file, where will be flipped image saved</param>
        public static BitmapSource FlipHorizontalImage(BitmapSource inputSource)
        {
            TransformedBitmap tb = new TransformedBitmap(inputSource, new ScaleTransform(-1, 1));
            //copy pixels to new bitmapSource because of strange .NET native memory leak
            int stride = tb.PixelWidth * tb.Format.BitsPerPixel / 8;

            byte[] bitmapArray = new byte[tb.PixelHeight * stride];
            tb.CopyPixels(bitmapArray, stride, 0);
            return(BitmapSource.Create(tb.PixelWidth, tb.PixelHeight, tb.DpiX, tb.DpiY, tb.Format,
                                       tb.Palette, bitmapArray, stride));
        }
Beispiel #8
0
        private void LoadNBitmap(System.Drawing.Bitmap bitmap)
        {
            BitmapImage nBitmap = ToBitmapImage(bitmap);
            double      scaleX  = Board.ActualWidth / nBitmap.PixelWidth;
            double      scaleY  = Board.ActualHeight / nBitmap.PixelHeight;
            var         transformedColBitmap = new TransformedBitmap(nBitmap, new ScaleTransform(scaleX, scaleY));

            nBitmapStride = ((transformedColBitmap.PixelWidth * transformedColBitmap.Format.BitsPerPixel + 7) / 8);
            int nBitmapSize = nBitmapStride * transformedColBitmap.PixelHeight;

            NBitmap = new byte[nBitmapSize];
            transformedColBitmap.CopyPixels(NBitmap, nBitmapStride, 0);
        }
Beispiel #9
0
        private void Bw_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker me = sender as BackgroundWorker;

            int[]             a    = new int[LedsPerStrip * StripCount];
            TransformedBitmap tbmp = null;

            System.Drawing.Color[,] grid = new System.Drawing.Color[StripCount, LedsPerStrip];

            while (!me.CancellationPending)
            {
                if (width == 0)
                {
                    viewport_border.Dispatcher.Invoke(() =>
                    {
                        PresentationSource source = PresentationSource.FromVisual(this);
                        width  = (int)(viewport.ActualWidth * source.CompositionTarget.TransformToDevice.M11);
                        height = (int)(viewport.ActualHeight * source.CompositionTarget.TransformToDevice.M22);
                        if (width != 0)
                        {
                            rtb = new RenderTargetBitmap(width, height, 96.0, 96.0, PixelFormats.Pbgra32);
                        }
                    });
                    Thread.Sleep(33);
                    continue;
                }

                viewport_border.Dispatcher.Invoke(() =>
                {
                    rtb.Render(viewport_border);
                    double scalex = LedsPerStrip * 1.0 / width;
                    double scaley = StripCount * 1.0 / height;
                    tbmp          = new TransformedBitmap(rtb, new ScaleTransform(scalex, scaley));
                    tbmp.CopyPixels(a, 4 * LedsPerStrip, 0);
                });

                for (int i = 0; i < StripCount; i++)
                {
                    for (int j = 0; j < LedsPerStrip; j++)
                    {
                        grid[i, j] = System.Drawing.Color.FromArgb(a[i * LedsPerStrip + j]);
                    }
                }

                _ledWall.SetWall(grid);

                Thread.Sleep(33);
            }
        }
Beispiel #10
0
        static float[] CreateInputTensorFromImage(String filename, int[] tensorDims, out float scaleFactor, out int offsetX, out int offsetY)
        {
            var bitmap       = new BitmapImage(new Uri(filename));
            var bitmapWidth  = bitmap.PixelWidth;
            var bitmapHeight = bitmap.PixelHeight;

            scaleFactor = 1.0f;
            if (bitmapWidth > bitmapHeight)
            {
                scaleFactor = (float)tensorDims[3] / bitmapWidth;
            }
            else
            {
                scaleFactor = (float)tensorDims[2] / bitmapHeight;
            }
            TransformedBitmap tb = new TransformedBitmap(bitmap, new System.Windows.Media.ScaleTransform(scaleFactor, scaleFactor));
            int newWidth         = tb.PixelWidth;
            int newHeight        = tb.PixelHeight;
            int channels         = tb.Format.BitsPerPixel / 8;
            int stride           = channels * newWidth;

            byte[] rawData        = new byte[stride * newHeight];
            byte[] rawLabelOutput = new byte[tensorDims[2] * tensorDims[3]];
            byte[] rawOutput      = new byte[stride * newHeight];
            tb.CopyPixels(rawData, stride, 0);
            int paddingX = tensorDims[3] - newWidth;
            int paddingY = tensorDims[2] - newHeight;

            float[] testData = new float[tensorDims[2] * tensorDims[3] * tensorDims[1]];
            for (int n = 0; n < tensorDims[2] * tensorDims[3] * tensorDims[1]; n++)
            {
                testData[n] = 0.0f;
            }
            offsetX = paddingX / 2;
            offsetY = paddingY / 2;
            // fill up tensor with image data
            for (int y = 0; y < newHeight; y++)
            {
                int y1 = y;
                for (int x = 0; x < newWidth; x++)
                {
                    testData[(x + offsetX) + (y + offsetY) * tensorDims[3] + tensorDims[2] * tensorDims[3] * 2] = rawData[(x + y1 * newWidth) * channels] / 255.0f;
                    testData[(x + offsetX) + (y + offsetY) * tensorDims[3] + tensorDims[2] * tensorDims[3]]     = rawData[(x + y1 * newWidth) * channels + 1] / 255.0f;
                    testData[(x + offsetX) + (y + offsetY) * tensorDims[3]] = rawData[(x + y1 * newWidth) * channels + 2] / 255.0f;
                }
            }
            return(testData);
        }
Beispiel #11
0
        private void SaveImages()
        {
            if (System.IO.File.Exists(Settings.BackgroundImagePath))
            {
                WriteableBitmap wb;
                BitmapImage     fileBitmap = new BitmapImage(new Uri(Settings.BackgroundImagePath, UriKind.Relative));
                wb = new WriteableBitmap(fileBitmap);
                //wb.DpiX = 300;

                int    heightOffset = Settings.TopMargin;
                double newWidth     = wb.PixelWidth - Settings.SideMargin * 2;

                foreach (var i in capturedImages)
                {
                    double scaleFactor = newWidth / i.PixelWidth;

                    var bitmap = new TransformedBitmap(i, new ScaleTransform(
                                                           scaleFactor,
                                                           scaleFactor));
                    var stride = (bitmap.PixelWidth * bitmap.Format.BitsPerPixel + 7) / 8;
                    var buffer = new byte[stride * bitmap.PixelHeight];
                    bitmap.CopyPixels(buffer, stride, 0);

                    wb.WritePixels(
                        new Int32Rect(Settings.SideMargin, heightOffset, bitmap.PixelWidth, bitmap.PixelHeight),
                        buffer, stride, 0);
                    heightOffset += bitmap.PixelHeight + Settings.ImageGap;
                }
                var s    = (wb.PixelWidth * wb.Format.BitsPerPixel + 7) / 8;
                var buff = new byte[s * wb.PixelHeight];
                wb.CopyPixels(buff, s, 0);

                var composite = new WriteableBitmap(wb.PixelWidth * 2, wb.PixelHeight, wb.DpiX, wb.DpiY, wb.Format, wb.Palette);
                composite.WritePixels(new Int32Rect(5, 0, wb.PixelWidth, wb.PixelHeight), buff, s, 0);
                composite.WritePixels(new Int32Rect(wb.PixelWidth, 0, wb.PixelWidth, wb.PixelHeight), buff, s, 0);
                PrintWindow pw = new PrintWindow(composite);
                pw.Show();
            }
            else
            {
                System.Windows.MessageBox.Show("No background image could be found. Nothing to attach images on to.", "PhotoboothFree");
            }
        }
Beispiel #12
0
        /// <summary>
        /// Method to scale the original image to 600 x 600.
        /// This should preserve the aspect ratio of the original image.
        /// </summary>
        /// <param name="fileName"></param>
        void ScaleImage(string fileName)
        {
            double fac1  = Convert.ToDouble(scaledWidth / (1.0 * originalWidth));
            double fac2  = Convert.ToDouble(scaledHeight / (1.0 * originalHeight));
            var    scale = new ScaleTransform(fac1, fac2);

            scaledImage = new TransformedBitmap(originalImage, scale);

            int stride = (scaledImage.PixelWidth * scaledImage.Format.BitsPerPixel + 7) / 8;

            if (scaledPixels != null)
            {
                scaledPixels = null;
            }
            scaledPixels = new byte[stride * scaledHeight];

            // Update the array scaledPixels from the scaled image
            scaledImage.CopyPixels(Int32Rect.Empty, scaledPixels, stride, 0);
        }
Beispiel #13
0
        public void Resize(Bitmap src, Bitmap dst, object options = null)
        {
            var source = Misc.AllocWriteableBitmap(src.Width, src.Height, src.Depth, src.Channel);

            Misc.CopyToWritableBitmap(source, src);
            var          scaleTransform = new ScaleTransform((double)dst.Width / src.Width, (double)dst.Height / src.Height);
            BitmapSource transformed    = new TransformedBitmap(source, scaleTransform);

            if (transformed.Format.BitsPerPixel > 64)
            {
                var converted = new FormatConvertedBitmap();
                converted.BeginInit();
                converted.Source            = transformed;
                converted.DestinationFormat = dst.Channel == 4 ? PixelFormats.Rgba64 : PixelFormats.Rgb48;
                converted.EndInit();

                transformed = converted;
            }
            transformed.CopyPixels(Int32Rect.Empty, dst.Scan0, dst.Stride * dst.Height, dst.Stride);
        }
Beispiel #14
0
        /// <summary>
        /// Создание таблиц преобразования интенсивностей изображений
        /// в новый динамический диапазон.
        /// Самый простой вариант - это линейное масштабирование.
        /// Динамическое масштабирование не исследовано, но может оказаться полезным.
        /// </summary>
        private void correctIntencityRange(BeamParams b)
        {
            filmLUT = new ushort[256];

            double r  = image.DpiX * R * 2.0 / 25.4;
            double x0 = b.O.X;
            double y0 = b.O.Y;

            Int32Rect rect = new Int32Rect();

            rect.X      = (int)(x0 - r);
            rect.Y      = (int)(y0 - r);
            rect.Width  = (int)(2 * r);
            rect.Height = (int)(2 * r);
            if (rect.X + rect.Width >= image.PixelWidth)
            {
                rect.X -= (int)(rect.X + rect.Width - image.PixelWidth + 1);
            }
            if (rect.Y + rect.Height >= image.PixelHeight)
            {
                rect.Y -= (int)(rect.Y + rect.Height - image.PixelHeight + 1);
            }
            if (rect.X < 0)
            {
                rect.Width += rect.X;
                if (rect.Width < 1)
                {
                    rect.Width = 1;
                }
                rect.X = 0;
            }
            if (rect.Y < 0)
            {
                rect.Height += rect.Y;
                if (rect.Height < 1)
                {
                    rect.Height = 1;
                }
                rect.Y = 0;
            }

            fx0    = (double)rect.X;
            fy0    = (double)rect.Y;
            fnx    = rect.Width;
            fny    = rect.Height;
            pixels = new byte[fnx * fny * 4];
            image.CopyPixels(rect, pixels, fnx * 4, 0);

            byte humax = 0;
            int  i, j;

            for (i = 0; i < rect.Width; i++)
            {
                for (j = 0; j < rect.Height; j++)
                {
                    byte bv = (byte)(255 - pixels[(i * rect.Height + j) * 4]);
                    pixels[(i * rect.Height + j) * 4] = bv;
                    if (bv > humax)
                    {
                        humax = bv;
                    }
                }
            }

            double scale = (double)(inorm - 1) / (double)humax;

            for (i = 0; i < 256; i++)
            {
                filmLUT[i] = (ushort)(scale * i);
            }

            // Debug
            //DumpPixels("C:/tmp/pixels.txt");
        }
        private void Correct(InferenceSession session, string path)
        {
            var rawImage = new BitmapImage();

            while (true)
            {
                try
                {
                    using (var stream = File.OpenRead(path))
                    {
                        rawImage.BeginInit();
                        rawImage.CacheOption  = BitmapCacheOption.OnLoad;
                        rawImage.StreamSource = stream;
                        rawImage.EndInit();
                    }
                }
                catch (System.IO.IOException e)
                {
                    var ret = WinForms.MessageBox.Show(
                        e.Message, "VRCPhotoRotationCorrector",
                        WinForms.MessageBoxButtons.RetryCancel);
                    if (ret == WinForms.DialogResult.Retry)
                    {
                        continue;
                    }
                    else
                    {
                        return;
                    }
                }
                break;
            }
            var scale       = INPUT_SIZE / (double)Math.Max(rawImage.PixelWidth, rawImage.PixelHeight);
            var scaledImage = new TransformedBitmap(rawImage, new ScaleTransform(scale, scale));
            var data        = new byte[INPUT_SIZE * INPUT_SIZE * 4];
            var stride      = INPUT_SIZE * 4;
            var offsetX     = (INPUT_SIZE - scaledImage.PixelWidth) / 2;
            var offsetY     = (INPUT_SIZE - scaledImage.PixelHeight) / 2;

            scaledImage.CopyPixels(data, stride, offsetX * 4 + offsetY * stride);
            var source = TransposeAndCast(data);
            var dims   = new int[] { 1, 3, INPUT_SIZE, INPUT_SIZE };
            var probs  = new float[4];

            for (int rot = 0; rot < 4; ++rot)
            {
                if (rot != 0)
                {
                    Rotate90(source);
                }
                var tensor = new DenseTensor <float>(source, dims);
                var inputs = new List <NamedOnnxValue>()
                {
                    NamedOnnxValue.CreateFromTensor <float>("input.1", tensor)
                };
                using (var results = session.Run(inputs))
                {
                    foreach (var r in results)
                    {
                        var values = r.AsTensor <float>().ToArray();
                        var e0     = (float)Math.Exp(values[0]);
                        var e1     = (float)Math.Exp(values[1]);
                        probs[rot] = e0 / (e0 + e1);
                    }
                }
            }
            var maxval = probs.Max();

            if (probs[0] != maxval)
            {
                var angle = 0.0;
                for (int a = 0; a < 4; ++a)
                {
                    if (probs[a] == maxval)
                    {
                        angle = a * 90.0;
                    }
                }
                var rotatedImage = new TransformedBitmap(rawImage, new RotateTransform(angle));
                var encoder      = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(rotatedImage));
                var tmp = path + ".tmp.png";
                using (var stream = File.OpenWrite(tmp))
                {
                    encoder.Save(stream);
                }
                File.Delete(path);
                File.Move(tmp, path);
            }
            // MessageBox.Show(path + ": " + probs[0].ToString() + ", " + probs[1].ToString() + ", " + probs[2].ToString() + ", " + probs[3].ToString());
        }
        // create the output image upon clicking the create button
        private void btnCreate_Click(object sender, RoutedEventArgs e)
        {
            // check that there is at least one enable tile to create the output image with
            bool allTilesDisabled = true;

            foreach (Tile tile in tilesList)
            {
                if (tile.enabled == true)
                {
                    allTilesDisabled = false;
                    break;
                }
            }
            if (allTilesDisabled)
            {
                // if every tile is disabled, show the relevant error message then return
                showErrorMessage();
                return;
            }

            // set the mouse to a wait cursor for the duration of the image processing
            Mouse.OverrideCursor = Cursors.Wait;

            try
            {
                // scale down the original image to the dimensions specified in the xScaling and yScaling text boxes
                var bmScaled = new TransformedBitmap(bmMain, new ScaleTransform(Convert.ToDouble(xScaling.Text) / bmMain.PixelWidth, Convert.ToDouble(yScaling.Text) / bmMain.PixelHeight));

                // calculate the stride (how many bytes in a single row of the image) of the resulting image
                int stride = (bmScaled.PixelWidth * bmScaled.Format.BitsPerPixel + 7) / 8;
                // create a byte array to copy this image into
                byte[] pixels = new byte[bmScaled.PixelHeight * stride];
                // copy the scaled imaged into the byte array
                bmScaled.CopyPixels(pixels, stride, 0);

                // create the output image, with size equal to the specified dimensions * the size of a tile, since the dimensions are given in 'tile' units.
                WriteableBitmap background = new WriteableBitmap(tilesSize * (Convert.ToInt32(xScaling.Text)), tilesSize * (Convert.ToInt32(yScaling.Text)), bmMain.DpiX, bmMain.DpiY, bmMain.Format, null);

                // iterate over the byte array, incrementing by 4 since there are 4 bytes in each pixel (A,R,G,B)
                for (int i = 0; i <= pixels.Length - 4; i = i + 4)
                {
                    // calcluate the color of the current pixel from each set of 4 bytes
                    Color c = Color.FromArgb(pixels[i + 3], pixels[i + 2], pixels[i + 1], pixels[i]);

                    // for this pixel, find the closest tile in the tiles list
                    var closestTile = findClosestTile(tilesList, c);
                    // calculate the position where this tile should be placed from the index
                    int row = (i / 4) / (Convert.ToInt32(xScaling.Text) + 0);
                    int col = (i / 4) % (Convert.ToInt32(xScaling.Text) + 0);

                    // add the found closest tile to the output image
                    addTileToImage(closestTile, row, col, background);
                }

                // show the output image, and enable saving
                imgMain.Source    = bmResult;
                btnSave.IsEnabled = true;
            }
            finally
            {
                // reset the mouse cursor
                Mouse.OverrideCursor = null;
            }
        }
Beispiel #17
0
        public override BitmapSource Process(BitmapSource bmp, IFrameDestination dest)
        {
            var sw = new Stopwatch();

            sw.Start();

            var sliceWidth    = bmp.Width / Width;
            var sliceHeight   = bmp.Height / Height;
            var destRect      = new Int32Rect();
            var srcRect       = new Int32Rect();
            var lineRect      = new Int32Rect();
            var bytesPerPixel = (bmp.Format.BitsPerPixel + 7) / 8;

            var wBmp = new WriteableBitmap(bmp);

            srcRect.Height  = 1;
            destRect.X      = 0;
            destRect.Width  = bmp.PixelWidth;
            destRect.Height = srcRect.Height;
            lineRect.Height = 1;
            lineRect.X      = 0;
            lineRect.Width  = bmp.PixelWidth;
            var blockSize = bytesPerPixel * bmp.PixelWidth * srcRect.Height;
            var stride    = bmp.PixelWidth * bytesPerPixel;
            var buffer    = new byte[blockSize];
            var line      = new WriteableBitmap(bmp.PixelWidth, 1, bmp.DpiX, bmp.DpiY, bmp.Format, bmp.Palette);

            for (var y = 0; y < bmp.PixelHeight; y++)
            {
                // copy line
                lineRect.Y = y;
                bmp.CopyPixels(lineRect, buffer, stride, 0);
                lineRect.Y = 0;
                line.WritePixels(lineRect, buffer, stride, 0);

                // calculate scaling
                var deltaAbs = (double)bmp.PixelWidth / 2 * Distortion;
                var deltaRel = deltaAbs * (1 - (double)y / bmp.PixelHeight);
                srcRect.Width = bmp.PixelWidth - (int)(2 * deltaRel);
                srcRect.X     = (int)deltaRel;
                srcRect.Y     = y;
                destRect.Y    = y;
                var scaleX = (double)bmp.PixelWidth / srcRect.Width;

                // scale line
                var scaledLine = new TransformedBitmap(line, new ScaleTransform(scaleX, 1, (double)line.PixelWidth / 2, 0));

                // copy scaled line to dest
                lineRect.X = (int)((bmp.PixelWidth * scaleX) - bmp.PixelWidth) / 2;
                scaledLine.CopyPixels(lineRect, buffer, stride, 0);
                lineRect.X = 0;
                lineRect.Y = y;
                wBmp.WritePixels(lineRect, buffer, stride, 0);
            }

            sw.Stop();
            Console.WriteLine("Distored image in {0}ms.", sw.ElapsedMilliseconds);

            wBmp.Freeze();
            _whenProcessed.OnNext(wBmp);
            return(wBmp);
        }
Beispiel #18
0
        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                System.Console.WriteLine("Not enough arguments given use FaceSegmentationCMD inputimage outputimage");
            }
            else
            {
                byte[] REDLABEL   = { 0, 0, 0, 255, 0, 255, 255, 255, 128, 255, 0 };
                byte[] GREENLABEL = { 0, 255, 0, 0, 255, 255, 0, 255, 128, 192, 128 };
                byte[] BLUELABEL  = { 0, 0, 255, 0, 255, 0, 255, 255, 128, 192, 128 };
                var    options    = new SessionOptions();
                options.SetSessionGraphOptimizationLevel(2);
                var    path     = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
                String onnxfile = path + "\\facesegmentation_full_344.onnx";


                InferenceSession session = null;
                try
                {
                    session = new InferenceSession(onnxfile, options);
                } catch (Exception e)
                {
                    System.Console.WriteLine("Could not load ONNX model, because " + e.ToString());
                    return;
                }
                try
                {
                    var    bitmap       = new BitmapImage(new Uri(args[0]));
                    var    bitmapWidth  = bitmap.Width;
                    var    bitmapHeight = bitmap.Height;
                    var    inputMeta    = session.InputMetadata;
                    var    container    = new List <NamedOnnxValue>();
                    int[]  inputDim     = new int[4];
                    double scaleFactor  = 1.0;
                    foreach (var name in inputMeta.Keys)
                    {
                        var dim = inputMeta[name].Dimensions;
                        for (int n = 0; n < dim.Length; n++)
                        {
                            inputDim[n] = dim[n];
                        }
                    }
                    if (bitmapWidth > bitmapHeight)
                    {
                        scaleFactor = (double)inputDim[3] / bitmapWidth;
                    }
                    else
                    {
                        scaleFactor = (double)inputDim[2] / bitmapHeight;
                    }
                    TransformedBitmap tb  = new TransformedBitmap(bitmap, new System.Windows.Media.ScaleTransform(scaleFactor, scaleFactor));
                    int    newWidth       = tb.PixelWidth;
                    int    newHeight      = tb.PixelHeight;
                    int    channels       = tb.Format.BitsPerPixel / 8;
                    int    stride         = channels * newWidth;
                    byte[] rawData        = new byte[stride * newHeight];
                    byte[] rawLabelOutput = new byte[inputDim[2] * inputDim[3]];
                    byte[] rawOutput      = new byte[stride * newHeight];
                    tb.CopyPixels(rawData, stride, 0);
                    int     paddingX = inputDim[3] - newWidth;
                    int     paddingY = inputDim[2] - newHeight;
                    float[] testData = new float[inputDim[2] * inputDim[3] * inputDim[1]];
                    // intialize the whole tensor data to background value so do not have to deal with padding later
                    for (int n = 0; n < inputDim[2] * inputDim[3] * inputDim[1]; n++)
                    {
                        testData[n] = -1.0f;
                    }
                    var offsetX = paddingX / 2;
                    var offsetY = paddingY / 2;
                    // fill up tensor with image data
                    for (int y = 0; y < newHeight; y++)
                    {
                        int y1 = y;
                        for (int x = 0; x < newWidth; x++)
                        {
                            testData[(x + offsetX) + (y + offsetY) * inputDim[3] + inputDim[2] * inputDim[3] * 2] = rawData[(x + y1 * newWidth) * channels] / 127.5f;
                            testData[(x + offsetX) + (y + offsetY) * inputDim[3] + inputDim[2] * inputDim[3]]     = rawData[(x + y1 * newWidth) * channels + 1] / 127.5f;
                            testData[(x + offsetX) + (y + offsetY) * inputDim[3]] = rawData[(x + y1 * newWidth) * channels + 2] / 127.5f;
                            testData[(x + offsetX) + (y + offsetY) * inputDim[3] + inputDim[2] * inputDim[3] * 2] -= 1.0f;
                            testData[(x + offsetX) + (y + offsetY) * inputDim[3] + inputDim[2] * inputDim[3]]     -= 1.0f;
                            testData[(x + offsetX) + (y + offsetY) * inputDim[3]] -= 1.0f;
                        }
                    }
                    foreach (var name in inputMeta.Keys)
                    {
                        var tensor = new DenseTensor <float>(testData, inputMeta[name].Dimensions);
                        container.Add(NamedOnnxValue.CreateFromTensor <float>(name, tensor));
                    }
                    using (var results = session.Run(container))
                    {
                        int numResults = results.Count;
                        foreach (var r in results)
                        {
                            System.Console.WriteLine(r.Name);
                            var resultTensor    = r.AsTensor <float>();
                            var resultDimension = resultTensor.Dimensions;
                            System.Console.WriteLine(resultDimension.Length);
                            var     resultArray = resultTensor.ToArray();
                            float[] pointVal    = new float[resultDimension[1]];
                            for (var y = 0; y < resultDimension[2]; y++)
                            {
                                for (var x = 0; x < resultDimension[3]; x++)
                                {
                                    for (var n = 0; n < resultDimension[1]; n++)
                                    {
                                        pointVal[n] = resultArray[x + y * resultDimension[3] + n * resultDimension[2] * resultDimension[3]];
                                    }
                                    Softmax(pointVal);
                                    byte labelVal = (byte)MaxIndex(pointVal);
                                    rawLabelOutput[x + y * resultDimension[3]] = labelVal;
                                }
                            }
                            if (resultDimension[1] < 64)
                            {
                                for (int y = 0; y < newHeight; y++)
                                {
                                    for (int x = 0; x < newWidth; x++)
                                    {
                                        int n = rawLabelOutput[(x + offsetX) + (y + offsetY) * resultDimension[3]];
                                        rawOutput[(x + y * newWidth) * channels + 3] = 255;
                                        rawOutput[(x + y * newWidth) * channels + 2] = REDLABEL[n];
                                        rawOutput[(x + y * newWidth) * channels + 1] = GREENLABEL[n];
                                        rawOutput[(x + y * newWidth) * channels]     = BLUELABEL[n];
                                    }
                                }
                            }
                            else
                            {
                                for (int y = 0; y < newHeight; y++)
                                {
                                    for (int x = 0; x < newWidth; x++)
                                    {
                                        int n = rawLabelOutput[(x + offsetX) + (y + offsetY) * resultDimension[3]];
                                        rawOutput[(x + y * newWidth) * channels + 3] = 255;
                                        rawOutput[(x + y * newWidth) * channels + 2] = (byte)n;
                                        rawOutput[(x + y * newWidth) * channels + 1] = (byte)n;
                                        rawOutput[(x + y * newWidth) * channels]     = (byte)n;
                                    }
                                }
                            }
                            var outputImage = ImageFromRawBgraArray(rawOutput, newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                            outputImage = ResizeImage(outputImage, (int)bitmapHeight, (int)bitmapWidth);
                            outputImage.Save(args[1]);
                        }
                    }
                }
                catch (Exception e)
                {
                    System.Console.WriteLine("Could not load image because of " + e.ToString());
                }
            }
        }