Created
July 3, 2018 19:29
-
-
Save dg1an3/03692c61c6afce1afbc1b22083a3182c to your computer and use it in GitHub Desktop.
Quick and dirty MPR generation from a 3D volume array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum Orientation | |
{ | |
Transverse, | |
Sagittal, | |
Coronal | |
} | |
public static class MprGenerator<TPixel> | |
{ | |
// generates an MPR from a volume given an orientation | |
// and a slice position in pixel distance | |
public static TPixel[,] | |
GenerateMpr(TPixel[,,] inputVolume, | |
Orientation orientation, | |
float slicePosition) | |
{ | |
var mprSize = | |
CalculateSize(inputVolume, orientation); | |
return | |
UpdatePixelsFromVolume(inputVolume, | |
orientation, | |
mprSize, | |
(int) Math.Round(slicePosition)); | |
} | |
// calculates the size of the MPR, given orientation and volume dimensions | |
static Tuple<int,int> | |
CalculateSize(TPixel[,,] uiv, | |
Orientation orientation) | |
{ | |
switch (orientation) | |
{ | |
case Orientation.Transverse: | |
return Tuple.Create(uiv.GetLength(0), uiv.GetLength(1)); | |
case Orientation.Coronal: | |
return Tuple.Create(uiv.GetLength(0), uiv.GetLength(2)); | |
case Orientation.Sagittal: | |
return Tuple.Create(uiv.GetLength(1), uiv.GetLength(2)); | |
} | |
throw new ArgumentException("Invalid orientation"); | |
} | |
// allocates and calculates the actual MPR slice | |
static TPixel[,] | |
UpdatePixelsFromVolume(TPixel[,,] uiv, | |
Orientation orientation, | |
Tuple<int, int> mprSize, | |
int slice) | |
{ | |
var pixels = new TPixel[mprSize.Item1, mprSize.Item2]; | |
// check for bounds based on the orientation | |
switch (orientation) | |
{ | |
case Orientation.Transverse: | |
slice += uiv.GetLength(2) / 2; | |
if (slice < 0 || slice >= uiv.GetLength(2)) | |
return null; | |
break; | |
case Orientation.Coronal: | |
slice += uiv.GetLength(1) / 2; | |
if (slice < 0 || slice >= uiv.GetLength(1)) | |
return null; | |
break; | |
case Orientation.Sagittal: | |
slice += uiv.GetLength(0) / 2; | |
if (slice < 0 || slice >= uiv.GetLength(0)) | |
return null; | |
break; | |
} | |
// now update based on the orientation | |
switch (orientation) | |
{ | |
case Orientation.Transverse: | |
for (int y = 0; y < pixels.GetLength(0); y++) | |
for (int x = 0; x < pixels.GetLength(1); x++) | |
pixels[y, x] = uiv[slice, y, x]; | |
break; | |
case Orientation.Coronal: | |
for (int y = 0; y < pixels.GetLength(0); y++) | |
for (int x = 0; x < pixels.GetLength(1); x++) | |
pixels[y, x] = uiv[y, slice, x]; | |
break; | |
case Orientation.Sagittal: | |
for (int y = 0; y < pixels.GetLength(0); y++) | |
for (int x = 0; x < pixels.GetLength(1); x++) | |
pixels[y, x] = uiv[y, x, slice]; | |
break; | |
} | |
return pixels; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment