Re: [Plastimatch] plastimatch crashed with mha files

232 views
Skip to first unread message

Paolo Zaffino

unread,
Feb 24, 2013, 4:07:22 AM2/24/13
to plast...@googlegroups.com, jim.c...@gmail.com

Hi,
The problem seems to be the img1.mha, not the img0.mha
Baybe it should be coded into an unrecognized data type.
Could you show the img1.mha header?

Paolo


Inviato da Yahoo! Mail su Android



From: Jim Chang <jim.c...@gmail.com>;
To: <plast...@googlegroups.com>;
Subject: [Plastimatch] plastimatch crashed with mha files
Sent: Sun, Feb 24, 2013 12:56:45 AM

I tried to run plastimatch to register two images but it immediately crashed (see the attached output).
It appears that something wrong with the mha creation or my ITK (v2.30.1)
 
What I did is the following:
 
1. use matlab code to create a raw file. The code is:


files=dir('*.dcm');
num_f=length(files);
img_raw=zeros(512,512,length(files));
for i=1:length(files)
  img=dicomread(files(i).name);
  img=img.';
  img_raw(:,:,i)=img;
end

fid=fopen('img0.raw','wb');
 fwrite(fid,img_raw,'float');
fclose(fid);

2. use the raw_to_mha to convert the raw file to a mha file:

raw_to_mha img0.raw img0.mha 3 "0.95 0.95 2.5"

3. Follow to run registration using the command

plastimatch register command_file.txt

with the example command_file.txt:

# command_file.txt
[GLOBAL]
fixed=img1.mha
moving=img0.mha
img_out=warped_img0.mha
xform_out=bspline_coefficients.txt

[STAGE]
xform=bspline
impl=plastimatch
threading=openmp
max_its=30
regularization_lambda=0.005
grid_spac=100 100 100
res=4 4 2

Would greatly appreciate it if anybody can help.

Jim

 
 
===========================================
 
Loading fixed image: img1.mha
ElementType required and not defined.
MetaObject: Read: MET_Read Failed
MetaImage: M_Read: Error parsing file
MetaImage: Read: Cannot parse file
ITK exception.
itk::ExceptionObject (01C3F394)
Location: "void __thiscall itk::MetaImageIO::ReadImageInformation(void)"
File: ..\..\..\InsightToolkit-3.20.1\Code\IO\itkMetaImageIO.cxx
Line: 97
Description: itk::ERROR: MetaImageIO(00540348): File cannot be read: img1.mha
for reading.
Reason: Invalid argument

Error: unhandled file type for loading image (0) phase5.mha
Press any key to continue . . .

--
You received this message because you are subscribed to the Google Groups "Plastimatch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to plastimatch...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Paolo Zaffino

unread,
Feb 24, 2013, 6:56:40 AM2/24/13
to plast...@googlegroups.com, jim.c...@gmail.com, p.za...@yahoo.it

Sorry for the mistakes but the smartphone changes what I type.
"Maybe it could" is what I wanted to type.

Inviato da Yahoo! Mail su Android



From: Paolo Zaffino <p.za...@yahoo.it>;
To: plast...@googlegroups.com <plast...@googlegroups.com>; jim.c...@gmail.com <jim.c...@gmail.com>;
Subject: Re: [Plastimatch] plastimatch crashed with mha files
Sent: Sun, Feb 24, 2013 9:07:22 AM

Jim Chang

unread,
Feb 24, 2013, 5:09:44 PM2/24/13
to plast...@googlegroups.com, jim.c...@gmail.com, p.za...@yahoo.it
The header of both mha files looks like:
 

ObjectType = Image

NDims = 3

BinaryData = True

BinaryDataByteOrderMSB = False

TransformMatrix = 1 0 0 0 1 0 0 0 1

Offset = 0 0 0

ElementSpacing = 0.97656 0.97656 2.5

DimSize = 3

AnatomicalOrientation = RPI

ElementType = MET_UCHAR

ElementDataFile = LOCAL

Marta Peroni

unread,
Feb 24, 2013, 5:16:52 PM2/24/13
to plast...@googlegroups.com, jim.c...@gmail.com
Hi Jim...
I think your DimSize is wrong...it should contain the number of pixels in each dimension, something like:
DimSize = 512 512 58

Hope this helps!
Marta
*******************************************************************
Marta Peroni, PhD

contacts:
mobile: +393488202136 (IT)

********************************************************************

Jim Chang

unread,
Feb 24, 2013, 5:30:53 PM2/24/13
to Marta Peroni, plast...@googlegroups.com
Hi Marta,
 
You are right! I re-generated mha files from the raw files using the command:
 
raw_to_mha img0.raw img0.mha "512 512 120" "0.95 0.95 2.5"
 
instead of
 
raw_to_mha img0.raw img0.mha 3 "0.95 0.95 2.5"
 
That does the trick! Thanks very much.
 
Jim

Jim Chang

unread,
Feb 24, 2013, 5:44:13 PM2/24/13
to Marta Peroni, plast...@googlegroups.com
Ooops. Apparently I replied too quickly. Although the dimensions are correct, the raw data type got changed from float to unsigned char. So the mha image is different from the raw image. The raw_to_mha command does not specify how to keep the format of the original raw file. Is there anything I missed?
 
Thanks,
Jim 

Marta Peroni

unread,
Feb 24, 2013, 5:50:59 PM2/24/13
to Jim Chang, plast...@googlegroups.com
Hi Jim!
Another thing you can use to convert from dcm to mha, given you are already using matlab, is the function writemha.m (you can find it in plastimatch_directory/extra/matlab). You can directly pass the matrix of data, element spacing, data type, offset to the function and it will write the corresponding mha.
This is what I would do to get it working :)
Thanks
Marta

