Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image processing 如何正确设置图像的步幅?_Image Processing_Bitmap_Bitmapdata - Fatal编程技术网

Image processing 如何正确设置图像的步幅?

Image processing 如何正确设置图像的步幅?,image-processing,bitmap,bitmapdata,Image Processing,Bitmap,Bitmapdata,从双[,]转换为位图时 Bitmap image = ImageDataConverter.ToBitmap(new double[,] { { .11, .11, .11, }, { .11, .11, .11, },

双[,]
转换为
位图时

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;
    }
}
公共类ImageDataConverter
{
公共静态位图ToBitmap(双[,]输入)
{
int width=input.GetLength(0);
int height=input.GetLength(1);
位图输出=灰度。创建灰度图像(宽度、高度);
BitmapData数据=输出.LockBits(新矩形(0,0,宽度,高度),
ImageLockMode.WriteOnly,
输出(像素格式);
int pixelSize=System.Drawing.Image.GetPixelFormatSize(output.PixelFormat)/8;
int offset=数据。步长-宽度*像素大小;
双最小值=0.0;
双最大值=255.0;
不安全的
{
字节*地址=(字节*)数据。Scan0.ToPointer();
对于(int y=0;y
我不知道你是怎么到5点的

跨距是一行像素(扫描线)的宽度,四舍五入到四字节边界


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

这不是4字节。这是4位。我不知道你所说的“4字节”或“4位”是什么意思。如果你使用它,它是用每像素8位表示256(2^8)灰度,意思是每像素1字节,每行3字节,四舍五入到4。颜色深度应该是多少?你为什么问我?这取决于你的要求。您发布的代码将创建具有8位灰度的图像。