Friday 28 October 2011

Explanation of POWMOD pseudocode

The following is a brute-force algorithm for computing B to the power of E mod M, assuming all the input numbers have N digits with base 256.

Explanation:

Wednesday 26 October 2011

Using Kinect with OpenNI on Ubuntu 11.10

I recently upgraded to Ubuntu 11.10 and then my Kinect stopped working. I'm using OpenNI library for getting data from the Kinect.

At the beginning, when I ran the sample program NiViewer from the OpenNI libaray, I got the following error:

Open failed: failed to set USB interface.

I found the solution on OpenNI Google group discussion. You need to remove the gspca_kinect kernel module that comes with the upgrade by doing:
rmmod gspca_kinect
It seems to work even though it says "cann't find the module gspca_kinect". However, every time you restart the computer or replugin the Kinect USB, the module will be loaded again. To permanently remove the module, you can change the name of the module file:
sudo mv /lib/modules/3.0.0-12-generic/kernel/drivers/media/video/gspca/
gspca_kinect.ko /lib/modules/3.0.0-12-generic/kernel/drivers/media/video/gspca/gspca_kinect.ko.BAK

A better way may be blacklisting the module as suggested by this post. In this way, even after a kernel upgrade, the module will not be loaded.
$sudo gedit /etc/modprobe.d/blacklist.conf
Then add the following line to the end of the file and reboot:
blacklist gspcs_kinect

After this, the program could run, but I only got the RGB image data and the depth image was blank. Someone find out that this is due to the wrong use of memcpy in the OpenNI library. The line of code that causes the problem is in "/Source/OpenNI/Linux-x86/XnUSBLinux-x86.cpp" function "XN_THREAD_PROC xnUSBReadThreadMain(XN_THREAD_PARAM pThreadParam)" around line 1057. Instead of using memcpy, it should be:
memmove(pTransfer->buffer + nTotalBytes, pBuffer, pPacket->actual_length);
The difference between memcpy and memmove is that with memcpy, the destination cannot overlap the source at all, while with memmove, it can. For example, memcpy might always copy addresses from low to high. If the destination overlaps after the source, this means some addresses will be overwritten before copied. memmove would detect this and copy in the other direction - from high to low - in this case. However, checking this and switching to another (possibly less efficient) algorithm takes time.

The previous version of memcpy in libc6 may not make the difference so distinct, but in the new updated version of libc6 version 2.13, it might copy memory backward in some conditions, which causes issues if the source and destination overlap. You can read more about the changes in the new version in "/usr/share/doc/libc6/NEWS.Debian.gz".

Saturday 22 October 2011

Update ruby to 1.9.2 on Ubuntu 11.10

$sudo apt-get remove ruby rubygems ruby1.8 ruby1.8-dev ruby1.8-full
$sudo apt-get install ruby1.9.1-full
$sudo update-alternatives --set ruby /usr/bin/ruby1.9.1
$sudo env REALLY_GEM_UPDATE_SYSTEM=1 gem update --system

Tuesday 4 October 2011

Desktop Recorder for Ubuntu and Conversion

A good desktop recording software for Ubuntu is gtk-recordMyDesktop. Installation and usage instructions can be found here. To select a portion of the desktop to record, click Select Window button. Next click the mouse, hold and drag to select the portion in the preview window (note you don't do this on the actual desktop). It may be easier to instead right-click on the red circle in the system tray and click Select Area On Screen. In this way, you can select the portion actually on the desktop.

The video produced is in .ogv format. To convert the video format, you can use mencoder. You can install it using the command:
$ sudo apt-get install mencoder
To convert the recorded out.ogv file to recorded.avi of AVI format, you can use the following command:
$ mencoder out.ogv -o recorded.avi -oac mp3lame -ovc lavc
-ovc sets the video encoder. lavc refers to libavcodec.