Thursday, December 13, 2012

Ogre3D Object Placement Utility


I wrote an open source object placement utility that allows for placing objects (trees) on a terrain using the mouse.


It consists of a terrain and a water surface (pond). The user can select from 3 different models of trees and place them on the terrain. You enter edit mode by pressing space and a toolbar with all available models appears on the left side of the screen. The toolbar can easily be extended with more models.

I hope people will find it useful for object placement in scenes or using it as a base for terrain editors and such.

The build system is currently for Linux only (cmake and make) but it should be easy to compile in Windows or MacOS. The code is not perfect so any comments and suggestions are welcome.


Here is a demo:


Links
Souce code on GitHub
Object Placement Utility at Ogre3D Forums

Thursday, October 11, 2012

Icon Action Sheet for iOS 6

One of the new features of iOS 6 is the replacement of the button based action sheet with the new icon  action sheet. If you tap the options for a library photo for example you get icons for emailing the photo, posting it on Facebook, tweeting it, etc. But how can you integrate this feature on your app?

Github user jgrana recently posted an open source reusable class that does exactly this; it allows you to easily reproduce this control in your app.


Links: IconActionSheet on GitHub

Wednesday, May 16, 2012

OpenCV 2.4 on Ubuntu 12.04

Edit (August 2013): Just used these steps to install OpenCV 2.4.6.1 on Ubuntu 12.10 without problems.

OpenCV 2.4.0 was released on May 2012 providing, a lot of improvements and bug fixes. For a complete change log see here.

This post will hopefully make your life easier to configure, compile, install and test it out.

This time I tried to categorize and explain the dependencies, so that it is easier to configure it for your needs. As always, comments and suggestions are welcome.

Ubuntu 12.04 provides a package of OpenCV 2.3.1 that you can easily install by typing:
sudo apt-get install libopencv-dev
If you do not care about having the latest version you could skip the rest of the post.

Install Dependencies


Essentials
These are libraries and tools required by OpenCV.
sudo apt-get install build-essential checkinstall cmake pkg-config yasm

Image I/O
Libraries for reading and writing various image types. If you do not install then the versions supplied by OpenCV will be used.
sudo apt-get install libtiff4-dev libjpeg-dev libjasper-dev


Video I/O
You need some or all of these packages to add video capturing/encoding/decoding capabilities to the highgui module.
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev

Python
Packages needed to build the Python wrappers.
sudo apt-get install python-dev python-numpy

Other third-party libraries
Install Intel TBB to enable parallel code in OpenCV.
sudo apt-get install libtbb-dev

GUI
The default back-end for highgui in Linux is GTK. You can optionally install QT instead of GTK and later enable it in the configuration (see next section). 
sudo apt-get install libqt4-dev libgtk2.0-dev

Compile and Install

Get a copy of the source code here, extract and create a build directory:
tar -xvf OpenCV-2.4.0.tar.bz2
cd OpenCV-2.4.0/
mkdir build
cd build
Configure using CMake. You have a lot of options in this step. This is what I use:
cmake -D WITH_QT=ON -D WITH_XINE=ON -D WITH_OPENGL=ON -D WITH_TBB=ON -D BUILD_EXAMPLES=ON ..
Notice, that by adding the -D WITH_QT=ON, the highgui module will use QT instead of GTK. If you want to go with GTK just remove this. For more information on the options, look at the CMakeLists.txt file. When you are happy with the configuration you have, you can start compiling:
make
If compilation finishes without errors, you can install by saying:
sudo make install
Finally, make sure that your programs can link to the OpenCV library in run-time by adding the following line at the end of your /etc/ld.so.conf:
/usr/local/lib
And then configure dynamic linker run-time bindings:
sudo ldconfig

Testing

An easy way to test that the compilation went well is to use the OpenCV test utilities. For example, to test the core module go to OpenCV-2.4.0/build/bin and run:
 ./opencv_test_core
