//パレットの色で減色
        private BitmapSource ReduceColor指定色で減色(BitmapSource source, List <Color> palette)
        {
            if (palette.Count == 0)
            {
                return(source);
            }
            var wb     = new WriteableBitmap(source);
            int h      = wb.PixelHeight;
            int w      = wb.PixelWidth;
            int stride = wb.BackBufferStride;

            byte[] pixels = new byte[h * stride];
            wb.CopyPixels(pixels, stride, 0);
            long   p = 0;
            int    pIndex = 0;
            double min, distance;
            Color  myColor;

            for (int y = 0; y < h; ++y)
            {
                for (int x = 0; x < w; ++x)
                {
                    p       = y * stride + (x * 3);
                    myColor = Color.FromRgb(pixels[p + 0], pixels[p + 1], pixels[p + 2]);
                    min     = GetColorDistance(myColor, palette[0]);
                    pIndex  = 0;
                    for (int i = 0; i < palette.Count; ++i)
                    {
                        distance = GetColorDistance(myColor, palette[i]);
                        if (min > distance)
                        {
                            min    = distance;
                            pIndex = i;
                        }
                    }
                    myColor       = palette[pIndex];
                    pixels[p]     = myColor.R;
                    pixels[p + 1] = myColor.G;
                    pixels[p + 2] = myColor.B;
                }
            }
            wb.WritePixels(new Int32Rect(0, 0, w, h), pixels, stride, 0);
            return(wb);
        }
