def animate(): while True: line = random_middle_line() d = DividingLine(synthetic_image(line=line)) d.traverse(line, True) #d.traverse_np(line, True) pylab.clf() pylab.imshow(d.debug) pylab.waitforbuttonpress(0.01)
def test_dividingline(self): line = [1,1,-300] image = synthetic_image((640,480), line) d = DividingLine(image) result = d.traverse(line) assert result['p0'] + result['n0'] == image.shape[0] * image.shape[1] assert result['p1'] + result['n1'] == image.sum() assert result['p2'] + result['n2'] == (image.astype('i8')**2).sum() d2 = DividingLine(image) d2.traverse(line) assert np.all(d2.debug == d.debug) within_eps = lambda a, b: np.all(np.abs(a - b) < 1e-5) assert within_eps(float(result['p1'])/result['p0'], 255) assert within_eps(float(result['n1'])/result['n0'], 0)
def optimize(rgb, debug=False): im = rgb.mean(2).astype('u1') size = im.shape[::-1] mask = table_calibration.make_mask(config.bg['boundpts']) d = DividingLine(im, mask) def error(x): theta, dist = x line = middle_offset(theta, dist, size) s = 1./(d.score(line, debug) + 1e-5) if debug: clf() imshow(d.debug * d.image) pylab.waitforbuttonpress(0.01) return s for iteration in xrange(5): initial = (np.random.rand()*2*pi, (2*np.random.rand()-1)*100) r, score, _, _, _ = scipy.optimize.fmin(error, initial, full_output=True, disp=False) # This threshold is based on empirical observations. Might change. if score < 0.02: break else: print 'Failed %d times' % iteration raise ValueError line = middle_offset(r[0], r[1], size) line = np.array(line) / line[2] res = d.traverse(line) # Scale the line so that a positive dot product indicates the point is # on the 'bright' side of the line if res['p1'] / res['p0'] < res['n1'] / res['n0']: line *= -1 return line
import dividingline; reload(dividingline) from dividingline import dividingline reload(dividingline) from dividingline import DividingLine, synthetic_image, random_middle_line import pylab import scipy.optimize line = [1,1,-300] #im = synthetic_image((640,480), line) im = imread('tableshot.png').mean(2)[100:,:]*255 d = DividingLine(im) res = d.traverse(line, True) d2 = DividingLine(im) res2 = d2.traverse_np(line, True) def animate(): while True: line = random_middle_line() d = DividingLine(synthetic_image(line=line)) d.traverse(line, True) #d.traverse_np(line, True) pylab.clf() pylab.imshow(d.debug) pylab.waitforbuttonpress(0.01) middle_offset = dividingline.middle_offset def optimize():