eXTReMe Tracker
Dec 152014
 

Converting nifti files to dicom using nifti toolbox in matlab

clear; clc;
 addpath('/usr/local/matlab/toolbox/niftitools');
 subjectdir='/media/storage/SPECT_mapping/bbobby';
 cd(subjectdir);
 [dicomfile, dicompath] = uigetfile('*.dcm','Select DICOM header for metadata');
 metadata = dicominfo(fullfile(dicompath, dicomfile));
 metadataISAS=metadata; metadataMPR=metadata; metadataFUS=metadata;
 
 metadataMPR.SeriesDescription = 'SAG_MPRAGE_T1_Recon'; metadataMPR.SeriesNumber = 1;
 metadataISAS.SeriesDescription = 'ISAS Hyperperfusion'; metadataISAS.SeriesNumber = 2;
 metadataFUS.SeriesDescription = 'MPRAGE ISAS Fusion'; metadataFUS.SeriesNumber = 3;
 
 cd(subjectdir);
 isas = uigetfile('hyper*.nii','Select hyperpefusion file');
 structural = uigetfile('mprage*.nii','Select MPRAGE');
 
 isas = load_untouch_nii(isas); isas = isas.img; isas = int16(isas);
 structural = load_untouch_nii(structural); structural = structural.img;
 
 display('... Merging Hyperperfusion and MPRAGE');
 
 merged = structural;
 merged(isas>0) = max(max(max(structural)))+10;
 
 display('... Finished Merging')
 
 isasdir = fullfile(subjectdir,'isas');
 if exist(isasdir) ~= 7
     mkdir(isasdir);
     mkdir(fullfile(isasdir,'SPECT_ISAS')); spectdir = fullfile(isasdir,'SPECT_ISAS');
     mkdir(fullfile(isasdir,'structural')); structdir = fullfile(isasdir,'structural');
     mkdir(fullfile(isasdir,'fusion')); fusiondir = fullfile(isasdir,'fusion');
 end
 
 cd(isasdir);
 
 % dicomwrite(reshape(x4,[170,256,1,256]), 'SPECT_ISAS.dcm', metadata, 'CreateMode', 'copy');
 display('... Creating DICOMS');
 
 for slicenum = 1:size(isas,1)
     
     cd(spectdir); metadataISAS.InstanceNumber = slicenum;
     dicomwrite(fliplr(rot90(squeeze(isas(slicenum,:,:)))), sprintf('SPECT_ISAS_%03d.dcm',slicenum), metadataISAS, 'CreateMode', 'copy');
     
     cd(structdir); metadataMPR.InstanceNumber = slicenum;
     dicomwrite(fliplr(rot90(squeeze(structural(slicenum,:,:)))), sprintf('structural_%03d.dcm',slicenum), metadataMPR, 'CreateMode', 'copy');
     
     cd(fusiondir); metadataFUS.InstanceNumber = slicenum;
     dicomwrite(fliplr(rot90(squeeze(merged(slicenum,:,:)))), sprintf('FUSION_MPR_ISAS_%03d.dcm',slicenum), metadataFUS, 'CreateMode', 'copy');
     
     display(sprintf('... Creating slice %d',slicenum))
 end
 
 cd(subjectdir);
Sep 192012
 
Simple image compression algorithm using plain FFT computation

FFT2 based image compression

Test code to generate a circular mask that removes the low frequency components from an image.

Step through radius to get the percentage compression.

IM=imread('images_01.jpg');IM=IM(:,:,2); %Select a channel
%
cx=size(IM,2)/2; cy=size(IM,1)/2;
ix=size(IM,2);iy=size(IM,1);r=833;
[x,y]=meshgrid(-(cx-1):(ix-cx),-(cy-1):(iy-cy));
c_mask=((x.^2+y.^2)>r^2);
%
filtIM = fft2(IM).*c_mask;
filtIM = ifft2(filtIM);
IM2=abs(double(IM)-abs(filtIM));
%
x=size(IM,1)*size(IM,2); y=size(find(c_mask==1)); y=y(1);
res=y/x*100;
%
figure(1); 
subplot(2,2,1); imshow(c_mask);
subplot(2,2,2); imagesc(IM2); axis image; axis off;
subplot(2,2,3); imshow(IM); title('Origianl image');
subplot(2,2,4); imagesc(abs(filtIM)); axis image; axis off;
title(sprintf('Percent original: %0.2f',res));
Mar 052012
 

Here’s a quick chart I made for quick look-up for conversion between Celsius and Fahrenheit scale.

[°C] =  5/9 × ([°F] – 32)

A graphical lookup for conversion between Fahrenheit and Celsius

A graphical look-up for conversion between Fahrenheit and Celsius (click to view full size)

The bold marking are at multiples of 100 degree Celsius.

Nov 162011
 

On most Linux/Unix based systems, you can save your Matlab figure as a postscript instead of saving it as a png or jpg. One advantage of saving figures in this manner is that you can save multiple images as separate pages on that .ps document, whereas with png/jpg you have to save each image as a separate file.

To print the current image to postscript, all you need to run is:

print(‘-dpsc2′,’filename’);

To have multiple images appended within the postscript document, you need to use the -append switch.

print(‘-dpsc2′, ‘-append’, ‘filename’);

Your output postscript will have the name filename.ps

To convert the postscripts to pdf format, just run the ps2pdf command in the terminal window:

ps2fpdf filename.ps

The output of this would be filename.pdf

And you’re done.

Apr 142011
 

Assuming the nifti toolbox is in Matlab path, we can get the 91x109x91 mask to have the same dimensions as the normalized images generated with bounding boxes.

If we are making a mask for hippocampus, first we save that mask from WFU Pickatlas. Then to make it 79x95x68 voxels, run the following small script.

x=load_untouch_nii(‘hipp.img’);
x=x.img;xdim=[1:6 86:91]; ydim=[1:6 102:109]; zdim=[1:11 80:91];
origin=[40 57 26]; datatype=16;
x(xdim,:,:)=[]; x(:,ydim,:)=[]; x(:,:,zdim)=[];
nii=make_nii(x, [2 2 2], origin, datatype);
save_nii(nii, ‘boxedhippo.nii’)

We can then use these masks for signal extraction or any further processing.