예제 #2
0
        // This transform block will be used in a future version of the applicaton
        private void InitializeBodyTransformer()
        {
            _BodyTransformer = new TransformBlock <ImageModel, ImageModel>(imageModel =>
            {
                foreach (Body body in imageModel.Bodies)
                {
                    if (body.IsTracked)
                    {
                        Joint j = body.Joints[JointType.HandRight];
                        if (j.TrackingState.Equals(TrackingState.Tracked))
                        {
                            DepthSpacePoint point = this.kinectSensor.CoordinateMapper.MapCameraPointToDepthSpace(j.Position);
                            int x = (int)Math.Floor(point.X + 0.5);
                            int y = (int)Math.Floor(point.Y + 0.5);
                            if ((x >= 0) && (x < imageModel.ColorWidth) && (y >= 0) && (y < imageModel.ColorHeight))
                            {
                                Dispatcher.Invoke(() =>
                                {
                                    byte[] temp = new byte[_pointerBitmap.PixelWidth * _pointerBitmap.PixelHeight * 4];
                                    _pointerBitmap.CopyPixels(temp, _pointerBitmap.BackBufferStride, 0);

                                    for (int posX = 0; posX < _pointerBitmap.PixelWidth; posX++)
                                    {
                                        for (int posY = 0; posY < _pointerBitmap.PixelHeight; posY++)
                                        {
                                            int pointerPosition = ((_pointerBitmap.PixelWidth * posY) + posX) * 4;
                                            int imagePosition   = (((y + posY) * imageModel.DepthWidth) + (x + posX)) * 4;

                                            imageModel.DisplayPixels[imagePosition]     = temp[pointerPosition];
                                            imageModel.DisplayPixels[imagePosition + 1] = temp[pointerPosition + 1];
                                            imageModel.DisplayPixels[imagePosition + 2] = temp[pointerPosition + 2];
                                            imageModel.DisplayPixels[imagePosition + 3] = temp[pointerPosition + 3];
                                        }
                                    }
                                });
                            }
                        }
                    }
                }


                return(imageModel);
            });
        }
        //今表示している画像をニアレストネイバー法で2倍表示
        private BitmapSource F4今表示している画像をニアレストネイバー法で2倍(BitmapSource source)
        {
            if (source == null)
            {
                return(null);
            }
            if (source.PixelHeight > 1000)
            {
                if (MessageBox.Show("縦ピクセル2000以上の大きな画像になる、実行する?", "処理実行確認", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
                {
                    return(source);
                }
            }
            var wb     = new WriteableBitmap(source);
            int h      = wb.PixelHeight;
            int w      = wb.PixelWidth;
            int stride = wb.BackBufferStride;
            var pixels = new byte[h * stride];

            wb.CopyPixels(pixels, stride, 0);
            int  nH = h * 2;
            int  nW = w * 2;
            var  nWb = new WriteableBitmap(nW, nH, 96, 96, source.Format, source.Palette);
            int  nStride = nWb.BackBufferStride;
            var  nPixels = new byte[nH * nStride];
            long nP = 0, p = 0;
            int  bpp = source.Format.BitsPerPixel / 8;

            for (int y = 0; y < nH; ++y)
            {
                for (int x = 0; x < nW; ++x)
                {
                    nP = y * nStride + (x * bpp);
                    p  = (y / 2) * stride + ((x / 2) * bpp);
                    for (int i = 0; i < bpp; ++i)
                    {
                        nPixels[nP + i] = pixels[p + i];
                    }
                }
            }

            nWb.WritePixels(new Int32Rect(0, 0, nW, nH), nPixels, nStride, 0);
            return(nWb);
        }
예제 #4
0
        public override void Initialize(AVSValue args, ScriptEnvironment env)
        {
            string          path       = args[0].AsString();
            BitmapImage     BitmapFile = new BitmapImage(new Uri(path));
            WriteableBitmap Bitmap     = new WriteableBitmap(BitmapFile);

            vi.pixel_type      = ColorSpaces.CS_BGR32;
            vi.fps_denominator = 1001;
            vi.fps_numerator   = 30000;
            vi.height          = (int)BitmapFile.Height;
            vi.width           = (int)BitmapFile.Width;
            vi.num_frames      = 1000;
            SetVideoInfo(ref vi);

            imagePitch = Bitmap.BackBufferStride;
            byte[] BitmapData = new byte[vi.height * imagePitch];
            Bitmap.CopyPixels(BitmapData, imagePitch, 0);
            imageStream = new MemoryStream(BitmapData, 0, vi.height * imagePitch, false, true);
        }
예제 #5
0
        //2.368 ColorReverce1の2重ループを1重ループに変更
        private void ColorReverce3()
        {
            var wb     = new WriteableBitmap(OriginBitmap);
            int w      = wb.PixelWidth;
            int h      = wb.PixelHeight;
            int stride = wb.BackBufferStride;

            byte[] pixels = new byte[h * stride];
            wb.CopyPixels(pixels, stride, 0);
            for (int i = 0; i < pixels.Length; i += 4)
            {
                //RGBを反転、アルファ値はそのまま
                pixels[i]     = (byte)(255 - pixels[i]);
                pixels[i + 1] = (byte)(255 - pixels[i + 1]);
                pixels[i + 2] = (byte)(255 - pixels[i + 2]);
            }
            wb.WritePixels(new Int32Rect(0, 0, w, h), pixels, stride, 0);
            MyImage.Source = wb;
        }
예제 #6
0
        /// <summary>
        /// Converting to bitmap and dividing to vectors.
        /// </summary>
        /// <param name="imageToEdit"></param>
        public void ConvertToGrayscale()
        {
            var bitmapImage = ConvertBitmapSourceToBitmapImage();

            _bitmap = new WriteableBitmap(bitmapImage);

            CalculateImageStrideLength(bitmapImage);
            int arraySize = _imageStride * _bitmap.PixelHeight;

            byte[] pixels = new byte[arraySize];
            _bitmap.CopyPixels(pixels, _imageStride, 0);

            // Creating an instance of DLL menager and setting params.
            _myProcessingData.ThreatsNum = ThreadsNum;
            _myProcessingData.IsAsm      = IsAsm;
            _myProcessingData.SplitByteArrayToRegisters(pixels);

            _myProcessingData.RunConversionProcess();
        }
        public void ProcessImage(XRayImage xRayImage, IProcessor processor)
        {
            ImageHistory.Push(xRayImage.XRayBitmap);
            var originalImage = xRayImage.XRayBitmap;

            BitmapSource bitmapSource  = new FormatConvertedBitmap(originalImage, PixelFormats.Pbgra32, null, 0);
            var          modifiedImage = new WriteableBitmap(bitmapSource);

            var height      = modifiedImage.PixelHeight;
            var width       = modifiedImage.PixelWidth;
            var pixelData   = new int[width * height];
            var widthInByte = 4 * width;

            modifiedImage.CopyPixels(pixelData, widthInByte, 0);
            var processedPixelData = processor.Process(pixelData, width, height);

            modifiedImage.WritePixels(new Int32Rect(0, 0, width, height), processedPixelData, widthInByte, 0);
            xRayImage.XRayBitmap = modifiedImage.ToBitmapImage();
        }
        //画像の全ピクセルの色をColorの配列にして返す
        private Color[] GetAllPixelsColor(BitmapSource source)
        {
            var wb     = new WriteableBitmap(source);
            int h      = wb.PixelHeight;
            int w      = wb.PixelWidth;
            int stride = wb.BackBufferStride;

            byte[] pixels = new byte[h * stride];
            wb.CopyPixels(pixels, stride, 0);
            int p = 0;

            Color[] color = new Color[h * w];
            for (int i = 0; i < color.Length; ++i)
            {
                p        = i * 4;
                color[i] = Color.FromRgb(pixels[p + 2], pixels[p + 1], pixels[p]);
            }
            return(color);
        }
예제 #9
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");
            }
        }
