STAIR Vision Library

Coding Tips and Tricks

C++

Reserving space in an stl::vector
If you're going to be pushing a lot of data onto an stl::vector then it is often more efficient to reserve the approximate amount of elements required ahead of time:
vector myVec;
myVec.reserve(10000);
Note that this is different to vector myVec(10000); which actually allocates a vector of size 10000.

Sorting a vector and keeping a reverse index

vector<double> myVec = ...;
vector<pair<double, int> > sortedVec;
sortedVec.reserve(myVec.size());
for (unsigned i = 0; i < myVec.size(); i++) {
    sortedVec.push_back(make_pair(myVec[i], i));
}
sort(sortedVec.begin(), sortedVec.end());
for (unsigned i = 0; i < sortedVec.size(); i++) {
    assert(myVec[sortedVec[i].second] == sortedVec[i].first);
}

Using stl containers
Make sure you define both the assignment operator and copy constructor.

Perl

Sorting a vector and keeping a reverse index
my @myVec = ( ... );

my %reverseIndex = ();
@sortedVec = sort { $a <=> $b } @myVec;
for (my $i = 0; $i <= $#sortedVec; $i++) {
    $reverseIndex{$sortedVec[$i]} = $i;
}

Shell

Finding all source files containing 'foo'
find . -name "*.cpp" -exec grep -H foo {} \;

Changing the extension of a bunch of files

foreach i (`ls *.oldExt`)
  mv $i $i:r.newExt
end

Using a different version of the compiler to build SVL

make "CCC=g++-4.1"

Matlab

Processing files
files = dir([BASEDIR, '/*.jpg']);
for i = 1:length(files),
  disp(['Processing ', files(i).name, '...']);
  img = imread([BASEDIR, '/', files(i).name]);
  imagesc(img);
  pause(1);
  drawnow;
end;

Adding SVL code to the Matlab Path
You can add the STAIR Vision Library to the Matlab path so that you can call SVL scripts and MEX functions from any directory. Once you start Matlab run the command: run('<BASEDIR>/svl/scripts/addSVLPaths'); where <BASEDIR> is the SVL installation directory.