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):

   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.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)

   #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 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


Its AOSP (Android Open Source Project) time…

For the next few months, we shall concentrate on AOSP, both the application framework and its internals. We shall also see integration of OpenCV applications into the AOSP framework, and explore the power that a Linux device gives to its owner.

Its AOSP time… !!!

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:


Video in OpenCV

Video files in OpenCV

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:


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


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

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

Install OpenCV 2.3 with video support in Fedora 15

Much has changed since OpenCV 2.0.. android support, CUDA support, etc are up and running. So is the change in the method of installing OpenCV. So, i though to quickly jot down the steps to follow for a painless install.

I must add, due to changes in gcc, removal of libv4l support from linux kernel, etc.. OpenCV 2.1 and 2.2 give loads of issues during install. I just couldnt manage installing v2.1, though v2.2 needed few hacks as mentioned in the link : . So, for people wanting to install v2.2, can follow exactly same steps mentioned below after making the above hack. Note that, even UVC-webcams dont work with v2.2. So, if you’re like me, needing lots of webcam feed for vision-based tasks, its best to avoid v2.2. Moral of the story : Go for v2.3..

1. Install the following :

sudo yum install eigen2-devel, CTL, CTL-devel, OpenEXR_CTL-libs, tbb, yasm, yasm-devel, tbb-devel, OpenEXR CTL-devel, OpenEXR CTL, perl-URI, perl-Compress-Raw-Zlib, perl-Compress-Raw-Bzip2, perl-l0-Compress, libucil-clevel, perl-HTML-Tagset, perl-HTML-Parser, perl-Iibwww-perl, perl-XML-Parser, 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

sudo yum install libXext-clevel, glib2-devel, libXrender-devel, libXfixes-devel, Iibunicap, freetype-devel, boost-system, fontconfig-devel, libpng-devel, boost-filesystem, boost-serialization, boost-thread, boost-date-time, boost-regex, libXt-devel, libXdamage-devel, libXcomposite-devel, libXcursor-devel, libXrandr-devel, atk-devel, libXinerama-devel, libXxf86vm-devel, libXi-devel, libxml2-devel, blas-devel, libjpeg-turbo-devel, pixman-clevel, cairo-devel, pango-devel, libdrm-devel, mesa-libGL-devel, boost-graph, boost-wave, libucil, boost-signals, boost-python, boost-iostreams, boost-program-options, boost-random, boost-test, boost, mesa-libGLU-devel, ilmbase-devel, libunicapgtk, gdk-pixbuf2-devel, libibverbs, libmlx4, librdmacm, check, check-devel, boost-jam, rarian, rarian-compat, cmake, plpa-libs, numactl, environment-modules, boost-devel, boost-build, OpenEXR-devel, jasper-devel, lapack-devel, libunicap-devel, libtifl-devel, atlas-devel, openmpi, boost-openmpi, ucview, gstreamer-devel, gtk2-devel, jasper, 0penEXR

sudo yum install xorg-x11-proto-devel, libXau-devel, Iibxcb-devel, libX11-devel, libram1394-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

(i have tried copying the package names via OCR software and some editing as much as could be possible from the screenshot below)


Install from Add/Remove Programs (PackageKit)

List of Packages to install for OpenCV dependency resolution

2. Now, download and Install FFMPEG

  • Eventhough FFMPEG is at v0.8.. its best to go for v0.7.. thats coz of several dependency issues. Many things like VLC, etc are based on ffmpeg v0.69 code, and v0.8 MAY give lots of issues..
  • Download ffmpeg 0.7 from :
  • extract the above file (say files get extracted to /home/user/ffmpeg)
  • Open terminal and cd to above location.. i.e. : cd /home/user/ffmpeg
  • Now type the following :

./configure –enable-gpl –enable-version3 –enable-nonfree –enable-postproc –enable-libfaac –enable-libopencore-amrnb –enable-libopencore-amrwb –enable-libtheora –enable-libvorbis –enable-libxvid –enable-x11grab –enable-swscale –enable-shared


sudo make install

  • And you are done.. FFMPEG is ready to be integrated with OpenCV.

3. Download OpenCV 2.3 . Extract to a folder (say /home/user/Opencv-2.3)

  • There still are a few packages of ffmpeg like libavcodec-dev, libavutil-dev, etc which need to be present in /usr/local/ffmpeg/ . But, the above  install of ffmpeg doesnt create this. So, open Add/Remove Programs and install following or install using yum:

sudo yum -y install ffmpeg ffmpeg-devel ffmpeg-libs

  • Now, the requisite libraries are all present in the location OpenCV can find them. Open terminal and cd to the OpenCV folder.. i.e.

cd /home/user/OpenCV-2.3

  • Create a folder called Release

mkdir Release

  • cd to the Release folder and type the following to install OpenCV

cd Release/


make -j 2

sudo make install

  • Few more steps need to be done to ensure flawless build.

sudo gedit /etc/

(Type the following and save-close the file) : /usr/local/lib

sudo ldconfig

sudo gedit /etc/bashrc

(Type the following and save-close the file) :


And you are done !!! 🙂 OpenCV 2.3 with ffmpeg support is ready to be used on Fedora 15 with full webcam support.. ENJOY !!

NOTE : due to requests for Ubuntu specific steps, i’ll put the changed steps in this post itself in a few days

Install OpenCV in Linux with video (ffmpeg) support

Faced with the task of running Computer Vision problems in Linux environment, i started googling for the methods.. Though i found the install steps at many places, but my install always failed, getting stuck while doing a make operation on the source files.

Finally, i managed to install it from source.. I am listing the steps here, with a special thanks to all the people, their blogs and google for helping me succeed in installing OpenCV.

  • Download ffmpeg
  • Extract contents to a folder. do a cd to the folder location in the terminal. Build ffmpeg as follows (as root) :
  • su -c
  • ./configure –enable-shared
  • make all
  • make install
  • The –enable-shared option during configuration ensures compatibility between LGPL licence of ffmpeg contents and BSD licence of OpenCV.
  • ADDITIONAL IMPORTANT STEPS (before installing OpenCV)
  • goto the folder where library files of ffmpeg have got installed in your system. you can do a “locate ffmpeg” to know that. for e.g, in my case, its /usr/include/ffmpeg/
  • Now, type in the follwing (as root):
  • ln -s libavcodec/avcodec.h avcodec.h
  • ln -s libavformat/avformat.h avformat.h
  • ln -s libavformat/avio.h avio.h
  • ln -s libavutil/avutil.h avutil.h
  • ln -s libswscale/swscale.h swscale.h
  • Download latest build of OpenCV 2.1 or OpenCV 2.0
  • install swig in your version of linux.. its available in the repos of all the distros. Swig is the application that interacts between different programming languages like C / C++, Python, etc. Swig is extremely essential for succesfull build of OpenCV.
  • yum install swig sip (Fedora)
  • apt-get install swig python-sip (Debian based distros)
  • Extract and build OpenCV as follows :
  • ./configure –enable-shared

ENSURE the summary of this step is somewhat similar to

If not, please install the required dependencies from your package manager (yum / Synaptic) as mentioned in the link. Any missing dependency will lead to some problem or the other.

  • make
  • make install
  • Now, comes the finishing touch stage.. you need to let the system know where the OpenCV library files are located.
  • export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH
  • ldconfig

And you are done.. enjoy the OpenCV build by running examples as mentioned in the technical documentation : OpenCV install Guide

OpenCV summary of configure step

Example of playing a file using ffplay command

playing a video file using ffplay