예제 #10
0
        public static Mat <byte> BitmapSourceToMat(BitmapSource source)
        {
            WriteableBitmap writeableBmp = new WriteableBitmap(source);

            byte[] rawData = new byte[writeableBmp.PixelWidth * writeableBmp.PixelHeight];
            writeableBmp.CopyPixels(rawData, writeableBmp.PixelWidth, 0);

            Mat <byte> image = new Mat <byte>(writeableBmp.PixelHeight, writeableBmp.PixelWidth);

            for (int i = 0; i < writeableBmp.PixelHeight; i++)
            {
                for (int j = 0; j < writeableBmp.PixelWidth; j++)
                {
                    image[i, j] = rawData[i * writeableBmp.PixelWidth + j];
                }
            }

            return(image);
        }
예제 #11
0
        public static WriteableBitmap ConvertToGrayScale(ZoomBorder border, BitmapImage source, Int32Rect roi)
        {
            WriteableBitmap wb = new WriteableBitmap(source);

            int[] grayPixels   = new int[wb.PixelWidth * wb.PixelHeight];
            int   widthInBytes = 4 * wb.PixelWidth;

            wb.CopyPixels(grayPixels, widthInBytes, 0);

            //foreach (int x in index)
            for (int x = 0; x < wb.PixelWidth * wb.PixelHeight; x++)
            {
                // get the pixel
                int pixel = grayPixels[x];

                // get the component
                int red   = (pixel & 0x00FF0000) >> 16;
                int blue  = (pixel & 0x0000FF00) >> 8;
                int green = (pixel & 0x000000FF);

                // get the average
                int average = (byte)((red + blue + green) / 3);

                // assign the gray values keep the alpha
                unchecked
                {
                    grayPixels[x] = (int)((pixel & 0xFF000000) | (average << 16) | (average << 8) | average);
                }
            }

            if (roi.Height > 0 && roi.Width > 0)
            {
                roi = RoiToPixel(border, source, roi);
                wb.WritePixels(roi, grayPixels, widthInBytes, (roi.Y * source.PixelWidth) + roi.X);
            }
            else
            {
                wb.WritePixels(new Int32Rect(0, 0, wb.PixelWidth, wb.PixelHeight), grayPixels, widthInBytes, 0);
            }

            return(wb);
        }
