ginput() in opencv-python

The Image Processing Toolbox in MATLAB has this very useful utility called as ginput, which allows manually picking co-ordinate points from an image. I tried to recreate that functionality in python based OpenCV, and the result is the following code:


import cv2
import numpy as np
from matplotlib import pyplot as plt
import os

a = np.array([0,0], dtype='float32')
def getXY(imgPath):

 try:
   os.environ["DEBUG"]
   DEBUG = 1

 print "-------------------------------"
 print "Using OpenCV version : ",cv2.__version__
 print "-------------------------------"

 print "TASK : Mimic getxy() feature of MATLAB"
 print "***************************************"

 except KeyError:
    #print "For verbose mode, set DEBUG flag"
    DEBUG = 0

 #define the event
 def getxy(event, x, y, flags, param):
   global a
   if event == cv2.EVENT_LBUTTONDOWN :
      a = np.vstack([a, np.hstack([x,y])])
      if (DEBUG):
      print "(row, col) = ", (x,y)

   #Read the image
   img = cv2.imread(imgPath)
   if (DEBUG):
      print "Reading the image..."

   #Set mouse CallBack event
   cv2.namedWindow('image')
   cv2.setMouseCallback('image', getxy)
   if (DEBUG):
      print "Set MouseCallback functionality..."

   #show the image
   print "Click to select a point OR press ANY KEY to continue..."
   cv2.imshow('image', img)
   cv2.waitKey(0)
   cv2.destroyAllWindows()

   #obtain the matrix of the selected points
   b = a[1:,:]
   if (DEBUG):
      print ""
      print "The clicked points..."
      print b, b.dtype, b.shape

   print "The selected points are returned as a float64 MATRIX..."
   return b

Steps to use it :

  • save the above source code as getXY.py in your working directory.
  • In your python code, import this file, i.e.,

from getXY import *

  • Pass the image path to getXY, and start clicking on the displayed image. The no. of times you click, those many points will get saved.
  • If you want a verbose version, set the environment variable DEBUG = 1 from the terminal before running the code, i.e.,

DEBUG=1; export $DEBUG

That’s it! I’ll upload the code onto a git repo as well soon and provide a link here as well.

  • To remove the verbose code, unset the DEBUG variable, i.e, :

unset $DEBUG

Advertisements

OpenCV with video support in recent Linux distros

There were days when we had to painstakingly go through the manual install process for getting OpenCV running on our systems. Those days are long gone, and for the good. Current day distros bundle both OpenCV and ffmpeg in their repositories, making life extremely easy for enthusiasts and geeks alike. The following 4 steps provide a fully functional OpenCV install on Fedora:

  • sudo yum groupinstall “Development Tools” “Development Libraries”
  • sudo yum install eigen2-devel CTL CTL-devel OpenEXR_CTL-libs tbb yasm yasm-devel tbb-devel OpenEXR CTL-devel  gstreamer-plugins-base-devel libsigc++20-devel glibmm24-devel libxml++-devel gstreamermm xine-lib libunicapgtk-devel xine-lib-devel gstreamermm-devel python-devel sip-macros sip vamp-plugin-sdk audacity sip-devel xorg-x11-proto-devel libXau-devel libX11-devel automake libogg-devel libtheora-devel libvorbis-devel libdc1394-devel x264-devel faac-devel xvidcore-devel dirac-devel gsm-devel zlib-devel faad2-devel speex-devel lame-devel orc-compiler orc-devel libvdpau cppunit libvdpau-devel schroedinger-devel dirac x264 lame faad2 amrwb-devel opencore-amr-devel amrnb amrnb-devel
  • sudo yum install ffmpeg ffmpeg-devel opencv opencv-devel opencv-python
  • sudo updatedb

That’s it! Dont believe me, well run a sample python or c code and see for yourself! 🙂 You can download this PDF file containing a sample code written in both python and C. Provide the video file appropriately and enjoy seeing your OpenCV installation playing a video file.

