Monday, 12 September 2011

Plotting polar images in Matlab

I'm currently working with ultrasound images acquired on a polar grid, with equispaced range and angle values. Such images cannot be plotted directly using the image or imagesc command in Matlab, since it requires a x-y grid of coordinates. At first I tried resampling the polar image to a rectangular grid using a 2D interpolator (TriScatteredInterp), which works but takes quite a lot of time. However, at a conference I attended recently I got a very useful tip from Marcelo Matuda from the University of São Paulo: 

Convert the polar coordinates to rectangular coordinates, and plot the image as a surface, seen directly from above. Simple! And fast.

Here is a little example code that illustrates this approach:

%% ---------------------------- %%
close all
clear all

%% Create an example image using peaks function
im = peaks(512);

%% Specify axes for the image (chosen arbitrarily)
[nZ,nX] = size(im); 
theta = ((0:(nX-1))-nX/2)*(0.1*(pi/180)) - pi/2;
rr = (0:(nZ-1))*0.1e-3 + 0.05;

%% Plot image in rectangular coordinates
figure
imagesc(theta*(180/pi), rr*1e3, im)
xlabel('theta [deg]')
ylabel('r [mm]')

%% Create grids and convert polar coordinates to rectangular
[THETA,RR] = meshgrid(theta,rr);
[XX,YY] = pol2cart(THETA,RR);

%% Plot as surface, viewed from above
figure
surf(XX*1e3,YY*1e3,im,'edgecolor','none')
view(0,90)
xlabel('x [mm]')
ylabel('y [mm]')
%% ---------------------------- %%

The code outputs two images, one of the polar plot made using the imagesc command:


... and one with the "proper" polar plot using surf: 


Sunday, 4 September 2011

Fixing SD card error on HTC desire

I recently started getting problems with the SD card on my HTC Desire phone. A notifier saying that "The SD card has an unexpected problem ..." would pop up, and if I tried using the camera, an error message would display, saying that the the SD card was mounted as read-only.

After some googling I found that several others had a similar problem. After reading this post I tested running mounting the card in a card reader and running the dosfsck command, with syntax

dosfcsk -va /dev/sdb1

This executed without error, but after putting the card back in the phone, I found that the same error messages were still popping up. After reading this forum thread, I finally decided to back up the contents of the card, and reformat it to FAT32 it using GParted. Even if this is possibly a suboptimal solution, it fixed the problem, and I once again have a functioning phone.

Thursday, 1 September 2011

Cropping an image from the command line (/Matlab)

I recently had to crop some figures which had a lot of unnecessary white space on the sides. The figures were made in Matlab, and I wanted to find some way to do the cropping from the command line, to avoid manually editing each image.

As always when it comes to image editing, ImageMagick provides a solution, as explained here. The "convert" command is used to crop images with the following syntax:

convert -crop (width)x(height)+(horzOffset)+(vertOffset) infile outfile

where (horzOffset) and (vertOffset) denote the pixel coordinates of the upper left corner of the cropping window (sideways, down). The parentheses are not included in the syntax. Example:

convert -crop 1800x1597+700+0 im1.png imCropped.png

crops im1.png to size 1800x1597, with 700 horizontal offset and 0 pixels vertical offset. To do the same from Matlab, simply use the "system" command:

> system('convert -crop 1800x1597+700+0 im1.png imCropped.png')

Wednesday, 6 July 2011

Setting up dual displays in Ubuntu (VGA & DVI)

For a time, I've been wondering how to set up dual displays in Ubuntu. I have two screens connected, one via DVI and one via VGA. Whenever I tried using both, the VGA screen would always be placed to the left, whatever setup I made in the display settings. Finally, I found the solution in Mark's blog post.

The key is the xrandr command line function. I first enabled both displays under the display settings, and then used the command

xrandr --output VGA1 --right-of DVI1

Note that you may have to call the xrandr function without any arguments first, to find the name for each screen.


Wednesday, 18 May 2011

Removing N first characters from each line in a file

I recently had to remove the 5 first characters from each line in a text file; it was a source code file copied from the internet with the line numbers included. I found a nice way to do this in a forum post:

sed -i 's/\(.\{5\}\)//' file.m

Here, the "sed" function of the Linux command line is used with the -i (for "in place") switch to remove the 5 first characters from each line in the file "file.m". It looks a bit messy, but it works.

Wednesday, 4 May 2011

Using LaTeX in Inkscape Drawings

I use Inkscape to draw all my vector graphics. I often use these graphics in combination with PdfLaTeX to write papers and Beamer presentations. However, one of the things I have been struggling with is how to include greek letters and mathematical formulas in the graphics. One simple solution for displaying Greek letters is to use the corresponding unicode. For example, to display the letter gamma, create a text box, press Ctrl+U, and enter <03B3>. However, the character will (usually) not be available in the same font as the LaTeX document, and it is also hard to write anything more complex than single characters.

It was not until today that I found out that Inkscape actually has a export option that solves this problem quite nicely. This is documented very nicely here. To create a drawing with LaTeX elements, simply enter the LaTeX code in a text box, export as PDF, and cross the box in the PDF export options. This will export the graphics to a PDF file and the text to a separate .tex file, which can then be included in the LaTeX document using this syntax (or similar):

\begin{figure}
\centering
\def\svgwidth{\columnwidth}
\includesvg{image}
\end{figure}

where the file name of the exported graphic is "image". Note that no additional packages are needed. Consult the documentation for more details.

Tuesday, 3 May 2011

Spell checking LaTeX documents

Spell checking a LaTeX document is different from spell checking a plain text document, since the LaTeX document contains commands that should be ignored by the spell checker. For example, consider this command for including a figure with a caption:

\begin{figure}
\centering
\includegraphics[width = 7cm]{image1}
\caption{Focused image of piont scatterer.}
\end{figure}

Here, a spell checker should not suggest changing "\includegraphics" to "include graphics", but it should suggest changing the misspelled "piont" in the caption to "point". Luckily, there are spell checkers that interpret LaTeX commands, and I am currently using one of them, Aspell. In Linux, Aspell is easily installed by the following commands:

apt-get install aspell
apt-get install aspell-en

which installs both the program and the english dictionary. To spell check a LaTeX file called paper.tex on the command line, type

aspell -c paper.tex

Note that Aspell (and its relative Ispell) can also be used at runtime by text processing programs like Kile.