Метод Getbuffer в библиотеках SimpleITK для чтения изображения и вывода другого

Здесь,

В настоящее время я работаю над проектом C#, и мне нужно создать фрагмент с использованием библиотек SimpleITK, в котором моя матрица изображений ArrayIn будет сравниваться с числом DoubleVar.

Затем, если для каждого элемента значение ArrayIn больше значения DoubleVar, соответствующий элемент моего выходного изображения ArrayOut будет равен 1. Если нет, этот элемент будет равен 0. Матрица ArrayOut представляет собой двоичное изображение с единицами и нули. Размер AArrayOut будет таким же, как и входной. Мне нужно использовать функцию Getbufferasint16(), но я не могу этого сделать.

Следующий код является частью намерений, но безрезультатно

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

person user3213352    schedule 20.01.2014    source источник


Ответы (1)


Пожалуйста, найдите этот новый пример того, как получить доступ к буферу из SimpleITK в C#: http://www.itk.org/SimpleITKDoxygen/html/ImageGetBuffer_8cs-example.html

Вам нужно использовать Marshal для безопасного взаимодействия неуправляемого кода с данными 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;
    }
  }
person blowekamp    schedule 28.01.2014