P.S : For C based codes, run gcc as:

gcc -L/usr/include/opencv -lopencv_core -lopencv_highgui -lm -o a.out fileName.c

For python based codes:

python fileName.py

Video in OpenCV

Video files in OpenCV

Using Skeltrack with Kinect on Fedora 18

Skeltrack is a skeleton tracking library developed by Igalia’s Joaquim Rocha. Developed as a FOSS library, it is very well integrated with Gnome technologies, an example of which is the capability to control Gnome desktop using Skeltrack.

Here, the steps needed to get Skeltrack up and running on your fresh F18 system is detailed. First, install the dependencies using yum:

sudo yum install libfreenect* autoconf automake gobject-introspection-devel gtk-doc */cairo.h

Now we are ready to begin the actual installation. Since, libfreenect lacks the motor control capabilities, GFreenect was developed at Igalia. It is a dependency for Skeltrack, so we need to install it first using the following steps:

  • sudo updatedb
  • export LD_LIBRARY_PATH=/usr/local/lib/
  • export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig/:/usr/local/lib/pkgconfig:/usr/lib/pkgconfig
  • git clone https://github.com/elima/GFreenect.git
  • cd GFreenect && libtoolize -v –copy –install
  • aclocal
  • autoconf
  • ./autogen.sh
  • ./configure
  • make
  • sudo make install

With this, GFreenect installation should be done. You can test the installation by plugging in Kinect and executing:

  • cd <PATH_TO_GFreenect_FOLDER>/examples
  • python2.7 test*.py

You should get something like this:

GFreenect Demo

Now to install Skeltrack, similar steps are needed:

  • sudo updatedb
  • export LD_LIBRARY_PATH=/usr/local/lib/
  • export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig/:/usr/local/lib/pkgconfig:/usr/lib/pkgconfig
  • git clone https://github.com/joaquimrocha/Skeltrack.git
  • cd Skeltrack && libtoolize -v –copy –install
  • aclocal
  • autoconf
  • ./autogen.sh
  • ./configure
  • make
  • sudo make install

Thats it! Skeltrack would be up and running on your system :). To test, run:

  • cd <PATH_TO_Skeltrack_FOLDER>/examples
  • ./test-kinect.o

These steps should be enough to get us ready to use the amazing Skeltrack library.. In case you’re even more eager to play around with Kinect and want to show off your gesture skills, install and use Skeltrack-Desktop-Control :

And we’re done. To run the software:

  • cd <PATH_TO_Skeltrack_FOLDER>/src
  • ./skeltrack-desktop-control

Happy Kinect hacking! 🙂

Configure CodeBlocks to compile OpenCV codes

CodeBlocks is one of the IDE(s) that i prefer using due to its simplicity and fast interface. Plus the benefit of having MATLAB like command suggestions. The steps are outlined below:

  • Search the path where opencv is installed in your system.. you can do a :

locate cv.h

  • in my system, this returns : /usr/local/include/opencv/cv.h. Copy the path till the opencv directory, i.e : /usr/local/include/opencv/
  • Now, open CodeBlocks > New Project > (any project type of your choice.. for starters, Console Project is a good beginning) . Now goto,

Project > Build Options > Linker Settings tab

  • Under “Other Linker options” add the following, one below the other:

-lopencv_core
-lopencv_highgui
-lopencv_contrib
-lopencv_ml
-lopencv_legacy
-lopencv_imgproc
-lopencv_video
-lopencv_features2d
-lopencv_calib3d
-lopencv_objdetect
-lopencv_flann

  • Now, in the same window, under “Search directories tab”, add the opencv directory you copied above, i.e. for my system, it’ll be :

/usr/local/include/opencv/

  • Now, Project > Properties > C/C++ Parser options, again add the same directory path.

And you are done !! Enjoy compiling OpenCV codes in CodeBlocks 🙂