Homogeneous representation of point and lines

In [2]:
import numpy as np # math package for N-dimensional arrays and other stuff
import matplotlib.pyplot as plt # image input/output and graph drawing 
import cv2 # opencv
import tools # helper functions, available on the web page
In [3]:
bbox = np.array( ( (0,0), (800,600) ) ) # bounding-box for drawing
In [4]:
ua1 = ( 100, 100 )
ua2 = ( 300, 200 )

ub1 = ( 700, 300 )
ub2 = ( 50, 500 )
In [5]:
la = np.cross( [ua1[0], ua1[1], 1 ], [ua2[0], ua2[1], 1] )
lb = np.cross( [ub1[0], ub1[1], 1 ], [ub2[0], ub2[1], 1] )
In [6]:
ba1, ba2 = tools.line2segment( bbox, la )
bb1, bb2 = tools.line2segment( bbox, lb )
In [7]:
ux = np.cross( la, lb );
In [8]:
ux = ( ux[0]/ux[2], ux[1]/ux[2] )
In [9]:
plt.plot( bbox[ ( 0, 0, 1, 1, 0 ), 0 ], bbox[ ( 0, 1, 1, 0, 0 ), 1 ] )
plt.plot( ua1[0], ua1[1],  'ro' )
plt.plot( ua2[0], ua2[1],  'ro' )
plt.plot( ub1[0], ub1[1],  'go' )
plt.plot( ub2[0], ub2[1],  'go' )
plt.plot( [ ba1[0], ba2[0] ], [ ba1[1], ba2[1] ], 'r' )
plt.plot( [ bb1[0], bb2[0] ], [ bb1[1], bb2[1] ], 'g' )
plt.plot( ux[0], ux[1], 'mo' )
plt.axis( 'equal')
plt.show()

Edge detection using opencv

In [11]:
img = cv2.imread( 'data/cd_box.png' ) # we use cv2 function since it provides uint8 pixels
In [12]:
plt.imshow( img )
plt.show()
In [13]:
edges = cv2.Canny(img,350,400)
In [15]:
plt.imshow( edges )
plt.show()
In [22]:
e = cv2.findNonZero( edges )
In [23]:
e
Out[23]:
array([[[299,  35]],

       [[300,  35]],

       [[301,  35]],

       ...,

       [[307, 388]],

       [[308, 388]],

       [[309, 388]]], dtype=int32)
In [26]:
e.shape = (e.shape[0],e.shape[2])
In [28]:
e = e.astype(float)
In [32]:
plt.imshow( img )
plt.plot( e[:,0], e[:,1], 'r.', markersize=1 )
plt.show()