This finding is in no way revolutionary, but I found it useful, so maybe others will too. I'm running Ubuntu Linux on my laptop, and recently I wanted to run a Matlab script on a Linux workstation. I can log on the the workstation using ssh, for example
ssh martin@server.name.no
which gives me access to the terminal. However, SSH can also be used for graphical programs, if the server is running a X window system. The SSH connection is configured for X session forwarding by adding a "-X" switch at logon,
ssh -X martin@server.name.no
Such forwarding is further explained here. If I logon using the above syntax and start matlab, it appears as a regular window on my desktop, although it's running on the workstation. Unfortunately it can be a bit slow in responding to mouse and keyboard input, so using the terminal version of Matlab may in some cases be just as good.
Wednesday, 4 January 2012
Sunday, 11 December 2011
Changing the bit depth of images using ImageMagick
Today I wanted to create a simple black-and-white version of a grayscale image that I have. This is pretty easily done in Matlab (you can scale the image to values between 0 and 1 and use round()), but I was guessing there was probably already some simple way to do it in ImageMagick - and there is!
This is the original version of the image (from the great graphical novel series "Bone"):
This is the original version of the image (from the great graphical novel series "Bone"):
To convert it to a 3-bit image (8 levels), I used the -depth switch with the convert command:
convert bone.jpg -colorspace gray -depth 3 bone3bit.jpg
And similarly, for black-and-white (1 bit image):
convert bone.jpg -colorspace gray -depth 1 bone1bit.jpg
Friday, 11 November 2011
Setting up Matlab mex compilation in Ubuntu
NOTE: This post was updated on January 16, 2012.
Recently I have been trying to compile some Matlab mex-files from the excellent Machine Vision Toolbox by Peter Corke. When I first tried to compile, I got the following warning:
Warning: You are using gcc version "4.6.1-9ubuntu3)". The version
currently supported with MEX is "4.3.4".
I tried several ways of "downgrading" gcc, but as it turns out, the newer version of gcc on Ubuntu 11.10 is backwards compatible and works fine. The message above is simply a warning, not an error, and there is no need to change gcc to an older version. However, my real problem was this: I got errors of the type
error: expected expression before ‘/’ token
I had no idea why this error was popping up, until I read a post on similar issues on xcorr. It turns out that C++ style "//" comments are not allowed in the standard mex configuration that is set when you run "mex -setup". To avoid the error, open the mexopts.sh file (mine is located in ~/.matlab/R2011b/mexopts.sh), and replace all occurences of "-ansi" with "-std=c99".
Thursday, 3 November 2011
Batch renaming files in Ubuntu Linux
I often deal with a large number of data files, for example files containing ultrasound data from experiments. Recently I had to rename about 200 files which were named something like this,
oldName001.mat, oldName002.mat, ..., oldName200.mat
to something like this:
newName001.mat, newName002.mat, ..., newName200.mat
oldName001.mat, oldName002.mat, ..., oldName200.mat
to something like this:
newName001.mat, newName002.mat, ..., newName200.mat
Basically, I wanted to keep the numbering but change the base of the name. I found out that on Ubuntu 11.10, which I'm currently using, there are two different flavors of the "rename" command. One is a Perl script, and using this, the syntax for doing the above operation is
rename "s/oldName/newName/" *mat
To test without actually renaming, use the "-n" switch. Type "man rename" or have a look at these Webmaster Tips for several more examples of this usage. However, there is also a rename command as part of the linux-util package. To distinguish this from the above command in Ubuntu, call the command as rename.ul:
rename.ul oldName newName *mat
Notice that the syntax is simpler and easier to remember for this version. Type "man rename.ul" or have a look at this NixCraft post for more examples.
Finally I found out that it is also possible to batch rename files using Thunar, which is the default file manager in Ubuntu 11.10. Mark all the files, right-click and choose "rename", and use the "Search and replace" option.
Wednesday, 26 October 2011
Installing Matlab with standalone user on Ubuntu Linux
OK, after doing this in an awkward manner a couple of times, here is how I install Matlab on Ubuntu with a standalone license:
I also recommend having a look at the Ubuntu Documentation for Matlab installation. It might not always be completely up to date, but the procedure doesn't change much between releases.
- Download the files from Mathworks
- Run the install script with sudo (as root), but DO NOT activate the license yet. Choose custom install, use default install location, tick the box for creating symbolic links.
- After installing, try to run Matlab (not as root). The activation procedure will start automatically. Register your own user name for the standalone license.
- Matlab (with GUI) can now be run by calling the command "matlab -desktop". To create a application launcher for it, use the "Main Menu" program (also called Alacarte, can be installed with the Ubuntu Software Center). Get a logo for the launcher here, or put it right where it belongs by using this command:
I also recommend having a look at the Ubuntu Documentation for Matlab installation. It might not always be completely up to date, but the procedure doesn't change much between releases.
Monday, 10 October 2011
Merging images into movie using FFmpeg
Let's say that you have a number of images, named image001.jpeg, image002.jpeg, image003.jpeg etc., and you want to merge them into a single movie, for example to create a time-lapse video. FFmpeg is a great commend-line tool for doing this kind of stuff. To use FFmpeg to create the movie "outmovie.mp4", with 25 frames per second and reasonable quality, enter
ffmpeg -i image%03d.jpeg -sameq -r 25 outmovie.mp4
ffmpeg -i image%03d.jpeg -sameq -r 25 outmovie.mp4
Note the format image%03d.jpeg, indicating that the numbering of the files has three digits with (possible) leading zeros. To set the output bitrate explicitly to for example 64 kbits/s, use the switch "-b 64k" rather than "-sameq". See the FFmpeg documentation for lots of other possibilities.
Wednesday, 5 October 2011
Converting images from PDF to PNG using ImageMagick
I usually prefer vector formats like PDF for my figures, but some times I need to make a PNG version. ImageMagick is my tool of choice to do this, since it can be called from the command line. My initial try was something like this:
convert figure.pdf figure.png
which indeed converts the figure from PDF to PNG, but with a much too low resolution in the PNG image. After reading up on the subject on the Magick-users mailing list and the ImageMagick documentation, I found out that the default resolution at which the PDF file is read is 72 DPI. To read with a higher resolution, for example 600 DPI, use the -density option:
convert figure.pdf figure.png
which indeed converts the figure from PDF to PNG, but with a much too low resolution in the PNG image. After reading up on the subject on the Magick-users mailing list and the ImageMagick documentation, I found out that the default resolution at which the PDF file is read is 72 DPI. To read with a higher resolution, for example 600 DPI, use the -density option:
convert -density 600 figure.pdf figure.png
As a little side comment, I could mention that I first tried to do this because when I saved a figure from Inkscape as PNG, the resolution was very poor. The workaround was then to save as PDF and convert to PNG. However, afterwards I remembered that Inkscape also has a "export bitmap" option, which will allow you to set the density before saving to PNG. Oh well, the ImageMagick conversion may still come in useful some day...
Subscribe to:
Posts (Atom)



