当我从图像中分离像素时,像素会发生什么?

时间:2015-11-09 19:55:37

标签: c# image-processing uwp pixels

我想这个问题我问的是Bitmap编码器/解码器是如何工作的。我在Windows Paint中使用1个红色像素(BGRA = 36,27,237,255)创建一个位图,并在我的程序中打印图像和缓冲区,这是预期的。

enter image description here

但如果我将图像设为1x2并使第二个像素为黄色(0,242,254,255),则颜色会合并,我会得到一个意外的缓冲区。

JPEG:

enter image description here

PNG:

enter image description here

发生了什么事?

缓冲区是否也知道下一个像素是向左,向右,向下还是向上?例如,如果我将黄色像素放在右边,这将如何影响缓冲区?

代码:

        SoftwareBitmap detectorInput = null;
        WriteableBitmap displaySource = null;

        try
        {


            StorageFile photoFile = await StorageFile.GetFileFromPathAsync(Windows.ApplicationModel.Package.Current.InstalledLocation.Path + @"\Two.png");
            if (photoFile == null)
            {
                return;
            }

            //this.rootPage.NotifyUser("Opening...", NotifyType.StatusMessage);

            // Open the image file and decode the bitmap into memory.

            using (IRandomAccessStream fileStream = await photoFile.OpenAsync(Windows.Storage.FileAccessMode.Read))
            {
                BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);
                BitmapTransform transform = this.ComputeScalingTransformForSourceImage(decoder);

                using (SoftwareBitmap originalBitmap = await decoder.GetSoftwareBitmapAsync(decoder.BitmapPixelFormat, BitmapAlphaMode.Ignore, transform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage))
                {


                    const BitmapPixelFormat InputPixelFormat = BitmapPixelFormat.Gray8;
                    if (FaceDetector.IsBitmapPixelFormatSupported(InputPixelFormat))
                    {
                        using (detectorInput = SoftwareBitmap.Convert(originalBitmap, InputPixelFormat))
                        {
                            // Create a WritableBitmap for our visualization display; copy the original bitmap pixels to wb's buffer.
                            displaySource = new WriteableBitmap(originalBitmap.PixelWidth, originalBitmap.PixelHeight);
                            originalBitmap.CopyToBuffer(displaySource.PixelBuffer);
                            Two.Source = displaySource;


                            PixelDataProvider pixelData = await decoder.GetPixelDataAsync(
                                BitmapPixelFormat.Bgra8, // WriteableBitmap uses BGRA format 
                                BitmapAlphaMode.Straight,
                                transform,
                                ExifOrientationMode.IgnoreExifOrientation, // This sample ignores Exif orientation 
                                ColorManagementMode.DoNotColorManage
                            );

                            // An array containing the decoded image data, which could be modified before being displayed 
                            byte[] sourcePixels = pixelData.DetachPixelData();
                            two = sourcePixels;
                            foreach (byte sourcePixel in sourcePixels)
                            {
                                TwoText.Text += sourcePixel + " ";
                            }
                        }
                    }
                    else
                    {
                        return;
                        //this.rootPage.NotifyUser("PixelFormat '" + InputPixelFormat.ToString() + "' is not supported by FaceDetector", NotifyType.ErrorMessage);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            return;
            // this.rootPage.NotifyUser(ex.ToString(), NotifyType.ErrorMessage);
        }
    }

0 个答案:

没有答案