예제 #12
0
        //できた!範囲外にならない、穴も開かない、あと足りないのは補間
        private void Kaiten4(float kakudo)
        {
            var wb     = new WriteableBitmap(OriginBitmap);
            int w      = wb.PixelWidth;
            int h      = wb.PixelHeight;
            int stride = wb.BackBufferStride;
            var pixels = new byte[h * stride];

            wb.CopyPixels(pixels, stride, 0);//元画像のピクセル

            //変換後画像のサイズを適正なものに
            Rect nRect = JustぴったりサイズRect(OriginBitmap, kakudo);
            int  nw = (int)nRect.Width + 1;//Rectは1からカウントだけどBitmapは0だから+1している
            int  nh = (int)nRect.Height + 1;
            var  nWb = new WriteableBitmap(nw, nh, 96, 96, wb.Format, wb.Palette);
            int  nStride = nWb.BackBufferStride;
            var  nPixels = new byte[nh * nStride];//回転後のピクセル用
            long p, np;

            //変換後の座標を逆変換して元の座標を求めてその色を入れる
            for (int ny = 0; ny < nh; ny++)//変換後y座標
            {
                for (int nx = 0; nx < nw; nx++)
                {
                    //変換後座標をオフセットしてから
                    Point nPoint = new Point(nx + nRect.X, ny + nRect.Y);
                    //逆回転変換して元の座標取得
                    Point point = GetRotate逆回転座標(nPoint, kakudo);
                    int   xx    = (int)point.X;//切り捨て
                    int   yy    = (int)point.Y;
                    //元画像の範囲内に収まった座標だけ色を入れる
                    if (xx >= 0 && xx <= w - 1 && yy >= 0 && yy <= h - 1)
                    {
                        p           = yy * stride + xx;
                        np          = ny * nStride + nx;
                        nPixels[np] = pixels[p];//変換後座標に元座標の色を入れる
                    }
                }
            }
            nWb.WritePixels(new Int32Rect(0, 0, nw, nh), nPixels, nStride, 0);
            MyImage.Source = nWb;
        }
예제 #13
0
        /// <summary>
        /// Colors the connected objects in the bitmap. Note this function assumes the image is thresholded
        /// </summary>
        /// <param name="bitmap">The bitmap to color</param>
        /// <returns>A list of final colors in the image, one for each object</returns>
        public static List <Color> ColorBitmap(WriteableBitmap bitmap)
        {
            int stride = (bitmap.PixelWidth * bitmap.Format.BitsPerPixel + 7) / 8;

            byte[] pixelArray = new byte[bitmap.PixelHeight * stride];
            bitmap.CopyPixels(pixelArray, stride, 0);

            var colorMergeDictionary = new Dictionary <int, int>();
            var colors = new List <Color>();
            var random = new Random();

            for (int i = 0; i < bitmap.PixelHeight; i++)
            {
                for (int j = 0; j < bitmap.PixelWidth; j++)
                {
                    int index = i * stride + 4 * j;

                    // Check if it is a foreground object and has not been colored
                    if (GetPixelColor(pixelArray, index) == Colors.Black)
                    {
                        var color = GetPixelColoring(pixelArray, index, stride, colors, random, colorMergeDictionary);
                        pixelArray[index]     = color.B;
                        pixelArray[index + 1] = color.G;
                        pixelArray[index + 2] = color.R;

                        // Keep track of all the colors in the image so
                        // we can merge them later
                        if (!colors.Contains(color))
                        {
                            colors.Add(color);
                            colorMergeDictionary[colors.Count - 1] = colors.Count - 1;
                        }
                    }
                }
            }

            var trueColors = MergeConnectedColors(pixelArray, colors, colorMergeDictionary);

            bitmap.WritePixels(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight), pixelArray, stride, 0);

            return(trueColors);
        }
예제 #14
0
        public static WriteableBitmap TurnImageBlackWhite(BitmapSource image)
        {
            var bitmap = new WriteableBitmap(image);

            var width  = bitmap.PixelWidth;
            var height = bitmap.PixelHeight;

            var stride = width * ((bitmap.Format.BitsPerPixel + 7) / 8);

            var arraySize = stride * height;
            var pixels    = new byte[arraySize];

            //copy all data about pixels values into 1-dimentional array
            bitmap.CopyPixels(pixels, stride, 0);

            var j = 0;

            //count occurences of each intensity value which occur in image and store it
            for (var i = 0; i < pixels.Length / 4; i++)
            {
                //get pixels channels values
                var r = pixels[j + 2];
                var g = pixels[j + 1];
                var b = pixels[j];

                //set pixels to grayscale
                var s = (int)Math.Round((b + g + r) / 3.0, 0, MidpointRounding.AwayFromZero);

                pixels[j + 2] = (byte)s;
                pixels[j + 1] = (byte)s;
                pixels[j]     = (byte)s;

                j += 4;
            }

            var rect = new Int32Rect(0, 0, width, height);

            bitmap.WritePixels(rect, pixels, stride, 0);

            return(bitmap);
        }
