eXTReMe Tracker
Oct 242012
 

1. Convert the brain.mgz volume to analyze:

mri_convert $SUBJECTS_DIR/bert/mri/brain.mgz brain.img

2. Align brain.img with your functional data in spm (eg, func.img)

3. Create your spm statistical output (eg, t.img)

4. Create the registration file:

tkregister2 --s subjectname --mov meanswaf.img --regheader --reg  register.dat --surf orig

This will bring up the tkregister window with the orig volume. Hit the “Compare” button to see the functional. The green line will be the surface. Make sure the alignment is good (ie, the green line follows the bright intensity patterns in the functional). Hit the “Save” button to save the registration. This will create a file called register.dat. If you make modifications to the registration and then want to view or edit it later, re-run the above command WITHOUT –regheader. This requires a lot of manual tweaking to get the functional brain aligned with Bert brain.

5. View your functional data in the volume:

tkmedit subjectname orig.mgz -overlay spmT_0001.img -overlay-reg register.dat

6. View your functional data on the surface:

tksurfer subjectname lh inflated -overlay spmT_0001.img -overlay-reg register.dat

Adopted from spmPainting

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));
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.

Mar 302011
 

FSL: flirt is used to compute an intial affine normalization of the T1 weighted images; this is then fed to fnirt to compute the overall transformation. flirt is also used to register the EPI’s to the subject’s structural image. This was then used along with fnirt-s warp in applywarp to normalize the EPIs.

Script for normalizing the T1-weighted structurals to the template:

Registering T1-structural to MNI152
bet my_structural my_betted_structural
 flirt -ref ${FSLDIR}/data/standard/MNI152_T1_2mm_brain -in my_betted_structural -omat my_affine_transf.mat
 fnirt --in=my_structural --aff=my_affine_transf.mat --cout=my_nonlinear_transf --config=T1_2_MNI152_2mm
 applywarp --ref=${FSLDIR}/data/standard/MNI152_T1_2mm --in=my_structural --warp=my_nonlinear_transf --out=my_warped_structural

Registering functional data to MNI152 (via structural scan)

bet my_structural my_betted_structural
 flirt -ref my_betted_structural -in my_functional -dof 7 -omat func2struct.mat
 flirt -ref ${FSLDIR}/data/standard/MNI152_T1_2mm_brain -in my_betted_structural -omat my_affine_transf.mat
 fnirt --in=my_structural --aff=my_affine_transf.mat --cout=my_nonlinear_transf --config=T1_2_MNI152_2mm
 applywarp --ref=${FSLDIR}/data/standard/MNI152_T1_2mm --in=my_functional --warp=my_nonlinear_transf --premat=func2struct.mat --out=my_warped_functional
Mar 142011
 
#!/bin/bash
 #$x would be the input filename and %3d will pad it to be a 3 digit number with zero pads, %4d will make it 4 digit and so on.
 num=`expr match "$x" '[^0-9]*\([0-9]\+\).*'`
 paddednum=`printf "%03d" $num`
 echo $paddednum
Dec 012010
 

Script to preprocess 4D fMRI time series:

spm_defaults;
 global defaults;
%% Slice Timing
display('Step 1: Slice Timing');
sliceorder = [1:2:17 2:2:16];
 refslice = 17;
 TR = 1;
 precision = 5;
 nslices = length(sliceorder);
P = dir('4Dout*.nii');
 P = char(P.name);
 TA = TR - TR/nslices;
if precision > 0
 TAstr = num2str(TA, precision);
 TA = str2double(TAstr);
 end
timing(1) = TA / (nslices -1);
 timing(2) = TR - TA;
 spm_slice_timing(P, sliceorder, refslice, timing);
%% Realignment and Reslicing
display('Step 2: Realignment');
 P=dir('a4Dout*.nii');
 P=char(P.name);
 FlagsC = struct('quality',1,'fwhm',5,'rtm',1,'interp',1);   %Flags for realignment
 spm_realign(P,FlagsC);
display('Step 3: Reslice');
 %Flags for reslicing
 FlagsR = struct('mask', 1, 'mean', 1,'interp',4,'which',0, 'wrap', defaults.realign.write.wrap);
 spm_reslice(P,FlagsR);
 display('... saving motion parameters as a motion_report.pdf ');
 saveas(gcf, 'motion_report', 'pdf');
 close all;
%% Normalization
display('Step 4: Normalization');
template = fullfile(spm('Dir'),'templates', 'EPI.nii');
 srcimage = 'mean*.nii';
