I'm using SimpleITK to read a DICOM, and save a particular slice as a PNG file. I can write back a new DICOM file to disk fine, and it looks as expected. But whenever I try to save it in any other format, it's greatly corrupted. By that I mean it looks nothing like the input, it's completely garbled.
Here is the code:
var imageReader = new ImageFileReader();
imageReader.SetOutputPixelType(PixelIDValueEnum.sitkUInt8);
var dicomFileNames = ImageSeriesReader.GetGDCMSeriesFileNames(@"D:\Study");
imageReader.SetFileName(dicomFileNames[255]);
var image = imageReader.Execute();
var fileWriter = new ImageFileWriter();
fileWriter.SetFileName("slice.png");
fileWriter.Execute(image);
Getting the image's buffer and using it to create a BitMap suffers the same problem. Reading the DICOM series (my eventual goal) instead of one file, and using the 3D volume and extracting a slice in that manner also has the same issue.
What am I missing?
EDIT: Using PixelIDValueEnum.sitkUInt16
greatly improves the output of ImageFileWriter
, although the contrast is off and loses some detail. I still cannot convert the buffer to a BitMap and save that as a PNG, this code still creates corrupted data:
var size = image.GetSize();
var length = (int)(size[0] * size[1]) * 2;
var buffer = image.GetBufferAsUInt16();
var rgbValues = new byte[length];
Marshal.Copy(buffer, rgbValues, 0, length);
var newBitmap = new Bitmap((int)image.GetWidth(), (int)image.GetHeight(), (int)image.GetWidth(), PixelFormat.Format16bppArgb1555, buffer);
newBitmap.Save(@"C:\test.png", ImageFormat.Png);
I have tried every PixelFormat.16bpp*
value without success, some data must be getting lost, because the output of the ImageFileWriter is more than 50% larger than when I save the bitmap.
Here is the bad BitMap:
Most medical images are 16 bit deep ( short
or unsigned short
), not 8 bit ( sitkUInt8
). Try a few other pixel formats. If that does not help, attach an extracted PNG slice - that will allow more/better advice.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.