예제 #15
0
        //ポスタリゼーション
        //4段階、
        private BitmapSource Gensyoku4division(BitmapSource source)
        {
            var wb     = new WriteableBitmap(source);
            int h      = wb.PixelHeight;
            int w      = wb.PixelWidth;
            int stride = wb.BackBufferStride;

            byte[] pixles = new byte[h * stride];
            wb.CopyPixels(pixles, stride, 0);
            long p = 0;

            for (int y = 0; y < h; ++y)
            {
                for (int x = 0; x < w; ++x)
                {
                    p = y * stride + (x * 4);
                    for (int i = 0; i < 3; ++i)
                    {
                        if (pixles[p + i] < 63.8)
                        {
                            pixles[p + i] = 0;
                        }
                        else if (pixles[p + i] < 128)
                        {
                            pixles[p + i] = 85;
                        }
                        else if (pixles[p + i] < 191)
                        {
                            pixles[p + i] = 170;
                        }

                        else
                        {
                            pixles[p + i] = 255;
                        }
                    }
                }
            }
            wb.WritePixels(new Int32Rect(0, 0, w, h), pixles, stride, 0);
            return(wb);
        }
예제 #16
0
        //Обработка изображения
        private byte[] DetectEdges(WriteableBitmap image, int kernelSize)
        {
            var pixels = new byte[image.PixelHeight * image.BackBufferStride];

            image.CopyPixels(pixels, image.BackBufferStride, 0);

            double[,] kernel = CreateEdgeKernel(kernelSize);

            int deviation = DeviationCalc(kernelSize);

            var resultPixels = new byte[image.PixelHeight * image.BackBufferStride];

            for (int i = deviation; i < image.PixelWidth - deviation; i++)
            {
                for (int j = deviation; j < image.PixelHeight - deviation; j++)
                {
                    int index = j * image.BackBufferStride + 4 * i;

                    for (int c = 0; c < 3; c++)
                    {
                        double result = 0;

                        for (int k = i - deviation, w = 0; k <= i + deviation; k++, w++)
                        {
                            for (int l = j - deviation, h = 0; l <= j + deviation; l++, h++)
                            {
                                int indexGrid = l * image.BackBufferStride + 4 * k;

                                result += pixels[indexGrid + c] * kernel[w, h];
                            }
                        }

                        resultPixels[index + c] = (byte)(result);
                    }

                    resultPixels[index + 3] = pixels[index + 3];
                }
            }

            return(resultPixels);
        }
예제 #17
0
        private int CalculateThreshold(WriteableBitmap input, int offset)
        {
            var height = input.PixelHeight;
            var width  = input.PixelWidth;
            var coord  = new Coord(height, width);
            var pixels = new byte[coord.Size];

            input.CopyPixels(pixels, coord.Stride, 0);
            var sum = 0;

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    sum += pixels[coord.Get(x, y)];
                }
            }
            var threshold = (double)sum / (double)(width * height);

            return((int)threshold + offset);
        }
예제 #18
0
        public MainWindow()
        {
            InitializeComponent();
            // double width = MainGrid.ColumnDefinitions[1].ActualWidth;
            IsDrawing       = false;
            VertexDrag      = false;
            PolygonDrag     = false;
            polygonFinished = false;
            wbmap           = new WriteableBitmap(1067, 657, 300, 300, PixelFormats.Bgra32, null);
            vm = new MainWindowViewModel(wbmap.PixelWidth * wbmap.PixelHeight * wbmap.Format.BitsPerPixel / 8);
            wbmap.CopyPixels(vm.BitmapArraySource, wbmap.BackBufferStride, 0);
            Console.WriteLine("hello");

            MainImage.Source = wbmap;
            double height = MainImage.Height;
            double width  = MainImage.Width;

            actheight = MainImage.ActualHeight;
            actwidth  = MainImage.ActualWidth;
            Console.WriteLine("height: " + height + " width: " + width + " actual height: " + actheight + " actual width: " + actwidth);
        }
