I am using the DCMTK (3.6.0) library to get the value of the (0020,0013) tag which is the image number aka the slice number of the serie.
I use the following in a batch script
for /f "tokens=2 delims=[]" %%a in ('@echo. ^|c:\Libs\dcmtk-3.6.0\bin\dcmdump +P "0020,0013" %%i') do (set img_no=%%a)
It usually goes well, but sometimes this value is always set to '0' to the whole serie.
I tried to dump that with this command
C:\Libs\dcmtk-3.6.0\bin>dcmdump +P "0020,0013" PathToInvalideDICOM\img.dcm
(0020,0013) IS [0] # 2, 1 InstanceNumber
(0020,0013) IS [4] # 2, 1 InstanceNumber
(0020,0013) IS [0] # 2, 1 InstanceNumber
C:\Libs\dcmtk-3.6.0\bin>dcmdump +P "0020,0013" PathToCorrectDICOM\img.dcm
(0020,0013) IS [0] # 2, 1 InstanceNumber
(0020,0013) IS [5] # 2, 1 InstanceNumber
As we can see, sometimes the value to get (which is not the '0') is the last. In this case, everything is good. But in some particular cases, the correct value is stored between two '0'.
I also tried with a different dumper (DCM4CHE 2.0.23) and it give me the same result.
I want to know why this is happening. And more than that, how to get the correct value?
Is there a way, in a batch file, to eliminate the 0 until the correct number?
By default, my command line cited above take the last field... I think.
Most likely, the extra InstanceNumber tags are part of a dicom sequence and may not be the ones you want.
If you run dcmdump like this:
dcmdump +p +P "0020,0013" PathToInvalideDICOM\img.dcm
You should get the fully scoped output like:
(0008,1111).(0020,0013) IS [0] # 2, 1 InstanceNumber
(0020,0013) IS [1] # 2, 1 InstanceNumber
(5200,9230).(2005,140f).(0020,0013) IS [1] # 2, 1 InstanceNumber
Then it should be a matter of just validating exactly which InstanceNumber you really want (probably the global one)
In a batch file, you probably want to do a findstr to pick out the line. You might want to try:
dcmdump +p +P "0020,0013" PathToInvalideDICOM\img.dcm | findstr /b (0020
To pull out the "global" InstanceNumber tag. Then you should be able to parse the tokens as you have.