Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating DICOM File

Tags:

dicom

I have to pick all valid DICOM Files from folder. I can recursively pick all the files from the folder which have *.DCM extension. But any file with *.DCM also picked up and such file is not valid DICOM File.

What is best way.

I thought of reading few byte of the file and validating.

Or

Any Other Method or any other EXEs we have which validates.

Thank you, Harsha

Edit: Solution for the problem: I finally used the dcmftest.exe for verification. Hope I am on right track. -Harsha

like image 709
Harsha Avatar asked Jan 25 '11 11:01

Harsha


People also ask

How do I get DICOM metadata?

To read metadata from a DICOM file, use the dicominfo function. dicominfo returns the information in a MATLAB® structure where every field contains a specific piece of DICOM metadata.

What program opens DICOM files?

DICOM files may also open with IrfanView, Adobe Photoshop, and GIMP. If you're still having trouble opening the file, it might be because it's compressed. You can try renaming it so that it ends in . zip, and then compress it with a free file extractor program, like PeaZip or 7-Zip.

What is DICOM file?

A DICOM image file is an outcome of the Digital Imaging and Communications in Medicine standard. Specifically, image files that are compliant with part 10 of the DICOM standard are generally referred to as “DICOM format files” or simply “DICOM files” and are represented as “.dcm.”[2]

Can DICOM files be emailed?

DICOM images cannot be shared as you would other files. Sharing a batch of snapshots in the JPEG format or Word documents is a fairly simple process. You can either send them as an email attachment or copy them to a pen drive or an external hard disk and hand that over to the desired recipient.


2 Answers

You want to recognize DICOM files, not to validate. There is big difference. Validation means (at least!) that all the tags required for its SOP class are present.

Recognition is easy, as the DICOM file has to contain text DICM at the offset 0x80, so that tags start at the offset 0x84 of file.

Note that sometimes only the serialized dataset is stored (starting with tag group 8 at file offset 0), and these are more difficult to recognize, but are not standard.

EDIT: As an example, consider a RAR archive. It's easy to recognize, because it starts with Rar!. However, to be sure that it's a valid RAR archive, you have to decompress all the files and check their CRCs, and this is something that could be done only by RAR itself (and it's slow).

like image 160
ruslik Avatar answered Oct 01 '22 00:10

ruslik


I know this has been answered already but I had a similar requirement, so I whipped up some extension methods to do exactly that. Works on Files, FileStreams, MemoryStreams and generic Streams. Only reads the specific 4 bytes needed to validate the filetype. Extremely efficient, I was able to run through thousands of files within seconds.

C#

public static class Dicom
{
    public static bool IsDicomFile(this Stream s)
    {
        //Create an empty 4 byte array
        byte[] dba = new byte[4];

        //Seek to 0x80
        s.Seek(128, SeekOrigin.Begin);

        //Read the following 4 dba
        s.Read(dba, 0, 4);

        //Compare to 'DICM'
        return dba.SequenceEqual(new byte[4] {68, 73, 67, 77});
    }

    public static bool IsDicomFile(this MemoryStream ms)
    {
        return ((Stream)ms).IsDicomFile();
    }

    public static bool IsDicomFile(this FileStream fs)
    {
        return ((Stream)fs).IsDicomFile();
    }

    public static bool IsDicomFile(this FileInfo fi)
    {
        return fi.OpenRead().IsDicomFile();
    }
}

VB.NET

<Extension()> _
Public Function IsDicomFile(ByVal s As Stream) As Boolean
    'Create an empty 4 byte array
    Dim dba() As Byte = New Byte(3) {}

    'Seek to 0x80
    s.Seek(128, SeekOrigin.Begin)

    'Read the subsequent 4 bytes
    s.Read(dba, 0, 4)

    'Compare to 'DICM'
    Return dba.SequenceEqual(New Byte(3) {68, 73, 67, 77})
End Function

<Extension()> _
Public Function IsDicomFile(ByVal ms As MemoryStream) As Boolean
    Return DirectCast(ms, Stream).IsDicomFile
End Function

<Extension()> _
Public Function IsDicomFile(ByVal fs As FileStream) As Boolean
    Return DirectCast(fs, Stream).IsDicomFile
End Function

<Extension()> _
Public Function IsDicomFile(ByVal fi As FileInfo) As Boolean
    Return fi.OpenRead().IsDicomFile
End Function
like image 36
Philmatic Avatar answered Oct 01 '22 01:10

Philmatic