Paolo Zaffino

unread,
Feb 24, 2013, 6:14:21 PM2/24/13
to plast...@googlegroups.com, Jim Chang
Hi all,
anyway you have another two possibilities:
1) use plastimatch convert to convert the dicom files into an mha file
2) use directly the dicom folder in the parameters file.

Paolo



Da: Marta Peroni <m.pe...@gmail.com>
A: Jim Chang <jim.c...@gmail.com>
Cc: plast...@googlegroups.com
Inviato: Domenica 24 Febbraio 2013 23:50
Oggetto: Re: [Plastimatch] plastimatch crashed with mha files

Jim Chang

unread,
Feb 24, 2013, 6:37:09 PM2/24/13
to Paolo Zaffino, plast...@googlegroups.com
Thanks Paolo.
 
I followed Marta's suggestion using matlab writemha and it worked! Thanks Marta.
 
Jim

Paolo Zaffino

unread,
Feb 24, 2013, 6:57:36 PM2/24/13
to plast...@googlegroups.com
Hi Jim,
I'm glad to hear this.

Paolo


Da: Jim Chang <jim.c...@gmail.com>
A: Paolo Zaffino <p.za...@yahoo.it>
Cc: "plast...@googlegroups.com" <plast...@googlegroups.com>
Inviato: Lunedì 25 Febbraio 2013 0:37

Jim Chang

unread,
Feb 25, 2013, 4:21:17 PM2/25/13
to plast...@googlegroups.com, Paolo Zaffino
Hi Paolo & Marta,
 
I have a slightly different question regarding creating an mha image from a series of dicom images. The mha image I have  looks wrong. Please refer to the attached figure on saggittal and coronal views. Obviously something goes wrong. This does not always happens, but does occur.
 
Anybody knows what can be done to have correct images? Many thanks!
 
Jim
bad_img.png

Paolo Zaffino

unread,
Feb 25, 2013, 4:41:43 PM2/25/13
to plast...@googlegroups.com
Hi Jim,
did you generate this image using matlab?
If yes please try this plastimatch command:
plastimatch convert --input=dicom_folder --output-img=img.mha

In my opinion the problem is due to a "flip" of the 2D image.
If you generated it via matlab maybe the 2D matrix (the dicom file) has been rotated.

Hope this helps.
Paolo



Da: Jim Chang <jim.c...@gmail.com>
A: plast...@googlegroups.com
Cc: Paolo Zaffino <p.za...@yahoo.it>
Inviato: Lunedì 25 Febbraio 2013 22:21

Hi Jim!
Another thing you can use to convert from dcm to mha, given you are already using matlab, is the function writemha.m (you can find it in plastimatch_directory/extra/ matlab). You can directly pass the matrix of data, element spacing, data type, offset to the function and it will write the corresponding mha.



--
****************************** ****************************** *******

Marta Peroni, PhD

contacts:
mobile: +393488202136 (IT)

****************************** ****************************** ********



--
****************************** ****************************** *******
Marta Peroni, PhD

contacts:
mobile: +393488202136 (IT)

****************************** ****************************** ********
--
You received this message because you are subscribed to the Google Groups "Plastimatch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to plastimatch...@ googlegroups.com.
For more options, visit https://groups.google.com/ groups/opt_out.
 
 
--
You received this message because you are subscribed to the Google Groups "Plastimatch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to plastimatch...@ googlegroups.com.
For more options, visit https://groups.google.com/ groups/opt_out.
 
 

Marta Peroni

unread,
Feb 25, 2013, 4:49:29 PM2/25/13
to plast...@googlegroups.com
Hi!
it could be due to 2 things:
1) each 2D matrix image shall be transposed in matlab (as paolo suggested)
2) did you correctly ordered the CC axis according to Image Position Patient?

Marta
--
*******************************************************************

Jim Chang

unread,
Feb 25, 2013, 5:44:49 PM2/25/13
to plast...@googlegroups.com
Hi Paolo & Marta,
 
I used plastimatch command instead of using matlab code and got the right mha image.
You both are right that one/some of the dicom images are flipped. Is there an easy way
to detect it/them?
 
At any rate, I am good at this point, but may come back for more questions. :)
 
Thanks a lot, guys!
Jim

Marta Peroni

unread,
Feb 25, 2013, 5:48:29 PM2/25/13
to plast...@googlegroups.com
Well, the easiest solution is to use plastimatch convert and/or directly dicoms in the plastimatch parameter file.
If you do want to use matlab, than for the flip in CC direction, simply re-order (sort) the Image Position Patient and pile your images accordingly. For the 2D image rotation, it depends from the way matlab thinks (i.e. columwise) :)

Shoot a question whenever you fill like :)
marta

Paolo Zaffino

unread,
Feb 25, 2013, 6:06:43 PM2/25/13
to plast...@googlegroups.com
This is a good news!

If you didn't filp/transpose some 2D matrix during the "building" of the 3D matrix I think that you should look into the dicom header, in order to check if the image orentation  is always the same (the positive direction along which the voxel are counted) and in case "correct" it.

Anyway if you are interested in mha manipulation there is a python module that execute the same thing of the matlab files (read/write mha files).
The file is extra/python/mha.py

Have a nice day.
Paolo

Inviato: Lunedì 25 Febbraio 2013 23:44
Reply all
Reply to author
Forward
0 new messages