eXTReMe Tracker
Dec 222014

The purpose is to to convert a 3-dimensional image file which contains X,Y and the dimensions containing RGB values to a 2D image file in grayscale. It can be done in linux command line using imagemagick.

convert <img_in> -set colorspace Gray -separate -average <img_out>

The resulting image is a 2D image generated by averaging the R,G,B arrays.

Dec 152014

Converting nifti files to dicom using nifti toolbox in matlab

clear; clc;
 [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;
 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(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');
 % 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))
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;
filtIM = fft2(IM).*c_mask;
filtIM = ifft2(filtIM);
x=size(IM,1)*size(IM,2); y=size(find(c_mask==1)); y=y(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));
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:


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=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.