C# 在SimpleTk库中使用Getbuffer方法读取图像并输出另一个图像

C# 在SimpleTk库中使用Getbuffer方法读取图像并输出另一个图像,c#,pointers,image-processing,itk,simpleitk,C#,Pointers,Image Processing,Itk,Simpleitk,这里那里 我目前正在从事一个C#项目,我需要使用SimpleTk库创建一个代码段,其中我的图像矩阵ArrayIn将与一个数字DoubleVar进行比较 然后,如果对于每个元素,ArrayIn的值大于DoubleVar值,则我的输出图像ArrayOut的相应元素将为1。否则,该元素将为0。矩阵ArrayOut是一个带有1和0的二进制图像。阵列的大小将与输入的大小相同。我需要使用函数Getbufferasint16(),但我不能这样做 下面的代码是intents的一部分,但没有结果 uint8_t

这里那里

我目前正在从事一个C#项目,我需要使用
SimpleTk
库创建一个代码段,其中我的图像矩阵
ArrayIn
将与一个数字
DoubleVar
进行比较

然后,如果对于每个元素,
ArrayIn
的值大于DoubleVar值,则我的输出图像
ArrayOut
的相应元素将为1。否则,该元素将为0。矩阵
ArrayOut
是一个带有1和0的二进制图像。阵列的大小将与输入的大小相同。我需要使用函数
Getbufferasint16()
,但我不能这样做

下面的代码是intents的一部分,但没有结果

uint8_t *buffer = (my image) ->GetBufferAsUInt16();
buffer[c + numComponents*(x+ xSize* (y*+ySize*z))];

请查找这个关于如何在C#中从SimpleTk访问缓冲区的新示例:

您需要使用该类将非托管代码与C#数据安全地连接起来,或者使用不安全的块:

  input = SimpleITK.Cast(input, PixelId.sitkFloat32);
  // calculate the nubmer of pixels
  VectorUInt32 size = input.GetSize();
  int len = 1;
  for (int dim = 0; dim < input.GetDimension(); dim++) {
    len *= (int)size[dim];
  }
  IntPtr buffer = input.GetBufferAsFloat();
  // 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 {
    float* bufferPtr = (float*)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 j = 0; j < size[1]; j++) {
      for (int i = 0; i < size[0]; i++) {
        float pixel = bufferPtr[i + j*size[1]];
        // Do something with pixel here
      }
    }
  }
  // (2) Copy the buffer to a "safe" array (i.e. a fully typed array)
  // (note that this means memory is duplicated)
  float[] bufferAsArray = new float[len]; // Allocates new memory the size of input
  Marshal.Copy(buffer, bufferAsArray, 0, len);
  double total = 0.0;
  for (int j = 0; j < size[1]; j++) {
    for (int i = 0; i < size[0]; i++) {
      float pixel = bufferAsArray[i + j*size[1]];
      total += pixel;
    }
  }
input=simpletk.Cast(输入,PixelId.sitkFloat32);
//计算像素数
VectorUInt32 size=input.GetSize();
int len=1;
对于(int dim=0;dim