Code Monkey home page Code Monkey logo

Comments (6)

rexcardan avatar rexcardan commented on June 4, 2024

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.

from evil-dicom.

celiamt avatar celiamt commented on June 4, 2024

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!

from evil-dicom.

rexcardan avatar rexcardan commented on June 4, 2024

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();

from evil-dicom.

rexcardan avatar rexcardan commented on June 4, 2024

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}");
            }
        }

from evil-dicom.

celiamt avatar celiamt commented on June 4, 2024

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

from evil-dicom.

celiamt avatar celiamt commented on June 4, 2024

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

from evil-dicom.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.