public Bitmap konwertujObraz(Image obraz1, int przekroj = 0) { uint r = obraz1.GetWidth(); // VectorUInt32 w = new VectorUInt32(new[] { r, 512, 4 + 1 }); VectorInt32 start = new VectorInt32(new[] { 0, 0, 0 }); VectorInt32 size1 = new VectorInt32(new[] { 512, 512, 1 }); obraz1 = WybierzPrzekroj(obraz1, przekroj); IntensityWindowingImageFilter normalize = new IntensityWindowingImageFilter(); normalize.SetOutputMinimum(0); normalize.SetOutputMaximum(255); obraz1 = normalize.Execute(obraz1); PixelIDValueEnum u = PixelIDValueEnum.sitkFloat32; int len = 1; Image input = SimpleITK.Cast(obraz1, u); VectorUInt32 size = input.GetSize(); for (int dim = 0; dim < input.GetDimension(); dim++) { len *= (int)size[dim]; } IntPtr buffer = input.GetBufferAsFloat(); float bufferPtr = (float)buffer.ToInt32(); float[] bufferAsArray = new float[len]; float[,] newData = new float[size[0], size[1]]; Marshal.Copy(buffer, bufferAsArray, 0, len); obrazBitmap = new Bitmap(Convert.ToInt32(size[0]), Convert.ToInt32(size[1])); for (int j = 0; j < size[1]; j++) { for (int i = 0; i < size[0]; i++) { var bur = bufferAsArray[j * size[1] + i]; System.Drawing.Color newColor = System.Drawing.Color.FromArgb((int)bur, 0, 0, 0); obrazBitmap.SetPixel(j, i, newColor); } } Color s = obrazBitmap.GetPixel(34, 56); return(obrazBitmap); }
static void Main(string[] args) { if (args.Length < 1) { Console.WriteLine("Usage: SimpleGaussian <input>"); return; } // Read input image itk.simple.Image input = SimpleITK.ReadImage(args[0]); // Cast to we know the the pixel type input = SimpleITK.Cast(input, PixelId.sitkFloat32); // calculate the number 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(); // Note: C# also has a GetConstBufferAs... methods which do not // implicitly call MakeUnique. // 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; } } Console.WriteLine("Pixel value total: {0}", total); }