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

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… !!!

MATLAB: Running codes in mixed OS environment

Ever faced the problem of running executables in a MATLAB installation on Linux. I frequently use an executable file (.exe) provided by an eminent research lab, but the linux enthusiast that I am, shifting to Windows just for running this file is out of the question! So, how to solve this issue!

Well, you need access to either a system running Windows at your workplace, or have a virtual Windows installation in VirtualBox. Set up ssh via cygwin (refer SSH via CYGWIN) on such a system and also make sure to have a password-less SSH login enabled (refer ssh autologin) between the Linux and Windows systems.

Once this is done and given the fact that such a Windows environment is up and running, all you have to do through the MATLAB command prompt is:

system(‘ssh username@IP_of_windows command1;command2’);

where : command1 and command2 are the things you want to execute on that system. For e.g: in my scenario, i have already put the .exe (say: test.exe) in the cygwin home folder (C:/cygwin/home/<username>/) on the Windows system. Now, i just run:

system(‘ssh username@IP_of_windows ./test.exe’);

You can use few more ssh/scp via system command calls in MATLAB to copy back & forth the data. Thats it!

MATLAB: Run m-file From Linux Terminal / DOS

Yes, MATLAB m-files can very much be executed without initiating the MATLAB GUI. This can be quite handy when when running multiple codes simultaneously without creating a huge clutter for the user, or when running multiple instances of MATLAB and there is a need to prevent unnecessary eating up of the RAM through its GUI.

In Linux, it works via the re-direction operator ( < ), while in Windows, it has to be done using the -r flag. The steps are detailed below:

For MATLAB installations on Linux, ensure the MATLAB soft link is available in /usr/local/bin, so that it can be called from the terminal without specifying its full installation path, i.e.,

cd /usr/local/bin

sudo ln -s /path_to_MATLAB_installation/matlab

cd

matlab -desktop

Once MATLAB link has been created, exit the MATLAB GUI. Now run your m-file (say: test_run.m) from the terminal as:

matlab -nodesktop -nosplash < /path_to_file_location/test_run -logfile test_run.log

Thats it! The “-nodesktop” option ensures the full GUI isnt initiated and only the MATLAB command prompt pops up, “-nosplash” prevents the MATLAB splash screen from showing up. The redirection operator will run all the commands in the m-file as they would in a normal way and “-logfile” logs all that shows up in the MATLAB command window.

In Windows, the slight modification is that we have to first cd to the location where the m-file is present

cd path_to_file_location\

matlab.exe -nodesktop -nosplash -r test_run -logfile test_run.log

And we are done!

P.S : 1) Dont forget to check out the difference in RAM usage with “-nodesktop” enabled and normal GUI way

2) DO NOT put .m with the file name when using this command, else it’ll result in an error!

Installing WIndows using VirtualBox in Linux

Found a really nice article on the procedure to be followed for installing and performing certain post install steps. The site link is given below:

http://www.linuxjournal.com/content/using-windows-xp-virtualbox-linux