【问题标题】:How can I set the stride of an Image properly?如何正确设置图像的步幅?
【发布时间】:2017-04-08 06:40:13
【问题描述】:

在从double[,] 转换为Bitmap 时,

Bitmap image = ImageDataConverter.ToBitmap(new double[,]  
                                    {   
                                    { .11, .11, .11, }, 
                                    { .11, .11, .11, }, 
                                    { .11, .11, .11, }, 
                                    });

例程给出

data.Stride == 4

这个值从何而来?

因为double[,]3x3,所以步幅应该是5。对吧?

我怎样才能解决这个问题,不仅适用于这个,而且适用于任何维度?

相关源代码

public class ImageDataConverter
{
    public static Bitmap ToBitmap(double[,] input)
    {
        int width = input.GetLength(0);
        int height = input.GetLength(1);

        Bitmap output = Grayscale.CreateGrayscaleImage(width, height);

        BitmapData data = output.LockBits(new Rectangle(0, 0, width, height),
                                            ImageLockMode.WriteOnly,
                                            output.PixelFormat);            

        int pixelSize = System.Drawing.Image.GetPixelFormatSize(output.PixelFormat) / 8;

        int offset = data.Stride - width * pixelSize;

        double Min = 0.0;
        double Max = 255.0;

        unsafe
        {
            byte* address = (byte*)data.Scan0.ToPointer();

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    double v = 255 * (input[x, y] - Min) / (Max - Min);

                    byte value = unchecked((byte)v);

                    for (int c = 0; c < pixelSize; c++, address++)
                    {
                        *address = value;
                    }
                }

                address += offset;
            }
        }

        output.UnlockBits(data);

        return output;
    }
}

【问题讨论】:

    标签: image-processing bitmap bitmapdata


    【解决方案1】:

    不知道你是怎么到 5 点的。

    步幅是单行像素(扫描线)的宽度,四舍五入到四字节边界。

    Link

    因为它是 3x3,所以四舍五入到四字节边界的 3 是 4。

    【讨论】:

    • 这不是 4 字节。这是 4 位。
    • 我不知道您所说的“4 字节”或“4 位”是什么意思。如果您使用AForge CreateGrayscaleImage,则它使用每像素 8 位来表示 256 (2^8) 种灰度,这意味着每像素一个字节,每行三个字节,四舍五入到 4。
    • 颜色深度应该是多少?
    • 你为什么问我?这取决于您的要求。您发布的代码创建了一个 8 位灰度的图像。
    猜你喜欢
    • 1970-01-01
    • 2015-12-04
    • 1970-01-01
    • 2015-03-18
    • 2016-03-11
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多