XMLEdit plugin for Vim

XMLEdit is a useful plugin for Vim if you have to examine or edit XML files at the commandline. Its source can be downloaded and installed from here.

Some of the features of this plugin:

  • Jump between opening and closing tag by pressing \%
  • Jump between open bracket and close bracket by pressing %
  • Comment or uncomment a XML block

Tried with: Vim 7.4 and Ubuntu 14.04

About these ads

How to enable mouse in TTY

Mouse support is not enabled by default in TTY (Ctrl+Alt+F1) mode in Ubuntu. This is a shame since many console applications like Vim or Htop have mouse support even when used at the TTY.

Enabling mouse support in TTY is easy. Just install the General Purpose Mouse interface package:

$ sudo apt install gpm

No restart is needed since the package installation also starts the gpm server. Your mouse should start working immediately in TTY mode.

Tried with: GPM 1.20.4 and Ubuntu 14.04

Error: Can not get new positive sample


I was in the process of training a cascade classifier. I had exactly 354 positive samples and yet I got this error from opencv_traincascade when it entered stage 2:

$ opencv_traincascade -data car-classifier -vec pos-samples.vec -bg neg-image-filepaths.txt -precalcValBufSize 256 -precalcIdxBufSize 256 -numPos 354 -numNeg 4000 -nstages 20 -minhitrate 0.999 -maxfalsealarm 0.5 -w 50 -h 50 -nonsym -baseFormatSave 
numPos: 354
numNeg: 4000
numStages: 20
stageType: BOOST
featureType: HAAR
sampleWidth: 50
sampleHeight: 50
boostType: GAB
minHitRate: 0.995
maxFalseAlarmRate: 0.5
weightTrimRate: 0.95
maxDepth: 1
maxWeakCount: 100
mode: BASIC

===== TRAINING 0-stage =====
POS count : consumed   354 : 354
NEG count : acceptanceRatio    4000 : 1
Precalculation time: 100
|  N |    HR   |    FA   |
|   1|        1|        1|
|   2|        1|        1|
|   3|        1|        1|
|   4|        1|        1|
|   5| 0.997175|  0.40525|
Training until now has taken 0 days 1 hours 39 minutes 31 seconds.

===== TRAINING 1-stage =====                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
OpenCV Error: Bad argument (Can not get new positive sample. The most possible reason is insufficient count of samples in given vec-file.
) in get, file opencv-2.4.9/apps/traincascade/imagestorage.cpp, line 162
terminate called after throwing an instance of 'cv::Exception'
  what():  opencv-2.4.9/apps/traincascade/imagestorage.cpp:162: error: (-5) Can not get new positive sample. The most possible reason is insufficient count of samples in given vec-file.
 in function get


This problem seems to be very common. One possible solution is here. I took the hint from that and reduced the number of positive samples I provided as input to the program. Note that you need more positive samples to be present in the .vec file, just pass a smaller number to the program so it has some left over when it starts stage 1.

Here is the modified invocation I used:

$ opencv_traincascade -data car-classifier -vec pos-samples.vec -bg neg-image-filepaths.txt -precalcValBufSize 256 -precalcIdxBufSize 256 -numPos 200 -numNeg 2000 -nstages 20 -minhitrate 0.999 -maxfalsealarm 0.5 -w 50 -h 50 -nonsym -baseFormatSave

Tried with: OpenCV 2.4.9 and Ubuntu 14.04

Task Coach

Task Coach is an offline todo or task list management tool. It is available for Ubuntu, other Linux distros and even Android. It just saves a single .tsk XML file that you can place in your Dropbox for access across multiple computer and devices.

To install in Ubuntu:

$ sudo add-apt-repository https://launchpad.net/~taskcoach-developers/+archive/ppa
$ sudo apt-get update
$ sudo apt install taskcoach

Tried with: Task Coach 1.3.36 and Ubuntu 14.04

How to calibrate camera using OpenCV

