C# SimpleTk-处理缓冲区图像的像素

C# SimpleTk-处理缓冲区图像的像素,c#,pointers,image-processing,itk,simpleitk,C#,Pointers,Image Processing,Itk,Simpleitk,我目前正在使用simpletk库来处理三维图像。我已经能够读取我的.mhd元图像并执行阈值、腐蚀、膨胀等。输入图像是3D图像(或堆叠2D图像),每个像素有一个组件 现在我将使用get buffer方法进行手动像素操作。我知道简单的ITK-doxygen和示例 我只是这样做了,但无法看到我处理过的图像-输出图像显示与输入图像完全相同(我检查了像素循环及其工作情况,因此似乎不是我的缓冲区没有使用不安全的指针处理,就是我错误地导入了缓冲区)。这是我的密码 非常感谢您的帮助和指导!!谢谢 itk.si

我目前正在使用
simpletk
库来处理三维图像。我已经能够读取我的
.mhd
元图像并执行阈值、腐蚀、膨胀等。输入图像是3D图像(或堆叠2D图像),每个像素有一个组件

现在我将使用get buffer方法进行手动像素操作。我知道简单的ITK-doxygen和示例

我只是这样做了,但无法看到我处理过的图像-输出图像显示与输入图像完全相同(我检查了像素循环及其工作情况,因此似乎不是我的缓冲区没有使用不安全的指针处理,就是我错误地导入了缓冲区)。这是我的密码

非常感谢您的帮助和指导!!谢谢

itk.simple.Image输入=新的itk.simple.Image();
string filepath=“C:\\Users\\pragun\\Desktop\\Selected Data\\stack.mhd”;
ImageFileReader readerx=新的ImageFileReader();
SetFileName(文件路径);
input=readerx.Execute();
输入=SimpleTk.Cast(输入,PixelIDValueEnum.sitkUInt8);
//计算像素数
VectorUInt32 size=input.GetSize();
VectorDouble spacing=input.GetSpacing();
Console.WriteLine(大小[0]);
控制台写入线(大小[1]);
控制台写入线(大小[2]);
IntPtr缓冲区=新的IntPtr(0);
buffer=input.GetBufferAsUInt8();
//有两种方法可以访问缓冲区:
//(1)作为“不安全”块中的指针访问底层缓冲区
//(请注意,在C#中,“不安全”只是意味着编译器不能
//执行完整类型检查),并需要-unsafe编译器标志
不安全的
{
字节*bufferPtr=(字节*)buffer.ToPointer();
//现在可以根据Brad的电子邮件访问字节指针
//(当然,此示例仅为二维单通道图像):
//这是一个1-D阵列,但可以作为3-D阵列进行访问
//大小为[xS,yS,zS]的图像,您可以访问以下位置的图像:
//通过图像[x+y*xS+z*xS*yS]按您的意愿索引[x,y,z],
//所以x是最快的轴,z是最慢的轴。
对于(int k=0;k它应该将图像变成全白色
}
}
}
}
itk.simple.ImportImageFilter importer=新的ImportImageFilter();
进口商。设置尺寸(尺寸);
设置间距(间距);
进口商。SetBufferAsUInt8(缓冲区);
importer.SetOutputPixelType(PixelIDValueEnum.sitkUInt8);
itk.simple.Image output=importer.Execute();
simpletk.Show(输出);
试试:


在内环的第一行中,您已将数组的指定给
像素
变量,而不是元素。在第二行中,您为变量指定了一个新值。

应该是bufferPtr[i+j*size[0]+k*size[0]*size[1]=255。
        itk.simple.Image input = new itk.simple.Image();
        string filepath = "C:\\Users\\pragun\\Desktop\\Selected Data\\stack.mhd";

        ImageFileReader readerx = new ImageFileReader();
        readerx.SetFileName(filepath);
        input = readerx.Execute();

        input = SimpleITK.Cast(input, PixelIDValueEnum.sitkUInt8);

        // calculate the nubmer of pixels
        VectorUInt32 size = input.GetSize();
        VectorDouble spacing = input.GetSpacing();
        Console.WriteLine(size[0]);
        Console.WriteLine(size[1]);
        Console.WriteLine(size[2]);
        IntPtr buffer = new IntPtr(0);
        buffer = input.GetBufferAsUInt8();
        // There are two ways to access the buffer:
        // (1) Access the underlying buffer as a pointer in an "unsafe" block
        // (note that in C# "unsafe" simply means that the compiler can not
        // perform full type checking), and requires the -unsafe compiler flag
     unsafe
        {
            byte* bufferPtr = (byte*)buffer.ToPointer();
            // Now the byte pointer can be accessed as per Brad's email
            // (of course this example is only a 2d single channel image):
            // This is a 1-D array but can be access as a 3-D. Given an
            // image of size [xS,yS,zS], you can access the image at
            // index [x,y,z] as you wish by image[x+y*xS+z*xS*yS],
            // so x is the fastest axis and z is the slowest.
            for (int k = 0; k < size[2]; k++)
            {
                for (int j = 0; j < size[1]; j++)
                {
                    for (int i = 0; i < size[0]; i++)
                    {

                        byte pixel = bufferPtr[i + j * size[1] + k * size[2]*size[1] ] ;
                        pixel = 255; // example -> it should turn the images to all white
                    }
                }
            }
        }




        itk.simple.ImportImageFilter importer = new ImportImageFilter();


        importer.SetSize(size);
        importer.SetSpacing(spacing);
        importer.SetBufferAsUInt8(buffer);
        importer.SetOutputPixelType(PixelIDValueEnum.sitkUInt8);
        itk.simple.Image output = importer.Execute();


        SimpleITK.Show(output);
bufferPtr[i + j * size[1] + k * size[2]*size[1] ] = 255