Thursday, 29 September 2011

Matlab and version control with Subversion

At my company, we use Subversion for version control of our software. I currently use RabbitVCS as a file explorer front-end for Subversion (in Ubuntu Linux), but I've been looking for a way to use Subversion directly from Matlab.

I haven't found any solution that fully integrates with the file explorer in Matlab yet, although there are some useful tools available. However, it wasn't until today that I realized (the bleeding obvious fact) that the Subversion command line interface can be used directly from the command line. Simply put an exclamation mark in front and call the svn commands as usual, for example:

!svn help
!svn status
!svn add file1.m
!svn commit -m "This is a log message"

The command line version of Subversion is both simple and reliable, so until the Mathworks makes an official Subversion plugin, I guess I'm sticking to this approach.

Thursday, 15 September 2011

Using ImageMagick to convert images to grayscale

Although color images are nice, I try to avoid using them in articles, as that always costs extra. Sometimes I also prefer grayscale images for presentations, since the colors of the projector can often be quite different from what I see on my screen.

Anyway, here is a simple way to convert an image from color to grayscale using the command line tool ImageMagick:

convert inputFile -colorspace Gray outputFile

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
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
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')