You should see something like that:


Note that some failures when testing other modules may come from missing image files. To correctly run these tests or samples you should move the corresponding image files from OpenCV-2.4.0/samples to OpenCV-2.4.0/build/bin.

For testing that you can compile your own programs and link against the installed OpenCV libraries I have packaged the face detection sample with all the necessary files and a simple Makefile. Download it here, extract and type:
make
This should compile and run with a test image, so you should see something like this:



Monday, April 23, 2012

Elapsed Time for Shell Scripts

A lot of times you want to calculate the time it takes for a part of a shell script to run. Here is one of the ways you can do this:
T1=$(date +%s)
# Do something here
T2=$(date +%s)
diffsec="$(expr $T2 - $T1)"
echo | awk -v D=$diffsec '{printf "Elapsed time: %02d:%02d:%02d\n",D/(60*60),D%(60*60)/60,D%60}'
This will tell you the elapsed time between setting T1 and setting T2. The output is formatted as HH:MM:SS and printed to the console. You can easily redirect to a file for logging.

Sunday, February 26, 2012

Using FFMPEG



FFMPEG is a complete, cross-platform solution to record, convert and stream audio and video. It includes libavcodec; the leading audio/video codec library. FFMPEG is a free software project and it is published under the GNU Lesser General Public License 2.1+ or GNU General Public License 2+ (depending on which options are enabled).

To install FFMPEG in Ubuntu just type:
sudo apt-get install ffmpeg

This post contains a collection of useful examples of using FFMPEG from the command line.

Video Information
Get information on the video (codec, frame rate, etc.).
ffmpeg -i myvideo.avi

Video Encoding
To encode the video using a different codec we specify it using the -vcodec option.
ffmpeg -i myvideo.avi -vcodec mpg4 video_out.avi

Now let's say we want a better quality compression and a different frame rate from the original. We specify 1Mb/s bitrate using the -b option (default is 200kb/s) and we change the frame rate to 12 fps using the -r option.
ffmpeg -i myvideo.avi -vcodec mpg4 -b 1M -r 12 video_out.avi
Other values for the -vcodec option include:
copy:
 Copy raw codec data as is (do not reencode)
rawvideo:
 Raw video (uncompressed)
xvid:
 XviD (MPEG-4 Part2)
wmv2:
 Windows Video Media

Extracting Video Frames
The following example extracts the frames from the video and saves them as frame_001.png, frame_002.png,...
ffmpeg -i myvideo.avi -f image2 frame_%03d.png

Cropping a video to a specific time interval
The following example extracts a scene from the input video that start at 1 minute 45 seconds and has a duration of 30 seconds.
ffmpeg -i myvideo.avi -ss 00:01:45 -t 00:00:45 myscene.avi

Slow down or speed up a video
Let's say we need to slow down a video now. Unfortunately, decreasing the frame rate with the -r option will not do, since ffmpeg will drop frames to keep the duration of the video the same. We need to use mjpegtools to do that. To install mjpegtools type:
sudo apt-get install mjpegtools
For this example the original video has a frame rate of 30fps and we want to make it play at 1/3 of the speed. So we have to trick ffmpeg to believe that the original video had a 10fps frame rate. The yuvfps tool does exactly that. Make sure that the input frame rate (-s option) and the output frame rate (-r option) are the same.
ffmpeg -i myVideo.avi -f yuv4mpegpipe - | yuvfps -s 10:1 -r 10:1 | ffmpeg -f yuv4mpegpipe -i - -vcodec mpeg4 -b 12M  mySlowVideo.avi
Note that this will not work unless your video has one of the yuv pixel formats. If your pixel format is not compatible (rgba for example) you would have to convert it like this:
ffmpeg -i myVideo.avi -pix_fmt yuv420p myVideo2.avi

Monday, September 26, 2011

OpenCV 2.3.1 on Ubuntu 10.10 or later

