Skip to content

Commit 0791d44

Browse files
committed
Add CV\ML\KNearest->train
1 parent 305951a commit 0791d44

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

source/opencv2/opencv_ml.cc

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,24 @@
1616

1717
#include "../../php_opencv.h"
1818
#include "opencv_ml.h"
19-
#include <opencv2/ml.hpp>
19+
#include "core/opencv_mat.h"
2020

2121
using namespace cv::ml;
2222

2323
void opencv_ml_init(int module_number){
24+
opencv_ml_constants_init(module_number);
2425
opencv_k_nearest_init(module_number);
2526
}
2627

28+
void opencv_ml_constants_init(int module_number){
29+
opencv_ml_sample_types_init(module_number);
30+
}
31+
32+
void opencv_ml_sample_types_init(int module_number){
33+
REGISTER_NS_LONG_CONSTANT(OPENCV_ML_NS, "ROW_SAMPLE", ROW_SAMPLE, CONST_CS | CONST_PERSISTENT);
34+
REGISTER_NS_LONG_CONSTANT(OPENCV_ML_NS, "COL_SAMPLE", COL_SAMPLE, CONST_CS | CONST_PERSISTENT);
35+
}
36+
2737
/****************************************************************************************\
2838
* K-Nearest Neighbour Classifier *
2939
\****************************************************************************************/
@@ -75,13 +85,34 @@ PHP_METHOD(opencv_k_nearest, set_default_k)
7585
RETURN_NULL();
7686
}
7787

88+
PHP_METHOD(opencv_k_nearest, train)
89+
{
90+
zval *samples_zval, *responses_zval;
91+
long layout;
92+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OlO",
93+
&samples_zval, opencv_mat_ce,
94+
&layout,
95+
&responses_zval, opencv_mat_ce
96+
) == FAILURE) {
97+
RETURN_NULL();
98+
}
99+
opencv_k_nearest_object *obj = Z_PHP_K_NEAREST_OBJ_P(getThis());
100+
opencv_mat_object *samples_obj = Z_PHP_MAT_OBJ_P(samples_zval);
101+
opencv_mat_object *responses_obj = Z_PHP_MAT_OBJ_P(responses_zval);
102+
103+
RETURN_BOOL(obj->KNearest->train(*samples_obj->mat, (int)layout, *responses_obj->mat));
104+
}
105+
106+
78107

79108
/**
80109
* opencv_k_nearest_methods[]
81110
*/
82111
const zend_function_entry opencv_k_nearest_methods[] = {
83112
PHP_ME(opencv_k_nearest, create, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
84113
PHP_MALIAS(opencv_k_nearest, getDefaultK ,get_default_k, NULL, ZEND_ACC_PUBLIC)
114+
PHP_MALIAS(opencv_k_nearest, setDefaultK ,set_default_k, NULL, ZEND_ACC_PUBLIC)
115+
PHP_ME(opencv_k_nearest, train, NULL, ZEND_ACC_PUBLIC)
85116
PHP_FE_END
86117
};
87118
/* }}} */

source/opencv2/opencv_ml.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
extern void opencv_ml_init(int module_number);
2323
extern void opencv_k_nearest_init(int module_number);
24+
extern void opencv_ml_constants_init(int module_number);
25+
extern void opencv_ml_sample_types_init(int module_number);
2426

2527

2628
/****************************************************************************************\

0 commit comments

Comments
 (0)