[Insight-users] convert DICOM to other fomat using ManagedITK
黃琮閔
s94213003 at gmail.com
Fri Jul 25 13:39:13 EDT 2008
hi,
I want to convert DICOM to Bitmap, I have read ManagedITK example,
most of my DICOM data type is signed short.
I tried use itkRescaleIntensityImageFilter to conver Image
But it doesn't work well, can anyone give me some suggestion?
here is my read code :
itkImageFileReader_ISS3 reader =itkImageFileReader_ISS3.New();
itkGDCMImageIO gdcmImageIO = itkGDCMImageIO.New();
reader.FileName = filename;
reader.SetImageIO(gdcmImageIO);
reader.Update();
itkImage_SS3 imageSS3 = itkImage_SS3.New();
itkImage_UC3 imageUC3 = itkImage_UC3.New();
reader.GetOutput(imageSS3);
itkRescaleIntensityImageFilter_IUC3ISS3 ss2uc =
itkRescaleIntensityImageFilter_IUC3ISS3.New();
ss2uc.SetInput(imageSS3);
ss2uc.GetOutput(imageUC3);
ss2uc.Update();
Bitmap show = this.ConvertItkImageToBitmap(imageUC3);
Bitmap convert it's form ManagedITK example
private Bitmap ConvertItkImageToBitmap(itkImageBase image)
{
// Check image is 2D
/*
if (image.Dimension != 2)
{
String message = String.Empty;
message += "We can only display images with 2 dimensions.";
message += "The given image has {0} dimensions!";
throw new NotSupportedException(String.Format(message,
image.Dimension));
}
*/
// Check the pixel type is scalar
if (!image.PixelType.IsScalar)
{
String message = "We can only display images with
scalar pixels.";
message += "The given image has {0} pixel type!";
throw new NotSupportedException(String.Format(message,
image.PixelType));
}
// Check the pixel type is unsigned char
if (image.PixelType.TypeAsEnum != itkPixelTypeEnum.UnsignedChar)
{
String message = String.Empty;
message += "We can only display images with
UnsignedChar pixels.";
message += "The given image has {0} pixel type!";
throw new NotSupportedException(String.Format(message,
image.PixelType));
}
// The result will be a System.Drawing.Bitmap
Bitmap bitmap;
// Set pixel format as 8-bit grayscale
PixelFormat format = PixelFormat.Format8bppIndexed;
// Check if the stride is the same as the width
if (image.Size[0] % 4 == 0)
{
// Width = Stride: simply use the Bitmap constructor
bitmap = new Bitmap(image.Size[0], // Width
image.Size[1], // Height
image.Size[0], // Stride
format, // PixelFormat
image.Buffer // Buffer
);
}
else
{
unsafe
{
// Width != Stride: copy data from buffer to bitmap
int width = image.Size[0];
int height = image.Size[1];
byte* buffer = (byte*)image.Buffer.ToPointer();
// Compute the stride
int stride = width;
if (width % 4 != 0)
stride = ((width / 4) * 4 + 4);
bitmap = new Bitmap(width, height, format);
Rectangle rect = new Rectangle(0, 0, width, height);
BitmapData bitmapData = bitmap.LockBits(rect,
ImageLockMode.WriteOnly, format);
for (int j = 0; j < height; j++)
// Row
{
byte* row = (byte*)bitmapData.Scan0 + (j * stride);
for (int i = 0; i < width; i++)
// Column
row[i] = buffer[(j * width) + i];
}
bitmap.UnlockBits(bitmapData);
}// end unsafe
}// end if (Width == Stride)
// Set a color palette
bitmap.Palette = this.CreateGrayscalePalette(format, 256);
// Return the resulting bitmap
return bitmap;
}// end ConvertItkImageToBitmap()
ColorPalette CreateGrayscalePalette(PixelFormat format, int
numberOfEntries)
{
ColorPalette palette; // The Palette we are stealing
Bitmap bitmap; // The source of the stolen palette
// Make a new Bitmap object to steal its Palette
bitmap = new Bitmap(1, 1, format);
palette = bitmap.Palette; // Grab the palette
bitmap.Dispose(); // Cleanup the source Bitmap
// Populate the palette
for (int i = 0; i < numberOfEntries; i++)
palette.Entries[i] = Color.FromArgb(i, i, i);
// Return the palette
return palette;
}
thanks for your help
Huang
More information about the Insight-users
mailing list