MCPcopy Create free account
hub / github.com/SimpleITK/SimpleITK / Main

Method Main

Examples/CSharp/ImageGetSetBuffer.cs:31–115  ·  view source on GitHub ↗
(string[] args)

Source from the content-addressed store, hash-verified

29 public class Program
30 {
31 static void Main(string[] args)
32 {
33
34 if (args.Length < 1)
35 {
36 Console.WriteLine("Usage: inputImage outputImage");
37 return;
38 }
39
40 string inputFilename = args[0];
41 string outputFilename = args[1];
42
43 // Read input image
44 SitkImage input = SimpleITK.ReadImage(inputFilename);
45
46 // Cast so we know the the pixel type
47 input = SimpleITK.Cast(input, PixelId.sitkFloat32);
48
49 // calculate the number of pixels
50 VectorUInt32 size = input.GetSize();
51 int len = 1;
52 for (int dim = 0; dim < input.GetDimension(); dim++)
53 {
54 len *= (int)size[dim];
55 }
56 IntPtr buffer = input.GetBufferAsFloat();
57
58 // There are two ways to access the buffer:
59
60 // (1) Access the underlying buffer as a pointer in an "unsafe" block
61 // (note that in C# "unsafe" simply means that the compiler can not
62 // perform full type checking), and requires the -unsafe compiler flag
63 // unsafe {
64 // float* bufferPtr = (float*)buffer.ToPointer();
65
66 // // Now the byte pointer can be accessed as per Brad's email
67 // // (of course this example is only a 2d single channel image):
68 // // This is a 1-D array but can be access as a 3-D. Given an
69 // // image of size [xS,yS,zS], you can access the image at
70 // // index [x,y,z] as you wish by image[x+y*xS+z*xS*yS],
71 // // so x is the fastest axis and z is the slowest.
72 // for (int j = 0; j < size[1]; j++) {
73 // for (int i = 0; i < size[0]; i++) {
74 // float pixel = bufferPtr[i + j*size[1]];
75 // // Do something with pixel here
76 // }
77 // }
78 // }
79
80 // (2) Copy the buffer to a "safe" array (i.e. a fully typed array)
81 // (note that this means memory is duplicated)
82 float[] bufferAsArray = new float[len]; // Allocates new memory the size of input
83 Marshal.Copy(buffer, bufferAsArray, 0, len);
84 double total = 0.0;
85 for (int j = 0; j < size[1]; j++)
86 {
87 for (int i = 0; i < size[0]; i++)
88 {

Callers

nothing calls this directly

Calls 5

GetBufferAsFloatMethod · 0.80
GetWidthMethod · 0.80
GetHeightMethod · 0.80
GetSizeMethod · 0.45
GetDimensionMethod · 0.45

Tested by

no test coverage detected