예제 #19
0
        public void execute()
        {
            if (Path != null)
            {
                origin = LoadBitmap(Path);
            }

            try
            {
                if (origin != null)
                {
                    origin = origin.Resize(68, 42, WriteableBitmapExtensions.Interpolation.NearestNeighbor);
                    origin.CopyPixels(new Int32Rect(0, 0, origin.PixelWidth, origin.PixelHeight), monitor.BackBuffer,
                                      monitor.BackBufferStride * monitor.PixelHeight, monitor.BackBufferStride);
                }
            }
            catch (AccessViolationException e)
            {
                MessageBox.Show(e.Message);
            }
        }
예제 #20
0
        /// <summary>
        /// Tints the given image by a colour
        /// </summary>
        /// <param name="filename">The file to tint</param>
        /// <param name="color"></param>
        /// <returns>The tinted version of the image</returns>
        public static WriteableBitmap TintImage(string filename, int color)
        {
            // loads the specified image from the application data
            var image = new WriteableBitmap(new BitmapImage(new Uri("pack://application:,,,/PopoverGPMDP;component/img/" + filename, UriKind.Absolute)));

            var bytes = new byte[image.BackBufferStride * image.PixelHeight];

            image.CopyPixels(bytes, image.BackBufferStride, 0);

            for (var i = 0; i < bytes.Length; i++)
            {
                if (i % 4 != 3) // skip over the transparency byte
                {
                    bytes[i] = (byte)(color >> 8 * (i % 4) & 0xff);
                }
            }

            image.WritePixels(new Int32Rect(0, 0, image.PixelWidth, image.PixelHeight), bytes, image.BackBufferStride, 0);

            return(image);
        }
예제 #21
0
        private int[,] FindObjects(WriteableBitmap image, int width, int height)
        {
            int stride = image.PixelWidth * (image.Format.BitsPerPixel / 8);

            byte[] data = new byte[stride * image.PixelHeight];
            image.CopyPixels(data, stride, 0);
            int[,] objectMask = new int[width, height];

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    int index = (y * stride) + (x * 4);
                    if (data[index] == 0)
                    {
                        objectMask[x, y] = 1;
                    }
                }
            }
            return(objectMask);
        }
예제 #22
0
        private async void MyButton1_Click(object sender, RoutedEventArgs e)
        {
            MyButtonボタンの有効設定(true);
            var wb     = new WriteableBitmap(OriginBitmap);
            int w      = wb.PixelWidth;
            int h      = wb.PixelHeight;
            int stride = wb.BackBufferStride;

            byte[] pixels = new byte[h * stride];
            wb.CopyPixels(pixels, stride, 0);
            MyCancelSource = new CancellationTokenSource();
            CancellationToken token = MyCancelSource.Token;
            //MyProgressBar.IsIndeterminate = true;
            Progress <int> progress = new Progress <int>(MyProgressUpdate);
            await Task.Run(() => ColorReverce(pixels, w, h, stride, token, progress));

            wb.WritePixels(new Int32Rect(0, 0, w, h), pixels, stride, 0);
            MyImage.Source = wb;
            //MyProgressBar.IsIndeterminate = false;
            MyButtonボタンの有効設定(false);
        }
예제 #23
0
        public unsafe override void Draw(WriteableBitmap desktop)
        {
            // Avoid exception if window is dragged bottom of screen
            if (rectangle.Top + rectangle.Height >= framebuffer.Height)
            {
                rectangle.Height = framebuffer.Height - rectangle.Top - 1;
            }

            if ((rectangle.Y * desktop.PixelWidth + rectangle.X) < (source.Y * desktop.PixelWidth + source.X))
            {
                Int32Rect srcRect = new Int32Rect(source.X, source.Y, rectangle.Width, rectangle.Height);
                desktop.WritePixels(srcRect, desktop.BackBuffer, desktop.BackBufferStride * desktop.PixelHeight, desktop.PixelWidth * 4, rectangle.X, rectangle.Y);
            }
            else
            {
                Int32[] pixelBuf = new Int32[rectangle.Width * rectangle.Height];

                desktop.CopyPixels(new Int32Rect(source.X, source.Y, rectangle.Width, rectangle.Height), pixelBuf, rectangle.Width * 4, 0);
                desktop.WritePixels(new Int32Rect(0, 0, rectangle.Width, rectangle.Height), pixelBuf, rectangle.Width * 4, rectangle.X, rectangle.Y);
            }
        }
