Finding coordinates in a picture

Posted on Sun 01 December 2013 in Notes

This is a very handy one liner I found in Programming Computer Vision, that can give you the coordinates of non-black pixels in an image.

In [25]: a = np.random.randint(10, size =(5,5))

In [26]: a[a>6] = 0

In [27]: a
Out[27]: 
array([[8, 0, 0, 0, 6],
       [0, 6, 8, 0, 0],
       [9, 0, 0, 6, 6],
       [0, 0, 0, 8, 0],
       [0, 0, 0, 0, 0]])

In [28]: coord = np.array(a.nonzero()).T

In [29]: coord
Out[29]: 
array([[0, 0],
       [0, 4],
       [1, 1],
       [1, 2],
       [2, 0],
       [2, 3],
       [2, 4],
       [3, 3]])

It's the np.array(a.nonzero()).T where the magic happens.


Iteration count in python

Posted on Sun 01 December 2013 in Notes

This is a quite nifty "trick" in Python. Often times you want to iterate an object (array or list, or something else) and you want to use each item you iterate over while keeping count of how many iterations you've been through. The enumerate() function does this!

In [14]: datapoints = np.random.randint(10, size = (4,4))

In [15]: targets = np.random.randint(10, size = (4,4))

In [16]: for i, data in enumerate(datapoints):
   ....:     print i, '\t', data, data == targets[i]
   ....:     
0     [9 8 4 1]    [False False False False]
1     [2 1 9 1]    [False False False False]
2     [1 9 8 1]    [False False False False]
3     [5 5 0 4]    [False False False False]

Hidden Markov Model

Posted on Tue 26 November 2013 in Notes

Bayes theorem / understanding conditional probability P(A|B)

And two newer videoes show the usage

Hidden Markov Model