Image stabilization using OpenCV

This application deals with video of neural recordings. In such recordings, feature sizes are small. On top of it, recordings are quite noisy. Animal head movements introduces sharp shakes. Out of the box video stabilizer may not work very well on such recordings. Though there are quite a lot of plugins for ImageJ to do such a work, I haven’t compared their performance with this application. This application is hosted here https://github.com/dilawar/video_stabilizer and a demo video is available on youtube here  https://www.youtube.com/watch?v=vGjIFvzOOQ8 .

The summary of basic principle is following:

0. Collect all frames in a list/vector.

1. Use bilateral filter to smooth out each frame. Bilateral filter smoothens image without distorting the edges (well to a certain extent).

2.  Calculate optical flow between previous frame and current frame. This is a proxy for movement. Construct a transformation and store them in a vector. OpenCV function `goodFeatureToTrack` does almost all the work for us.

  1. Take average of these transformations and apply it on each frame of original recording; that’s correct motion.
Advertisements

Using mutt – saving password and writing mail in markdown

If I were to be forced to replace my mutt with some other general purpose email client I’d prefer thunderbird with plugin muttator.

If you prefer easy to setup and click-based tools then you will not find mutt very exciting, however those who loves to split hair and tweak their tools to their whims will find much in mutt to please them. Learning the full power of mutt takes time and some sort of persistence. This post is not intended for first time user. It is meant for those who have tried their hands on mutt. In the end, there are some link which can help who wants to start using mutt. I had the fleeting temptation to combine my scripts in one which can set up mutt automatically for arbitrary no of accounts, but then it beats the whole purpose of tweaking the tool.

Storing password

Mutt stores password in plain text format. To avoid this, I used gpg to encrypt a file in which password are stored. For example, a simple text file is created to store the password.

export EE_NAME="Dilawar Singh"
export EE_USER=dilawar
export EE_PASS=password1

This has export command which set an environment variable. Now I encrypt the file using gpg

gpd -c password.txt

It will ask for a pass-phrase. When decrypting the file, you have provide the same pass-phrase. Passphrase are easy to remember and hard to crack. If I were Gabbar Singh, I would have chose something like thakur a per katne chahiye the. Hard to guess and easy to remember, isn’t it? An encrypted file will be created by gpg default name password.txt.gpg. Mutt can read environment variables, let’s exploit this fact. We write a wrapper around the mutt command. Let’s name this script my_mutt or after the name of your pet doggie. And chmod +x my_mutt so it is executable. Store it somewhere like /usr/local/bin.

#!/bin/bash
pwds=`gpg -d ~/.mutt/password.txt.gpg`
eval $pwds
mutt -F ~/.mutt/mutt_server "$@"

Second line decrypt the encrypted file, text of the file is stored in pwds variable and this text is executed by eval command. The environment variables are set. We invoke mutt in this shell so that it can access environment variables EE_USER, EE_PASS etc. On line 4, -F ~/.mutt/mutt_server says that this file is my config file which mutt must read. The default configuration file is ~/.muttrc.

In the configuration file, write something like this. You need to write some more to setup mutt. This is only fragment which is relevant to one account.

# ~/Scripts/mutt/ee
set from="$EE_NAME $EE_USER@ee.iitb.ac.in"
set hostname="imaps://sandesh.ee.iitb.ac.in"
set folder="imaps://sandesh.ee.iitb.ac.in"
set imap_user="$EE_USER"
set imap_pass="$EE_PASS"
set record="+mail/Sent"
set postponed="=Drafts"
set spoolfile="imaps://sandesh.ee.iitb.ac.in/INBOX"
set signature="sign.txt"
set smtp_url="smtp://$GPO_USER@smtp-auth.iitb.ac.in:25"
set stmp_pass="$GPO_PASS"
set realname="$REAL_NAME"
my_hdr Organization: EE, IIT Bombay, http://www.ee.iitb.ac.in

In my personal opinion, this is a good solution to store password on machine unless someone has a better solution which does not involve using a green lantern, nanobots or ability of code-bending.

Writing HTML emails

Mutt works great with text emails. But if you want to write emails for wncc group where code has to be written inline, html can help. See this question for potential solutions. I have also written mine.

Resources:

  1. Mutt wiki is a good reference guide.
  2. My first mutt
  3. And this terminal lover blog