meanimg = dir(srcimage);
 P = char(meanimg.name);
 snmat_name = strrep(P, '.nii', '_sn.mat');
 if exist(snmat_name, 'file')
 disp(['... removing existing file named: ' snmat_name]);
 delete(snmat_name);
 end
 objmask_name = '';
spm_normalise(template, P, snmat_name, defaults.normalise.estimate.weight, objmask_name,defaults.normalise.estimate);
%% Write Normalized Images
display('Step 5: Writing Normalized Images');
PP = dir('a4D*.nii');
 PP = char(PP.name);
 spm_write_sn(PP, snmat_name, defaults.normalise.write);
%% Smoothing
display('Step 6: Smoothing ... ');
PP = dir('wa4D*.nii');
 PP = char(PP.name);
 PP = cellstr(PP);
for list = 1:length(PP)
 Q = strcat('s', PP{list});
 spm_smooth(PP{list}, Q, defaults.smooth.fwhm);
 end
 %display(sprintf('\nPreprocessing compled.\n\nTime taken %d sec.\n', x));
Getting data extracted from time series:
Create ROI maps with WFU Pickatlas and MarsBar.
 Extract the contents of masks by:
 XYZ=struct(roi); XYZ=XYZ.XYZ;
%% Convert XYZ to mm
XYZ=[XYZ; ones(1, size(XYZ,2))]; XYZmm=roi.mat*XYZ; XYZmm(end,:)=[];XYZmm=XYZmm';
%%
p=spm_vol('swaf00xx.nii');
 [time_data]=spm_get_data(p, XYZ);
 time_data=mean(time_data,2);
Mar 082010
 

In order to convert files in .img and .hdr format to a single .nii file, we first need to install the nifti tools toolbox for matlab.

nifti tools

Once the path for nifti tools has been set in matlab, the files in .hdr and .img format can be combined to create a .nii file by running the following two commands:

nii=load_nii(‘filename_with_hdr_extension’);
save_nii(nii, ‘desired_filename_for_niftifile.nii’);

If you have freesurfer installed, you can convert between the files types with mri_convert by specifying the input (-it) and the output (-ot) type.

mri_convert -i name_of_input_file -it nifti1 -ot nii -o name_of_output_file

If you have functional dataset with several volumes over the course of your experiment, you can use the fslmerge function available in FSL to combine all the nifti files into a single 4D file.

fslmerge -t output_name.nii *.nii

Where *.nii would cover the entire set of nifti files if they are arranged sequentially.

Its not required to type in the extension .hdr or .img for the input image or output image if you have specified the input and the output types. By specifying the input type of nifti1, you tell the program that it will be a .hdr/.img pair, and specifying output type as nii goes for having your output with .nii extension.

To gzip the nifti file for use in freesurfer:

gzip filename.nii filename1.nii.gz

This will create a gzipped nifti file.

To automatically segment the structural volume:

recon_all -i filename1.nii.gz -subjid SUBJID -all

This will segment the structures containing in the structural file filename.nii.gz. Complete information on using recon-all for freesurfer can be found at FreeSurfer Wiki.

For some reason, Freesurfer seems to work well in tcsh. I’ve often encountered problems of weird nature when running it under bash. In order to run your autosegmentation with freesurfer type in tcsh at the terminal, since by default it is set to bash in most linux distros.

Mar 032009
 

I had to create a movie/animation out of a series of images that I had in my folder. There were several methods described on several forums which suggested using GIMP for creating GIF files from JPG or PNG files when one is using Linux. Now the problem was that I had 70 files in total and it would have taken me a long to time to add all those images to GIMP in order to create my animation file.

Then I read somewhere that we can actually create GIF images in command line mode using Imagemagick. But to do so you need to have Imagemagick installed on your system. To install it in Ubuntu, run:

sudo apt-get install imagemagick

This program has several features that would let you do all sorts of things with images. I was interested in converting a bunch of PNG files into an animated GIF file. To do so, we need to  rename the files in a way that they would lie in a sequence when arranged alphabetically (if you plan on doing it the easy way). Suppose you have files named slide_01.png, slide_02.png, slide_02.png….slide_xx.png, and you want to convert them to movie.gif, we run:

convert -delay 10 -loop 0 slide*.png movie.gif

The parameter delay inserts a desired delay between two consecutive slides. The number x used for delay inserts 10x milliseconds of delay between two frames. Loop parameter 0 makes it repeat infinitely.