Whether you are using a RGB or a depth camera in your setup, you will first need to calibrate it to be able to undistort the resulting image and to obtain 3D coordinates of depth values. These are the steps I followed to do this:

  • If you are using a depth camera, it should have an accompanying RGB camera. Make sure that the depth camera is setup to be registered with the RGB camera. For example, this can be done in OpenNI using setImageRegistrationMode(IMAGE_REGISTRATION_DEPTH_TO_COLOR).

  • Prepare the setup you will use for capturing RGB or depth images. The placement of the camera and the objects should not be disturbed during calibration and later capture stages.

  • Print out a chessboard image to A4 paper. For example, I use this chessboard image. Paste or fix the printed chessboard to a board or pad.

  • Hold or place this board in various orientations and positions in the frame of the camera. Take one image for each position. Take at least 10 or 20 of these images.

  • Go ahead and do the capture of your experiment next. This will result in capturing a lot more images.

  • To obtain the calibration parameters, we use the camera calibration example code provided along with OpenCV. I have created a CMake project of this code here. Build the program.

  • The camera calibration program needs two XML files as input. They can be named anything you wish. I have provided samples of these files in config.xml and images.xml here.

  • config.xml holds the other details for calibration. BoardSize_Width and BoardSize_Height is the number of squares along the width and height of the board. Square_Size is the length (in mm) of a square on the chessboard. You will need to measure this from the printed chessboard. Input specifies the file which has the input images, which is images.xml in our case. Calibrate_NrOfFrameToUse is the number of images to use from that file for calibration. Write_outputFileName is the name of the output XML file from the calibration.

  • images.xml holds the paths to the chessboard calibration images we captured. Even if you intend to capture depth images for your experiment, you need the chessboard images from the RGB camera for the calibration step here.

  • Call the calibration program as:

$ ./calibrate-camera config.xml

The output will be written to output.xml. This file can be used as input in your OpenCV program to reconstruct the data from your captured images.

Reference: Camera calibration With OpenCV

Tried with: OpenCV 2.4.9 and Ubuntu 14.04

How to train OpenCV cascade classifier

OpenCV ships with an application that can be used to train a cascade classifier. The steps to prepare your data and train the classifier can be quite elaborate. I have detailed the steps that I used below to train the classifier to identify an object (say car):

  • Two OpenCV programs: opencv_createsamples and opencv_traincascade will be used for this process. They should be installed along with your OpenCV. You can also compile them while compiling OpenCV from source. Make sure to enable the option BUILD_APPS in the CMake GUI. This is the option that builds these applications.

  • Prepare images that have one or more instances of the object of interest. These will be used to generate positive samples later. Also, prepare images that do not have the object of interest. These will be used to generate negative samples later.

  • It is mandatory to use opencv_createsamples to generate the positive samples for opencv_traincascade. The output of this program is a .vec file that is used as input to opencv_traincascade.

  • If you have image patches of the object and need to generate positive sample images by applying transformations (rotations) on this in 3 dimensions and then superimposing it on a background, you can do that using opencv_createsamples. See its documentation for details.

  • In my case, I already had one or more instances of the object captured with its actual background. So, what I instead had to do was to indicate the patch rectangles in each image where my object was located. You can do this by hand or by writing a simple OpenCV program to display image to you and you mark out the rectangles on the objects and stores these values in a text file. The format of this text file required by opencv_createsamples can be seen in its documentation.

  • To create the positive samples file, I invoked opencv_createsamples as:

$ opencv_createsamples -info obj-rects.txt -w 50 -h 50 -vec pos-samples.vec

Here, obj-rects.txt is a text file that has the information of the rectangles where the object is located in each image. See step above for details. The output of this program is stored in pos-samples.vec.

  • To view the positive samples that have been created by this program:
$ opencv_createsamples -vec pos-samples.vec -w 50 -h 50

Note that it switches to viewing mode when you only provide these three parameters and their values should match what you provided to create the positive samples.

  • Now we can train the cascade classifier. The details of its parameters can be seen in its documentation. I used this invocation:
$ opencv_traincascade -data obj-classifier -vec pos-samples.vec -bg neg-filepaths.txt -precalcValBufSize 2048 -precalcIdxBufSize 2048 -numPos 200 -numNeg 2000 -nstages 20 -minhitrate 0.999 -maxfalsealarm 0.5 -w 50 -h 50 -nonsym -baseFormatSave

obj-classifier is a directory where we are asking the classifier files to be stored. Note that this directory should already be created by you. pos-samples.vec is the file we generated in step above. neg-filepaths.txt is a file with list of paths to negative sample files. 2048 is the amount of MB of memory we are requesting the program to use. The more the memory, the faster the training. 200 is the number of positive samples in pos-samples.vec. This number is also reported by opencv_createsamples when it finishes its execution. 2000 is the number of negative sample image paths we have specified in neg-filepaths.txt. 20 is the number of stages in the classifier we wish. 50x50 is the size of object in these images. This should be same as what was specified with opencv_createsamples. 0.999 and 0.5 are self-explanatory. Details on all these parameters are found in the documentation.

Tried with: OpenCV 2.4.9 and Ubuntu 14.04