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
bbox = np.array( ( (0,0), (800,600) ) ) # bounding-box for drawing
ua1 = ( 100, 100 )
ua2 = ( 300, 200 )
ub1 = ( 700, 300 )
ub2 = ( 50, 500 )
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] )
ba1, ba2 = tools.line2segment( bbox, la )
bb1, bb2 = tools.line2segment( bbox, lb )
ux = np.cross( la, lb );
ux = ( ux[0]/ux[2], ux[1]/ux[2] )
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()
img = cv2.imread( 'data/cd_box.png' ) # we use cv2 function since it provides uint8 pixels
plt.imshow( img )
plt.show()
edges = cv2.Canny(img,350,400)
plt.imshow( edges )
plt.show()
e = cv2.findNonZero( edges )
e
e.shape = (e.shape[0],e.shape[2])
e = e.astype(float)
plt.imshow( img )
plt.plot( e[:,0], e[:,1], 'r.', markersize=1 )
plt.show()