Archive

Archive for the ‘Matlab’ Category

Ellipse Selection in Matlab

July 17th, 2007 4 comments

Much of the work on image segmentation that I do requires an initial guess of the answer. This initialization gives the algorithm something to improve. Hopefully, it improves all the way to the correct answer. You can use any manner of contour as an initialization. I like to select slightly different contours while I’m testing to make sure I’m not ‘over-tuning’ my system for some specific set of initial conditions.

Sometimes, it is preferable to use a very close initial outline like this:

Close initilization

(by the way, if you are interested in initializations like this see this code)

However, when you are trying to demonstrate the robustness of your technique (and/or you don’t want to waste time drawing a complicated contour) it is nice to use geometric shapes. Squares are popular, and can be captured using this simple Matlab code.

>>[rect] = getrect(1);

Many things in life are shaped more like ellipses, though. For this case, there are no built-in Matlab functions to get this type of initialization. Fear not! I wrote some simple code that allows you to capture graphically (or define in terms of parameters) an ellipse very simply. The params are major and minor radius (a, b), center location (x0, y0) and angle of rotation (rho).

>>[mask, a, b, x0, y0, rho] = get_ellipse(I, a, b, x0, y0, rho);

Here is an example of the type of initialization you can get (shown in white). Also in the image below you can see the final segmentation in green.

Ellipse Initialization

Download this .m file and try it out yourself.

Categories: Matlab Tags: ,

Matlab: Figures without borders and toolbars

June 12th, 2007 7 comments

Something that has bothered me for *the entire* time I’ve used it is that when you show images using ‘imshow’ the resulting figure has the image surrounded in a sea of gray border. Well, I hate that gray border. It serves no purpose and today I have figured out how to remove it!

Matlab runs a script called ‘startup.m’ when it starts. This is located in your ~/matlab/ directory. If you include this line into that file (or just type it before you make the figure) then you remove the gray border:

>>iptsetpref('ImshowBorder','tight');

Its glorious… Now the picture scales to fill the whole window! (note this only works with the ‘imshow’ command, not ‘imagesc’).

BUT! That’s not all. I also learned about some other cute little tricks that should be included in your startup.m (if you’re me at least).

%removes menu and toolbar from all new figures
>>set(0,'DefaultFigureMenu','none');
%makes disp() calls show things without empty lines
>>format compact;

