File Exchange

image thumbnail

nrrdWriter(filename​, matrix, pixelspacing, origin, encoding)

version 1.2.0.0 by mdcacio
This function writes imagery and metadata to .nrrd or .nhdr medical imaging format files.

8 Downloads

Updated 03 Dec 2014

View License

This is a complement of the function nrrdread, "NRRD Format File Reader by Jeff Mather". It does exactly the opposite: it's a NRRD files writer.
- With filename it's possible to set the path, the filename and the format (nrrd or nhdr+data). For example, a filename like '/home/mariodiaz/testfile.nhdr' will create the file at the folder /home/mariodiaz, its filename will be testfile, and it will create the header (.nhdr) appart from the data (.raw, gzip,... depending on the encoding).
- matrix is the data to put into the file. It will also set some fields at the header like dimension, space dimensions, size, type, etc.
- Pixel spacing is equivalent to the output file voxel width and should be defined as [psx, psy, psz]
- Origin should be set to [0,0,0] by default (otherwise coordinates will be shifted)
- Encoding sets data encoding. 'raw', 'ascii' or 'gzip' available.

Comments and Ratings (11)

mdcacio

Hi all,

Thanks for the comments and contributions.

I'm afraid I haven't been able to check the code since I uploaded it, and I didn't encountered the issues mentioned in the comments back then - maybe I just used the function in a bounded way. Happily, I see some members of the Mathworks community managed to solve the issues. As I'm not coding / using Matlab from then I may not be able to solve any question, but I hope you will keep posting for the good of the community.

Thanks guys for the feedback.
All the best,
M

thatguy14

For those that were maybe having issues with it not doing proper cleanup (getting an error that the file did not exist or permission denied) this is because the function "writeData" was trying to delete a file before it had closed it. To change this go to that function, under the case {'gzip'} replace the code after the comment:

% Finally, we put this info into our nrrd file (fidIn)
fidTmpRaw = fopen(tmpFile, 'rb');
tmp = fread(fidTmpRaw, inf, [datatype '=>' datatype]);
cleaner = onCleanup(@() fclose(fidTmpRaw));
clearn2 = onCleanup(@() delete(tmpFile)); %DELETE THE FILE AFTER THE FUNCTION IS DONE AND FILE CLOSED
ok = fwrite (fidIn, tmp, datatype);

delete (tmpBase);
% delete (tmpFile); %COMMENT THIS OUT, THIS WAS CAUSING THE ISSUES (cleanup happens after function death)

Naim

Quan Chen

I think I found out why many of you got "unexpected end of input stream when attempting to gunzip the file"
In writeData function, when we reading from the gzipped file:
fidTmpRaw = fopen(tmpFile, 'rb');
tmp = fread(fidTmpRaw, inf, [datatype '=>' datatype]);
fclose(fidTmpRaw);
ok = fwrite (fidIn, tmp, datatype);
We shouldn't use [datatype '=>' datatype], the zip file has nothing to do with the datatype of your original data. Instead change it to 'uint8=>uint8', also change that in the fwrite. The final code should be:
fidTmpRaw = fopen(tmpFile, 'rb');
tmp = fread(fidTmpRaw, inf, 'uint8=>uint8');
fclose(fidTmpRaw);

ok = fwrite (fidIn, tmp, 'uint8');

Cheers!!!

lufei wang

mdcacio

Hi all.

Sorry I didn't answer so far. I'm not keeping an eye on this anymore; I think I added gzip option just as a feature but I didn't use it really.

In any case, thanks all for the comments. Hope you can improve the code for your benefit, and share anythink helpful.

Cheers,
M

Hii mdcacio, thank you very much for the code! It is helping a lot.

I had an issue when saving the file using gzip option. Do you have an e-mail so I can better explain the situation?

Cheers,

Rhenan Bartels

Urs Bohm

Hi, nice function. When I use gzip encoding it throws a warning and doesn't delete the temp zipped file. Adding fclose(fidtmpRaw) instead of the having in in the cleanup function works for me.
cheers

Hey, I rotated the image and resized it back to the original size with int16 data type then saved it ti a new file using your function. When I read again using nrrdread, I got this error:
Unexpected end of input stream when attempting to GUNZIP the file
any idea?

It works now,
I used this for the previous example
nrrdWriter('mynrrd.nrrd',x,[0.125,0.125,0.375],[-30.1875,-30.1875,-30],'gzip');

Hi
thanks for your contribution :)
I am trying to use it but I still don't understand the input parameters.
Why they are different from the data I get from nrrdread function e.g:
type: 'int'
dimension: '3'
space: 'left-posterior-superior'
sizes: '483 483 161'
spacedirections: '(0.125,0,0) (0,0.125,0) (0,0,0.375)'
kinds: 'domain domain domain'
endian: 'little'
encoding: 'gzip'
spaceorigin: '(-30.1875,-30.1875,-30)'

Have a nice day!
Ibraheem

Updates

1.2.0.0

- Fixed a little bug when path is not set up at nhdr option.

1.1.0.0

Little explanation about input parameters.

MATLAB Release Compatibility
Created with R2011b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired by: NRRD Format File Reader

Inspired: nrrdmeta2num(imgh)