Mail server info of National Center for Biological Sciences (NCBS) Bangalore

If you are a mutt user and working at NCBS Bangalore, you can setup your account in mutt as following.


set from="REAL_NAME "
set from="USER NAME "
set hostname="imaps://imap.ncbs.res.in"
set folder="imaps://imap.ncbs.res.in"
set record="+INBOX.Sent"
set imap_user="NCBS_USER"
set imap_pass="NCBS_PASS"
set postponed="=Drafts"
set spoolfile="+INBOX"
set signature="sign.txt"
set smtp_url="smtp://NCBS_USER@mail.ncbs.res.in" smtp_pass="$NCBS_PASS"
set realname="$REAL_NAME"

In short incoming server is imap.ncbs.res.in and outgoing server is mail.ncbs.res.in. Use default ports.

To get smtp server information, you can always use (in terminal)

msmtp -v --host=mail.ncbs.res.in --serverinfo --tls=on --tls-certcheck=off --port 25

Setup in BlueMail or other Android clients

For most clients, following settings should work.

IMG-20170811-WA0004

IMG-20170811-WA0003-1

Configuring mutt to display ‘attachment missing warning’

This method works if you are using vim as your editor. In file ~/.vim/ftplugin/mail.vim, write this function definition.

 
function! CheckAttach()
     let check='attach,angehängt,attachment,Anhang'
     let oldPos=getpos('.')
     let ans=1
     let val = join(split(escape(check,' \.+*'), ','),'\|')
     1
     if search('\%('.val.'\)','W')
       let ans=input("Attach file?: (leave empty to abbort): ", "", "file")
       while (ans != '')
               normal magg}-
               call append(line('.'), 'Attach: '.ans)
               redraw
           let ans=input("Attach another file?: (leave empty to abbort): ", 
", "file")
       endwhile
     endif
     exe ":write ". expand("<amatch>")
     call setpos('.', oldPos)
 endfu
 augroup script
     au!
     au BufWriteCmd,FileWriteCmd mutt* :call CheckAttach()
 augroup END

And in .muttrc, you should have something like this.

set editor = "vim -c 'set spell spelllang=en syntax=mail ft=mail enc=utf-8' '+/^$'"

Notice that we have ft=mail. This will automatically load the funtion.

Reference

1. http://www.mail-archive.com/mutt-users@mutt.org/msg37580.html

Or you can use this script.. Remove the above line from .vimrc.

Debugging : A case for ncurses-based console

Where would you log your debug messages when you are designing an algorithm?

I tried various things. Used various libraries (apache’s log4cxx is very powerful) for logging and even used database (sqlite3 is my all time favorite for this) to store all data as well as debug messages. Problem with sqlite3 is that one has to type a lot but it is a very worthy investment if one needs to process a great deal of data later. Of course, one can write test-routines in the code itself and later disable them altogether once application is tested. This seems to be a standard practise. But many times one has to look at the data by oneself and in such cases logging is essential.

One must follow a standard format while writing logging messages. Readability is of great importance. What is the use of message which are hard to read. Prettyprinting requires work and often increases the size of code. But one has to do it, there is no escape. Apache logging framework format is pretty good. One can even write logs to XML file. Beware that XML is not easy to read by humans. Computers, on the other hand, can process xml easily. If logs are too big, I’d suggest go for either XML files or sqlite3 database. SOCI’s soci library is a good library in C++ for sqlite3.

For medium or small debugging, writing to simple text files or console is good enough. And one usually design his algorithm in steps. Therefore one can concentrate logs generated by a section of program. In C++, compiler macros are very useful. For example, if I am testing functionA, then I write its debug messages between #ifdef DEBUG_FUNA   #endif and let the compiler know if I wants to activate or not.

I wish to suggest something more. If you are a big fan of terminal (no gui, no mouse, no problem) and have a look at ncurses library. It creates a text based user interface and I have used it in two applications. To cut the long story short, let me show you the screen-shot of how ncurses based screen can be split into many section in which different section can be used for different type of log messages.

2013-02-17-033243_1366x768_scrot

Even this is lame. Let me show you a video. First few seconds to this video will show you the blindness caused by naive logging on the console. Then we’ll see the power of ncurses. You’ll see two matrices, we wish the left one must have rows which has only single 1. If this is achieved, our algorithm is correct.

https://www.facebook.com/video/video.php?v=109918892526120&saved