I feel like these three commands cut a lot of the pointless (inefficient) crap out of Matlab’s default display interface. Thanks to commenter Matt who pointed me toward the set(0,’Default… thing. You can use this to set up *tons* of settings so that you don’t have to set it every time. Just call:

>>set(0,'Default');

For some other useful tips on exporting figures for papers, check this related post.

Categories: Matlab Tags: ,

Active Contours

May 15th, 2007 25 comments

UPDATE:
My new post: Sparse Field Active Contours
implements quicker, more accurate active contours.

The well-known Chan-Vese segmentation algorithm from the paper “Active Contours Without Edges,” is a great example of active contours. This technique deforms an initial curve so that it separates foreground from background based on the means of the two regions. The technique is very robust to initialization and gives very nice results when there is a difference between the foreground and background means.

In this video, the curve begins as a square. As time goes on the square changes shape so that it does a better and better job of separating the image into a light area and a dark area.

Below is a download-able Matlab demo. The code is very easy to read, and could be the foundation for lots of other active contour segmentation techniques.

sfm_chanvese_demo.zip (New! Described Here)

regionbased_seg.zip (old and slow)

I recently added some new active contour stuff based on a more complex (and sometimes more capable energy). Check out the latest results, and the full project writeup which is a little older!

Videos in Matlab and Linux

April 23rd, 2007 2 comments

If you do computer vision research (as some of you may). Or any other type of research for that matter… Its a good idea to make nice videos of the results that you obtain. This is a simple disaster-proof way of demoing your work. I’ve been doing this quite a bit lately (as a result of all the video tracking work). I’ve come up with some tips, tricks, and tools that will help out the aspiring video-creator (who uses Matlab and Linux… otherwise you’re out of luck).

Personally, I find Matlab’s built-in avi code terrible. It’s just bad. Instead, I prefer to make figures that show what I want to show on each frame, save those out, and then compile them into a movie later. This seems to be the nicest command to save the frames out:

>>for i=1:last_frame
>> %% code to create the frame in a figure
>> f = getframe;
>> imwrite(f.cdata, sprintf('./video/%04d.png',i));
>>end

This should leave you with a directory full of .png files. From here you have to assemble these into a movie. This is a fantastic script called mkmpeg4 (download). The way to run it is as such (by the way, this only works in Linux with mplayer installed)

$mkmpeg4 -o output.avi -f 30 `ls *.png`
$mplayer output.avi

Note the ` quotation marks as opposed to your typical ‘ marks. This makes compressed videos that look good and will still play on Linux, Windows, and Mac. (Also, they work in PowerPoint). One final tip. If you want to go the other way; take a video and convert it to frames, here’s how:

$mplayer -vo png movie_to_unpack.avi

If you video file is interlaced (looks good in a player, extracted images look bad), try this instead:

$mplayer -nosound -vf pp=ci -vo png:z=0 movie_to_unpack.avi

Happy Matlab Video-ing. Feel free to post your own tips or correct mine in the comments.

Categories: Mac/OSX, Matlab Tags: , ,

Technical Posters

February 14th, 2007 1 comment

I just finished making the poster that I will present in San Diego next week (take a look). I have a couple of thoughts regarding this experience:

1) Vector graphics are soooo cool. Always use them if you can… always. (note: I talk about how to get vector graphics in .eps form out of Matlab quickly and easily here: Matlab TeXniques)For those of you who don’t know why vector graphics are cool, let me enlighten you! When you resize a normal image (gif, png, jpg) you will run into problems (click image above). If you make it smaller, you throw away data, and if you make it bigger, you have to make up data! Both of these are bad. With vector images, though, you can scale them up and down all you like and they look as crisp & clean as the day they were created! Wonderful.

2) If you want to put LaTeX symbols in your power point file, forget TeXPoint… All the cool kids are using TeX4PPT these days. Its *free,* much easier to use, and creates (you guessed it) vector images of your LaTeX stuff instead of bitmaps. One potential hang-up is that it only runs on Office 2003+. Sorry all you guys still on XP.

Some slight problems that I had: I think it was due to not setting paths right, but I had to put my .ppt file in the miKTeX root director for it to compile the TeX. Also, you can’t right-click your text boxes to “TeXify” if the text has the squiggly red underline (just find an un-underlined place).After that rousing adventure in technical advice… if you’d like to see my poster without flying all the way to San Diego, click on the image above to open it in .pdf!

Categories: Academic, Matlab Tags:

Matlab TeXniques

January 29th, 2007 1 comment

Those of you who don’t know or don’t care what Matlab is… today’s post isn’t for you. Just skip it.

Now that the cool kids have left, us geeks can be alone: So, in my lab at GT we primarily use Matlab, so I’m trying to rekindle that romance after a torrid affair with C++ at Siemens. Unarguably, one of the best things about Matlab is how easy it is to visualize things. However, one little tidbit has always bugged me. There is always that damn big fat gray border around all of your figures. This is only mildly annoying while programming… but when it comes time to save figures out for a paper or a report annoying becomes infuriating. You have to scale and crop and zoom and you can’t script it well at all. (Not being able to script things is one of my pet peeves.)

But fear not dear friends… I have found the solution: exportfig. This is a great script for Matlab that lets you A) use tight borders for the figure, B) change the colors cleverly to black and white or grayscale, C) save the figure directly as a .eps file for easy integration with LaTeX! Here’s a nice page that explains how to use the library: Exporting Figures for Publication: Part 2.

One question that I haven’t been able to solve is how to eliminate that gray border in day-to-day usage. I pretty much never want it. If anybody has any suggestions, send them my way!

And, since I have the attention of all you Matlab-ers out there, I just got R2006a (this is the newest version). This baby rocks. It has a cool feature that allows you to execute code in little blocks called ‘cells.’ This is exactly how I debug and prototype, so it saves me a lot of highlighting and pressing [F9]. Check it out if you have the means to.

Categories: LaTeX, Matlab Tags: , ,

Trashbot

May 6th, 2006 4 comments

Trashbot ThumbnailI had a lot of fun “teaching” a Sony Aibo to find and pick up “trash.” This project uses some rudimentary computer vision, simple tracking, audio location, and neural network classification. Plus, its cute!    Click to continue →

Categories: Matlab, Projects Tags: ,

Robot Simulator

February 1st, 2006 5 comments

Robot Simulator IconThis was a quick and dirty project for a robotics class that I took at Georgia Tech in 2005. In this project, I built a Matlab simulator for testing out some simple obstacle avoidance/navigation code. My hope is that someone uses this a springboard to make a more complete version.    Click to continue →

Categories: Matlab, Projects Tags: ,

DTMF Generator

August 15th, 2003 7 comments

DTMF ThumbnailThis was a simple Matlab project, but it can be very handy for generating touch tones for telephony hacks. In fact, try it out, you can dial your phone just by holding it up to the speaker!    Click to continue →

Categories: Matlab, Projects Tags: