rexcardan / Evil-DICOM

A C# DICOM Library
171 stars 98 forks source link

Image corrupted #48

Closed celiamt closed 7 years ago

celiamt commented 7 years ago

Hi! I'm trying to use the library to anonymize a dicom image. In some cases it works fine, but in other cases the final image is corrupted. I mean, the pixels of the image are wrong and also the FileMetaInformationGroupLength tag of final image has changed. This also happen when I dont anonymize the image, I just read and write the image in a new file.

This is my code: string dir = @"C:\Users\Desktop\CT.dcm"; var dcmBytes = System.IO.File.ReadAllBytes(@dir); try {

            var dcm = EvilDICOM.Core.DICOMObject.Read(dcmBytes);
            var refName = new EvilDICOM.Core.Element.PersonName
            {
                FirstName = "",
                Tag = EvilDICOM.Core.Helpers.TagHelper.PATIENT_NAME
            };

            dcm.ReplaceOrAdd(refName);
            dcm.Write(@"C:\Users\Desktop\CT2.dcm");
            // return dcm.GetBytes();
        }
        catch (Exception ex)
        {
            throw new Exception("EXCEPTION: " + ex.Message);
        }

Thank you in advance!

rexcardan commented 7 years ago

Anonymization is built into Evil DICOM (http://rexcardan.github.io/Evil-DICOM/articles/anonymization.html). I am not sure what is happening in your image bytes. Do you have an anonymized file you could send so I could debug myself. Its almost impossible to just look at your code and figure it out.

celiamt commented 7 years ago

Thanks for your answer!

I send you the image in which I have the problem. https://www.dropbox.com/s/s5ase23jl9908jm/3DSlice1.dcm?dl=0

I also send you an screenshot with the original image and the final image (the corrupted image). https://www.dropbox.com/s/12liy3gbw7dkb4d/Image_corrupted.PNG?dl=0

I dont know what is happening with the pixel data, but I have observed that the FileMetaInformationGroupLength tag changes.

Thank you in advance!

rexcardan commented 7 years ago

I did notice one problem. These are compressed images. When EvilDICOM writes out, for right or wrong, it encodes the file assuming no compression. You can overcome this by setting the TransferSyntaxUID manually in the DICOMWriteSettings:

var writeSettings = DICOMWriteSettings.Default();
//Set writing syntax to current syntax
writeSettings.TransferSyntax = TransferSyntaxHelper.GetSyntax(dcm.GetSelector().TransferSyntaxUID.Data);
dcm.Write(@"C:\Users\REXNFX\Desktop\3DSlice1mod2.dcm", writeSettings);

What is weird is that it still shows up as scrambled in MicroDICOM, but shows up fine in other DICOM viewers. I am not sure why that is. I have compared the data written to original and it is the same except the patient name. Here is my verification code:

            var path = @"C:\Users\REXNFX\Desktop\3DSlice1.dcm";
            var dcm = DICOMObject.Read(path);
            //Keep original before we start modding
            var copy = DICOMObject.Read(path);

            var refName = new EvilDICOM.Core.Element.PersonName
            {
                Data = "",
                Tag = EvilDICOM.Core.Helpers.TagHelper.PATIENT_NAME
            };

            dcm.ReplaceOrAdd(refName);

            //This is compressed to JPEG-LS. We need to set manually on write
            var writeSettings = DICOMWriteSettings.Default();
            //Set writing syntax to current syntax
            writeSettings.TransferSyntax = TransferSyntaxHelper.GetSyntax(dcm.GetSelector().TransferSyntaxUID.Data);

            dcm.Write(@"C:\Users\REXNFX\Desktop\3DSlice1mod2.dcm", writeSettings);
            dcm = DICOMObject.Read(@"C:\Users\REXNFX\Desktop\3DSlice1mod2.dcm");

            //CHECK ELEMENT COUNT
            AssertAreEqual(dcm.AllElements.Count, copy.AllElements.Count);
            //CHECK ELEMENT DATA
            for (int i = 0; i < dcm.AllElements.Count; i++)
            {
                if (!dcm.AllElements[i].Tag.CompleteID.EndsWith("0000")) //Exclude headers
                    AssertAreEqual(dcm.AllElements[i].DData_, copy.AllElements[i].DData_, dcm.AllElements[i].Tag.CompleteID);
            }

            //CHECK PIXELS
            var origPixels = copy.GetSelector().PixelData.Data_;
            var writePixels = dcm.GetSelector().PixelData.Data_;
            for (int i = 0; i < origPixels.Count; i++)
            {
                if (origPixels[i] != writePixels[i])
                {
                    Console.WriteLine(i);
                }
            }
            Console.Read();
rexcardan commented 7 years ago

My AssertAreEqualCode looks like this:

        private static void AssertAreEqual<T>(T val1, T val2, string message = "")
        {
            if (typeof(T) == typeof(DICOMObject))
            {
                var dynVal1 = val1 as dynamic;
                var dynVal2 = val2 as dynamic;

                for (int i = 0; i < dynVal1.AllElements.Count; i++)
                {
                    if (!dynVal1.AllElements[i].Tag.CompleteID.EndsWith("0000")) //Exclude headers
                        AssertAreEqual(dynVal1.AllElements[i].DData_, dynVal2.AllElements[i].DData_, dynVal2.AllElements[i].Tag.CompleteID);
                }
            }
            else if (typeof(T) == typeof(ICollection) || typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(List<>))
            {
                var dynVal1 = val1 as dynamic;
                var dynVal2 = val2 as dynamic;

                for (int i = 0; i < dynVal1.Count; i++)
                {
                    if (dynVal1[0] != null && dynVal1[0].GetType() == typeof(DICOMObject))
                    {
                        var dcm1 = (DICOMObject)dynVal1[i];
                        var dcm2 = (DICOMObject)dynVal2[i];
                        AssertAreEqual(dcm1, dcm2, message);
                    }
                    else if (dynVal1[i]!=null&& !dynVal1[i].Equals(dynVal2[i])) { Console.WriteLine($"{message} item {i} not equal => {dynVal1[i]} != {dynVal2[i]}"); }
                }
            }
            else if (!val1.Equals(val2))
            {
                Console.WriteLine($"{message} not eqaul => {val1} != {val2}");
            }
        }
celiamt commented 7 years ago

Thank you!! I will try by setting the TransferSyntaxUID when write the image! I hope it works!

celiamt commented 7 years ago

Thanks! I have implemented the code by setting the TransferSyntaxUID manually and it works fine!