From 6dc7ae0ff618f41113cf810ea9bbdb0e5759bffa Mon Sep 17 00:00:00 2001 From: Alexander Mordvintsev Date: Mon, 6 Jun 2011 14:18:25 +0000 Subject: [PATCH] added some constants to python cv2 api --- modules/python/src2/cv2.cpp | 8 ++++++++ samples/python2/browse.py | 12 ++++++------ samples/python2/gaussian_mix.py | 4 ++-- samples/python2/letter_recog.py | 17 +++++------------ 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/modules/python/src2/cv2.cpp b/modules/python/src2/cv2.cpp index 511a3f0..e9c2aa4 100644 --- a/modules/python/src2/cv2.cpp +++ b/modules/python/src2/cv2.cpp @@ -856,6 +856,14 @@ void initcv2() PUBLISH(GC_INIT_WITH_MASK); PUBLISH(GC_EVAL); + PUBLISH(CV_ROW_SAMPLE); + PUBLISH(CV_VAR_NUMERICAL); + PUBLISH(CV_VAR_ORDERED); + PUBLISH(CV_VAR_CATEGORICAL); + + PUBLISH(CV_AA); + + #include "pyopencv_generated_const_reg.h" } diff --git a/samples/python2/browse.py b/samples/python2/browse.py index ed2353f..721fbaa 100644 --- a/samples/python2/browse.py +++ b/samples/python2/browse.py @@ -16,14 +16,14 @@ if len(sys.argv) > 1: img = cv2.imread(fn) else: sz = 4096 - print 'generating %dx%d precudural image ...' % (sz, sz) + print 'generating %dx%d procudural image ...' % (sz, sz) img = np.zeros((sz, sz), np.uint8) - track = np.cumsum(np.random.rand(1000000, 2)-0.5, axis=0) - track = np.int32(track*20 + (sz/2, sz/2)) + track = np.cumsum(np.random.rand(500000, 2)-0.5, axis=0) + track = np.int32(track*10 + (sz/2, sz/2)) cv2.polylines(img, [track], 0, 255, 1, cv.CV_AA) small = img -for i in xrange(4): +for i in xrange(3): small = cv2.pyrDown(small) def onmouse(event, x, y, flags, param): @@ -33,6 +33,6 @@ def onmouse(event, x, y, flags, param): zoom = cv2.getRectSubPix(img, (800, 600), (x+0.5, y+0.5)) cv2.imshow('zoom', zoom) -cv2.imshow('small', small) -cv.SetMouseCallback('small', onmouse, None) +cv2.imshow('preview', small) +cv.SetMouseCallback('preview', onmouse, None) cv2.waitKey() diff --git a/samples/python2/gaussian_mix.py b/samples/python2/gaussian_mix.py index 6f08e88..d7aaa21 100644 --- a/samples/python2/gaussian_mix.py +++ b/samples/python2/gaussian_mix.py @@ -1,6 +1,6 @@ import numpy as np from numpy import random -import cv2, cv +import cv2 def make_gaussians(cluster_n, img_size): @@ -22,7 +22,7 @@ def draw_gaussain(img, mean, cov, color): w, u, vt = cv2.SVDecomp(cov) ang = np.rad2deg( np.arctan2(u[1, 0], u[0, 0]) ) s1, s2 = np.sqrt(w)*3.0 - cv2.ellipse(img, (x, y), (s1, s2), ang, 0, 360, color, 1, cv.CV_AA) + cv2.ellipse(img, (x, y), (s1, s2), ang, 0, 360, color, 1, cv2.CV_AA) if __name__ == '__main__': diff --git a/samples/python2/letter_recog.py b/samples/python2/letter_recog.py index efd4297..e0fabc5 100644 --- a/samples/python2/letter_recog.py +++ b/samples/python2/letter_recog.py @@ -6,13 +6,6 @@ def load_base(fn): samples, responses = a[:,1:], a[:,0] return samples, responses -# TODO move these to cv2 -CV_ROW_SAMPLE = 1 -CV_VAR_NUMERICAL = 0 -CV_VAR_ORDERED = 0 -CV_VAR_CATEGORICAL = 1 - - class LetterStatModel(object): train_ratio = 0.5 def load(self, fn): @@ -26,10 +19,10 @@ class RTrees(LetterStatModel): def train(self, samples, responses): sample_n, var_n = samples.shape - var_types = np.array([CV_VAR_NUMERICAL] * var_n + [CV_VAR_CATEGORICAL], np.uint8) + var_types = np.array([cv2.CV_VAR_NUMERICAL] * var_n + [cv2.CV_VAR_CATEGORICAL], np.uint8) #CvRTParams(10,10,0,false,15,0,true,4,100,0.01f,CV_TERMCRIT_ITER)); params = dict(max_depth=10 ) - self.model.train(samples, CV_ROW_SAMPLE, responses, varType = var_types, params = params) + self.model.train(samples, cv2.CV_ROW_SAMPLE, responses, varType = var_types, params = params) def predict(self, samples): return np.float32( [self.model.predict(s) for s in samples] ) @@ -56,10 +49,10 @@ class Boost(LetterStatModel): sample_n, var_n = samples.shape new_samples = self.unroll_samples(samples) new_responses = self.unroll_responses(responses) - var_types = np.array([CV_VAR_NUMERICAL] * var_n + [CV_VAR_CATEGORICAL, CV_VAR_CATEGORICAL], np.uint8) + var_types = np.array([cv2.CV_VAR_NUMERICAL] * var_n + [cv2.CV_VAR_CATEGORICAL, cv2.CV_VAR_CATEGORICAL], np.uint8) #CvBoostParams(CvBoost::REAL, 100, 0.95, 5, false, 0 ) params = dict(max_depth=5) #, use_surrogates=False) - self.model.train(new_samples, CV_ROW_SAMPLE, new_responses, varType = var_types, params=params) + self.model.train(new_samples, cv2.CV_ROW_SAMPLE, new_responses, varType = var_types, params=params) def predict(self, samples): new_samples = self.unroll_samples(samples) @@ -105,7 +98,7 @@ if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('-model', default='rtrees', choices=models.keys()) - parser.add_argument('-data', nargs=1, default='letter-recognition.data') + parser.add_argument('-data', nargs=1, default='../cpp/letter-recognition.data') parser.add_argument('-load', nargs=1) parser.add_argument('-save', nargs=1) args = parser.parse_args() -- 2.7.4