예제 #24
0
        public static WriteableBitmap EqualizeHistogram(int[] lutR, int[] lutG, int[] lutB)
        {
            var imgSource = (BitmapSource)MainWindow.ModifiedImgSingleton.Source;
            var bitmap    = new WriteableBitmap(imgSource);

            var width  = imgSource.PixelWidth;
            var height = imgSource.PixelHeight;

            var stride = width * ((imgSource.Format.BitsPerPixel + 7) / 8);

            var arraySize = stride * height;
            var pixels    = new byte[arraySize];

            //copy all data about pixels values into 1-dimentional array
            bitmap.CopyPixels(pixels, stride, 0);

            var j = 0;

            //count occurences of each intensity value which occur in image and store it
            for (var i = 0; i < pixels.Length / 4; i++)
            {
                //get pixels channels values
                var r = pixels[j + 2];
                var g = pixels[j + 1];
                var b = pixels[j];

                //set new values according to lut
                pixels[j + 2] = (byte)lutR[r];
                pixels[j + 1] = (byte)lutG[g];
                pixels[j]     = (byte)lutB[b];

                j += 4;
            }

            var rect = new Int32Rect(0, 0, width, height);

            bitmap.WritePixels(rect, pixels, stride, 0);

            return(bitmap);
        }
예제 #25
0
        /// <summary>
        /// Prepare image data to send
        /// </summary>
        /// <param name="depthImageFrame"></param>
        /// <returns></returns>
        public bool PrepareImageData()
        {
            WriteableBitmap colorBitmap = this.ImageSource as WriteableBitmap;
            WriteableBitmap depthBitmap = this.DepthSource as WriteableBitmap;

            this.colorData = new Byte[colorBitmap.PixelHeight * colorBitmap.PixelWidth * bgr32BytesPerPixel];
            this.depthData = new Byte[depthBitmap.PixelHeight * depthBitmap.PixelWidth * gray8BytesPerPixel];

            colorBitmap.CopyPixels(this.colorData, colorBitmap.BackBufferStride, 0);
            depthBitmap.CopyPixels(this.depthData, depthBitmap.BackBufferStride, 0);

            float[] pointTemp = new float[1920 * 1080 * 3]; //3 dimension

            CameraSpacePoint[] cameraSpacePoints = new CameraSpacePoint[1920 * 1080];
            ushort[]           depthData         = new ushort[512 * 424];

            Microsoft.Kinect.KinectSensor sensor = Microsoft.Kinect.KinectSensor.GetDefault();
            DepthFrameReader e      = sensor.DepthFrameSource.OpenReader();
            DepthFrame       eFrame = e.AcquireLatestFrame();

            eFrame.CopyFrameDataToArray(depthData);

            // Get 3D point coordinates
            CoordinateMapper coordinateMapper = sensor.CoordinateMapper;

            coordinateMapper.MapColorFrameToCameraSpace(depthData, cameraSpacePoints);

            // Save 3D point coordinates to point3D array
            for (int i = 0; i < 1920 * 1080; ++i)
            {
                pointTemp[i * 3]     = cameraSpacePoints[i].X;
                pointTemp[i * 3 + 1] = cameraSpacePoints[i].Y;
                pointTemp[i * 3 + 2] = cameraSpacePoints[i].Z;
            }

            depthPointsInColorCoordinate = new byte[1920 * 1080 * 3 * sizeof(float)];
            Buffer.BlockCopy(pointTemp, 0, depthPointsInColorCoordinate, 0, 1920 * 1080 * 3 * sizeof(float));

            return(true);
        }