If you had files with non-uniform names, then you need to input each of them in a sequence after the delay and loop parameters. Suppose you have file summer.jpg, fall, winter.jpg, fall.jpg and spring.jpg and you want to order them as fall, winter, spring and summer in the gif image seasons.gif with a 1 second delay between each of them, use:

convert -delay 100 -loop 0 fall.jpg winter.jpg spring.jpg summer.jpg seasons.gif

Here are some animations of numbers from 0 to 9 with varying delays.

The delay values specified the above cases were: 1, 5, 15, 25, 50 and 100.

gif animation with 10 millisecond delay

GIF animation with 10 millisecond frame delay

gif animation with 50 millisecond delay

GIF animation with 50 millisecond frame delay

gif animation with 150 millisecond delay

GIF animation with 150 millisecond frame delay

gif animation with 250 millisecond delay

GIF animation with 250 millisecond frame delay

gif animation with 500 millisecond delay

GIF animation with 500 millisecond frame delay

gif animation 1 second delay

GIF animation 1 second frame delay

=================

Feb 182009
 

This evening I took a snapshot of my desk (which holds my desktop) with my cellphone camera. After transferring it via bluetooth, I opened the image and it was rather amusing to see the exact image of desk inside of an identical image. So just out of curiousity I snapped another photo with roughly the same FOV as before and loaded it on the computer, and again took a picture of that.

Photo iterations: or photo within photo

Photo iterations: or photo within photo

The picture looks quite interesting, as it depicts the same object at different time points within the same spatial space. I don’t see any artistic value in this picture, but what I do see is a lot of potential for philosophical discussion about time and space and how we can represent variations in time within a 2D space.

Feb 102009
 

Recently I bought a student version of Matlab2008b. It was for Linux platform. Here’s my experience with installing the program on 64-bit Linux (Debian) architecture, what problems arose along the way and how they were addressed – with help from Mathworks tech support.

Matlab at work

Matlab at work

Installing Matlab 7.7 (i.e. Matlab2008b) the old fashioned way was a breeze, but getting it to actually run on 64-bit architecture involved a few steps, which I didn’t know. I sent a query to Mathworks tech support saying:

The installation files were able to successfully install Matlab2008b on Ubuntu 9.04 (Jaunty), however, the program failed to load since it cannot find the jre files for 64-bit architecture. Is there a workaround for running the 32-bit program on 64-bit platform?

And they replied with a procedure to be followed to make that happen:

Yes – here is a full walkthrough:

1) When running the MATLAB installer (install_unix.sh) use the -glnx86 flag.  This will allow it to install properly on a 64-bit Linux machine:

./install_unix.sh -glnx86 -t

2) Once the installer is finished, you will need to activate through mathworks.com, as the activation client will not work properly on 64-bit computers.  To do so, follow the instructions below:

http://www.mathworks.com/support/solutions/data/1-3YZBZ6.html?solution=1-3YZBZ6

3) Once MATLAB is installed and activated, you will need to run MATLAB using the same -glnx86 flag that you used to install:

$MATLAB/bin/matlab -glnx86

(where $MATLAB is the MATLAB installation folder)

NOTE: Some users have problems loading the JRE when running in this configuration.  Creating a symlink in $MATLAB/sys/java/jre named glnxa64 pointing to glnx86 should resolve this issue.  You can do this with the following command:

ln -s $MATLAB/sys/java/jre/glnx86 $MATLAB/sys/java/jre/glnxa64

And now it works. Haven’t had any GUI crash on me yet, but it been less than an hour since I’ve been using it. One way to avoid typing matlab -glnx86 before each launch is to create a local alias.

gedit ~/.bashrc

Under one of the listing for aliases, you can type in:

alias mat='matlab -glnx86 &'

Save and exit. Then onward, you can launch the program by simply typing in mat in the terminal.

In case your compiz is enabled, you might not see the menus correctly. To fix that problem, you have to run the following command at the terminal.

export MATLAB_JAVA=/usr/lib/jvm/java-6-sun/jre/
Feb 032009
 

In 1858, two German mathematicians, August Ferdinand Möbius and Johann Benedict Listing, independently discovered what is popularly known as the Möbius strip. The characteristic feature of Möbius strip is that it is a surface with single side. In its most simplest form a Moebius strip can be constructed out a a strip of paper which is twisted halfway and the ends joined together. If one were to start tracing a surface, by the time they complete one trace they find that they are tracing the opposite side of the paper than the one from which they started. Go another round and you come back to the same side.