*Update: For OpenCV 2.4 on Ubuntu 12.04 see my latest post.

This post will guide you on how to compile and run OpenCV 2.3.1 on Ubuntu 10.10 or later.

First, install all prerequisites:
sudo apt-get install build-essential checkinstall git cmake ffmpeg libfaac-dev libjack-jackd2-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libsdl1.2-dev libtheora-dev libva-dev libvdpau-dev libvorbis-dev libx11-dev libxfixes-dev libxvidcore-dev texi2html yasm zlib1g-dev libtbb-dev libv4l-dev libopenexr-dev libunicap2-dev libavformat-dev libswscale-dev libdc1394-22-dev libgstreamer0.10-0 libgstreamer0.10-dev gstreamer0.10-tools gstreamer0.10-plugins-base libgstreamer-plugins-base0.10-dev gstreamer0.10-plugins-good gstreamer0.10-plugins-ugly gstreamer0.10-plugins-bad gstreamer0.10-ffmpeg python-numpy
This is not a minimal set of libraries, but it's a collection that will give you most of the functionality. For example, if you are sure that you will never need to use a firewire camera, you can ignore libdc1394-22-dev. But if you actually need to use this feature you will have to recompile OpenCV. Also, if you need CUDA support, follow the instructions in this post before continuing here.

Download OpenCV:
wget downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.3.1/OpenCV-2.3.1a.tar.bz2
Extract and create a build directory:
tar -xvf OpenCV-2.3.1a.tar.bz2
cd OpenCV-2.3.1/
mkdir build
cd build
Now run cmake:
cmake -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=OFF -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON ..
At this point check the output of cmake to make sure that all the features you are going to need are supported. If not you will have to install the corresponding libraries and run cmake again. Compile:
make
If everything went well you can go ahead and install:
sudo make install
Finally, add the following lines in your .bashrc:
# OpenCV stuff
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
To test that everything works, let's run the face detection demo. Go to the bin directory:
cd bin
And run:
./facedetect --cascade="../../data/haarcascades/haarcascade_frontalface_alt.xml" --nested-cascade="../../data/haarcascades/haarcascade_eye.xml" --scale=1.3 ../../samples/c/lena.jpg
You should see something like this:


How to install CUDA 4.0 on Ubuntu 10.10 or later

First, you need to install the NVIDIA development drivers. Download the current version (270.41.19) here.
To install the dev drivers, reboot and go into recovery mode. Select the "Drop to root shell prompt" option. Now type:
sudo telinit 3
Login again using your username, then cd to the directory where you downloaded the drivers (e.g. ~/Downloads):
cd ~/Downloads
chmod u+x devdriver_4.0_linux_64_270.41.19.run
sudo ./devdriver_4.0_linux_64_270.41.19.run
Follow the instructions on the screen. Make sure you write to your xorg.conf file. When done reboot:
sudo reboot
Download the CUDA Toolkit from this page (Linux -> CUDA Toolkit for Ubuntu Linux 10.10).
cd ~/Downloads
chmod u+x cudatoolkit_4.0.17_linux_64_ubuntu10.10.run
sudo ./cudatoolkit_4.0.17_linux_64_ubuntu10.10.run
Finally, download the GPU Computing SDK from the above website.
cd ~/Downloads
chmod u+x gpucomputingsdk_4.0.17_linux
./gpucomputingsdk_4.0.17_linux
Add the these lines at the end of your ~/.profile (if your OS is 32-bit, remove the lib64 entry):
#CUDA stuff
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib:/usr/local/cuda/lib64:$LD_LIBRARY_PATH
Now you just need to compile the samples. Assuming you installed the SDK in your home directory:
cd ~/NVIDIA_GPU_Computing_SDK
make
You can now run one of the demos, e.g. the particle demo
~/NVIDIA_GPU_Computing_SDK/C/bin/linux/release/particles
and you should see something like this: