img = cam.getImage().scale(400, 400) img2 = img.binarize() def costImage(i): # Make image black and white img1 = img.binarize(int(i)) mat = img1.getNumpy() countW = np.count_nonzero(mat) countB = mat.size - countW return ((countW - countB) / float(mat.size))**2 params = {} #bayesopt.initialize_params() params['n_iterations'] = 15 params['n_init_samples'] = 5 valid_values = np.transpose(np.array(range(256), dtype=float, ndmin=2)) mvalue, x_out, error = bayesopt.optimize_discrete(costImage, valid_values, params) x_out = int(x_out) print x_out img1 = img.binarize(x_out) img1 = img.sideBySide(img1).sideBySide(img2) img1.drawText("Threshold: " + str(x_out)) img1.show() foo = raw_input('Press any key')
start = clock() mvalue, x_out, error = bayesopt.optimize(testfunc, n, lb, ub, params) print "Result", x_out print "Seconds", clock() - start print "OO implementation" bo_test = BayesOptTest(n) bo_test.parameters = params bo_test.lower_bound = lb bo_test.upper_bound = ub start = clock() mvalue, x_out, error = bo_test.optimize() print "Result", x_out print "Seconds", clock() - start print "Callback discrete implementation" x_set = np.random.rand(100, n) start = clock() mvalue, x_out, error = bayesopt.optimize_discrete(testfunc, x_set, params) print "Result", x_out print "Seconds", clock() - start value = np.array([testfunc(i) for i in x_set]) print "Optimun", x_set[value.argmin()]
def optimize(self): min_val, x_out, error = bo.optimize_discrete(self.evaluateSample, self.x_set, self.params) return min_val, x_out, error
mvalue, x_out, error = bayesopt.optimize(testfunc, n, lb, ub, params) print "Result", x_out print "Seconds", clock() - start print "OO implementation" bo_test = BayesOptTest(n) bo_test.parameters = params bo_test.lower_bound = lb bo_test.upper_bound = ub start = clock() mvalue, x_out, error = bo_test.optimize() print "Result", x_out print "Seconds", clock() - start print "Callback discrete implementation" x_set = np.random.rand(100,n) start = clock() mvalue, x_out, error = bayesopt.optimize_discrete(testfunc, x_set, params) print "Result", x_out print "Seconds", clock() - start value = np.array([testfunc(i) for i in x_set]) print "Optimun", x_set[value.argmin()]
#Load images img = cam.getImage().scale(400,400) img2 = img.binarize() def costImage(i): # Make image black and white img1 = img.binarize(int(i)) mat = img1.getNumpy() countW = np.count_nonzero(mat); countB = mat.size-countW return ((countW-countB)/float(mat.size))**2 params = {} #bayesopt.initialize_params() params['n_iterations'] = 15 params['n_init_samples'] = 5 valid_values = np.transpose(np.array(range(256), dtype=float, ndmin=2)) mvalue, x_out, error = bayesopt.optimize_discrete(costImage, valid_values, params) x_out = int(x_out) print(x_out) img1 = img.binarize(x_out) img1 = img.sideBySide(img1).sideBySide(img2) img1.drawText("Threshold: "+str(x_out)) img1.show() foo = raw_input('Press any key')