Basic Moebius strip (twisted ribbon)

Basic Moebius strip (twisted ribbon)

A Mobius strip can be expressed mathematically in several diffferent forms. Geometric term for the the form exhibited by a Mobius strip is a Chiral. The parametric equations for a Moebius Strip can be expressed as:

x(u,v) = cos(u) + v*cos(u/2)*cos(u)
y(u,v) = sin(u) + v*cos(u/2)*sin(u)
z(u,v) = v * sin(u/2)
Default values for u and v:
u = [0, 2π] for one complete loop;, v = [-0.4, 0.4]

An equation for constructing a Moebius Strip using Matlab can be found at the Univesity of Stutgart’s mathematic department – Matlab code repository.

Rendering of Moebius strip using Matlab

Rendering of Moebius strip using Matlab

Among the most famous artwork using a Moebius strip is the one by M.C.Escher, which has a grid in the shape of gigure-8 Moebius strip with ants crawling on different sections along the strip.

M. C. Escher - Moebius Strip (insects)

M. C. Escher - Moebius Strip II (insects)

M.C.Escher has another piece of art in the form of a Moebius strip. This one has 3/2 twists instead of 1/2 as in the basic strip. By playing around with number of twists, one can create beautiful and complex loops.

M. C. Escher - Moebius Strip (twine)

M. C. Escher - Moebius Strip I (ribbon)

A simple, yet intriguing model of a Moebius strip is the one where a 1/2 twist Moebius strip is constructed from a transparent strip of plastic with the word Möbius written on it.

Moebius Strip written onto a transparent Moebius Strip

Moebius Strip written onto a transparent Moebius Strip

One of the most common example of a Moebius strip one encounters on a day to day basis is the Recycling sysmbol which has three foldeed arrows forming a loop. The folded arrows aren’t identical. One of the arrows folds in the opposite sides from the remaining two arrows. The design of recycling symbol appears somewhat similar to the Escher’s artwork with 3/2 twists.

Commonplace use of Moebius Strip - Recycling Symbol

Commonplace use of Moebius Strip - Recycling Symbol

This comic strip in the form of a infinitely repeating sequence of a stick figure kicking a a football at an unsuspecting stick figure and knocking it down is yet another example of silly, yet creative Moebius stripping.

Cartoon depiction of a continuous reel Moebius Strip

Cartoon depiction of a continuous reel Moebius Strip

Origami art, i.e. the art of paper folding, can be used to create some incredible shapes simply by folding a piece of paper. Here is an example of a Moebius strip created with Origami folding. The corrugated pattern on the strip makes it appear quite classy.

Origami paper folding - Moebius Strip

Origami paper folding - Moebius Strip

Here is an illustration of Moebius strip on a book cover. I haven’t been able to figure out of the illustration is an actual photograph if a jigsaw puzzle or it is merely a creation in Photoshop. Either way, a jigsaw puzzle that would form a jigsaw puzzle in the end would keep anyone occupied for several days – and several more if the structure keeps tumbling every now and then.

Moebius Strip depicted as a jigsaw puzzle on a book cover

Moebius Strip depicted as a jigsaw puzzle on a book cover

Creations that incorporate a Moebius strip are rather intriguing even as still images, but with animations they take on a spectacular form.  One is a interlocked gear chain and the other one is an escalator in the form of a half-twist Moebius strip.

Moebius strip in the form of a gear chain animation

Moebius strip in the form of a gear chain animation

Animation of Moebius Strip as an escalator

Animation of Moebius Strip as an escalator

Then there are some architectural models of Moebius strip in forms of scuptures that are placed at tourist attractions, and some that are placed in playgrounds.

Robert R. Wilson's sculpture of Moebius Strip at Ramsey Auditorium

Robert R. Wilson's sculpture of Moebius Strip at Ramsey Auditorium Fountain

Moebius Strip structure at a cildren's playground

Moebius Strip structure at a cildren's playground

Moebius Strip - 12-panel Moebius Climber at a playground

Moebius Strip - 12-panel Moebius Climber at a playground

Moebius Strip Jungle Gym near the Temple of Honor

Moebius Strip Jungle Gym near the Temple of Honor

And then there is a Moebius strip cake – how’s that for sweet!

Cake baked in the shape of a Moebius Strip

Cake baked in the shape of a Moebius Strip