【问题标题】:get image pixels into array将图像像素放入数组
【发布时间】:2014-01-23 08:34:58
【问题描述】:

我正在尝试将以下代码从 silverlight 重写为 wpf。在这里找到https://slmotiondetection.codeplex.com/

我的问题是 wpf 中缺少 WritaeableBitmap.Pixels。如何做到这一点?我了解它是如何工作的,但我像一周前一样开始使用 C#。

你能指出正确的方向吗?

    public WriteableBitmap GetMotionBitmap(WriteableBitmap current)
    {
        if (_previousGrayPixels != null && _previousGrayPixels.Length > 0)
        {
            WriteableBitmap motionBmp = new WriteableBitmap(current.PixelWidth, current.PixelHeight);

            int[] motionPixels = motionBmp.Pixels;
            int[] currentPixels = current.Pixels;
            int[] currentGrayPixels = ToGrayscale(current).Pixels;

            for (int index = 0; index < current.Pixels.Length; index++)
            {
                byte previousGrayPixel = BitConverter.GetBytes(_previousGrayPixels[index])[0];
                byte currentGrayPixel = BitConverter.GetBytes(currentGrayPixels[index])[0];

                if (Math.Abs(previousGrayPixel - currentGrayPixel) > Threshold)
                {
                    motionPixels[index] = _highlightColor;
                }
                else
                {
                    motionPixels[index] = currentPixels[index];
                }
            }

            _previousGrayPixels = currentGrayPixels;

            return motionBmp;
        }
        else
        {
            _previousGrayPixels = ToGrayscale(current).Pixels;

            return current;
        }
    }
    public WriteableBitmap ToGrayscale(WriteableBitmap source)
    {
        WriteableBitmap gray = new WriteableBitmap(source.PixelWidth, source.PixelHeight);

        int[] grayPixels = gray.Pixels;
        int[] sourcePixels = source.Pixels;

        for (int index = 0; index < sourcePixels.Length; index++)
        {
            int pixel = sourcePixels[index];

            byte[] pixelBytes = BitConverter.GetBytes(pixel);
            byte grayPixel = (byte)(0.3 * pixelBytes[2] + 0.59 * pixelBytes[1] + 0.11 * pixelBytes[0]);
            pixelBytes[0] = pixelBytes[1] = pixelBytes[2] = grayPixel;

            grayPixels[index] = BitConverter.ToInt32(pixelBytes, 0);
        }

        return gray;
    }

`

【问题讨论】:

    标签: c# wpf silverlight


    【解决方案1】:

    为了获取位图的原始像素数据,您可以使用BitmapSource.CopyPixels 方法之一,例如像这样:

    var bytesPerPixel = (source.Format.BitsPerPixel + 7) / 8;
    var stride = source.PixelWidth * bytesPerPixel;
    var bufferSize = source.PixelHeight * stride;
    var buffer = new byte[bufferSize];
    source.CopyPixels(buffer, stride, 0);
    

    写入WriteableBitmap 可以通过它的WritePixels 方法之一完成。

    您也可以通过 WriteableBitmap 的 BackBuffer 属性访问位图缓冲区。

    要将位图转换为灰度,您可以像这样使用FormatConvertedBitmap

    var grayscaleBitmap = new FormatConvertedBitmap(source, PixelFormats.Gray8, null, 0d);
    

    【讨论】:

      猜你喜欢
      • 2018-03-16
      • 1970-01-01
      • 1970-01-01
      • 2013-03-25
      • 1970-01-01
      • 2014-07-19
      • 2021-05-20
      • 2014-08-17
      • 2013-04-24
      相关资源
      最近更新 更多