예제 #26
0
        private void GetPixelMap(string path, PixelMap[] sources)
        {
            var bitmap = new BitmapImage(new Uri(path, UriKind.Relative));
            var wb     = new WriteableBitmap(bitmap);

            var width  = wb.PixelWidth;
            var height = wb.PixelHeight;
            var bpp    = wb.Format.BitsPerPixel;
            var dpiX   = wb.DpiX;
            var dpiY   = wb.DpiY;

            for (var i = 0; i < sources.Length; i++)
            {
                sources[i] = new PixelMap(width, height, dpiX, dpiY, bpp);
            }

            var stride = wb.PixelWidth * ((bpp + 7) / 8);

            var data = new byte[width * height * 4];

            wb.CopyPixels(data, stride, 0);

            for (var y = 0; y < height; y++)
            {
                for (var x = 0; x < width; x++)
                {
                    foreach (var source in sources)
                    {
                        source[x, y] = new Pixel
                        {
                            R = data[(y * width + x) * 4 + 0],
                            G = data[(y * width + x) * 4 + 1],
                            B = data[(y * width + x) * 4 + 2],
                            A = data[(y * width + x) * 4 + 3]
                        }
                    }
                    ;
                }
            }
        }
예제 #27
0
        private byte[] ChangeMaskColor(WriteableBitmap bitmap, Color newColor, int opacity, int diameter)
        {
            Color controlColor = new Color();

            newColor.A = (byte)opacity;
            byte[] array = new byte[Stride * diameter];
            bitmap.CopyPixels(array, Stride, 0);
            for (int i = 0; i < array.Length; i += 4)
            {
                if ((array[i] != controlColor.B ||
                     array[i + 1] != controlColor.G ||
                     array[i + 2] != controlColor.R ||
                     array[i + 3] != controlColor.A))
                {
                    array[i]     = newColor.B;
                    array[i + 1] = newColor.G;
                    array[i + 2] = newColor.R;
                    array[i + 3] = newColor.A;
                }
            }
            return(array);
        }
예제 #28
0
        public static byte[] ToArray(this WriteableBitmap bitmapSource, System.Windows.Int32Rect srcRect)
        {
            if (srcRect.IsEmpty)
            {
                return(new byte[1]);
            }
            try
            {
                // Stride = (width) x (bytes per pixel)
                int    stride = (int)srcRect.Width * (bitmapSource.Format.BitsPerPixel / 8);
                byte[] pixels = new byte[(int)srcRect.Height * stride];

                bitmapSource.CopyPixels(srcRect, pixels, stride, 0);

                return(pixels);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("********: " + ex.ToString());
                return(new byte[1]);
            }
        }
예제 #29
0
        private void LoadImage_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

                dlg.Filter = "Image Files|*.jpg;*.jpeg;*.png;";

                Nullable <bool> result = dlg.ShowDialog();
                if (result == true)
                {
                    string filename = dlg.FileName;

                    BitmapImage b = new BitmapImage();
                    b.BeginInit();
                    b.UriSource = new Uri(filename);
                    b.EndInit();

                    image_i.Source = b;

                    photo  = new WriteableBitmap(b);
                    height = photo.PixelHeight;
                    width  = photo.PixelWidth;

                    pixelData    = new uint[(int)(width * height)];
                    pixelDataTmp = new uint[(int)(width * height)];
                    widthInByte  = (int)(4 * width);

                    photo.CopyPixels(pixelData, widthInByte, 0);
                    chooseMethod_cbi.IsSelected = true;

                    DrawHistogram();
                }
            }
            catch (ArgumentException)
            {
                MessageBox.Show("File error!");
            }
        }
예제 #30
0
        private BitmapSource MakeBitmap()
        {
            int w, h, stride;

            w      = h = 10;
            stride = w;

            var wb     = new WriteableBitmap(w, h, 96, 96, PixelFormats.Gray8, null);
            var pixels = new byte[stride * h];

            wb.CopyPixels(pixels, stride, 0);

            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    pixels[y * stride + x] = (y % 2 == 0) ? (byte)0 : (byte)255;
                }
            }
            wb.WritePixels(new Int32Rect(0, 0, w, h), pixels, stride, 0);
            return(wb);
        }