1+ /*
2+ +----------------------------------------------------------------------+
3+ | PHP-OpenCV |
4+ +----------------------------------------------------------------------+
5+ | This source file is subject to version 2.0 of the Apache license, |
6+ | that is bundled with this package in the file LICENSE, and is |
7+ | available through the world-wide-web at the following url: |
8+ | http://www.apache.org/licenses/LICENSE-2.0.html |
9+ | If you did not receive a copy of the Apache2.0 license and are unable|
10+ | to obtain it through the world-wide-web, please send a note to |
11+ | hihozhou@gmail.com so we can mail you a copy immediately. |
12+ +----------------------------------------------------------------------+
13+ | Author: HaiHao Zhou <hihozhou@gmail.com> |
14+ +----------------------------------------------------------------------+
15+ */
16+
17+ #include " ../../php_opencv.h"
18+ #include " opencv_ml.h"
19+ #include " core/opencv_mat.h"
20+
21+ using namespace cv ::ml;
22+
23+ void opencv_ml_init (int module_number){
24+ opencv_ml_constants_init (module_number);
25+ opencv_k_nearest_init (module_number);
26+ }
27+
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+
37+ /* ***************************************************************************************\
38+ * K-Nearest Neighbour Classifier *
39+ \****************************************************************************************/
40+
41+ zend_class_entry *opencv_k_nearest_ce;
42+
43+
44+ zend_object_handlers opencv_k_nearest_object_handlers;
45+
46+
47+ /* *
48+ * CV\ML\KNearest->create
49+ * @param execute_data
50+ * @param return_value
51+ */
52+ PHP_METHOD (opencv_k_nearest, create)
53+ {
54+ zval instance;
55+ object_init_ex (&instance, opencv_k_nearest_ce);
56+ opencv_k_nearest_object *object = Z_PHP_K_NEAREST_OBJ_P (&instance);
57+ object->KNearest = KNearest::create ();
58+ RETURN_ZVAL (&instance,0 ,0 );
59+ }
60+
61+ /* *
62+ * CV\ML\KNearest->getDefaultK
63+ * @param execute_data
64+ * @param return_value
65+ */
66+ PHP_METHOD (opencv_k_nearest, get_default_k)
67+ {
68+ opencv_k_nearest_object *obj = Z_PHP_K_NEAREST_OBJ_P (getThis ());
69+ RETURN_LONG (obj->KNearest ->getDefaultK ());
70+ }
71+
72+ /* *
73+ * CV\ML\KNearest->setDefaultK
74+ * @param execute_data
75+ * @param return_value
76+ */
77+ PHP_METHOD (opencv_k_nearest, set_default_k)
78+ {
79+ long val;
80+ if (zend_parse_parameters (ZEND_NUM_ARGS (), " l" , &val) == FAILURE) {
81+ RETURN_NULL ();
82+ }
83+ opencv_k_nearest_object *obj = Z_PHP_K_NEAREST_OBJ_P (getThis ());
84+ obj->KNearest ->setDefaultK ((int )val);
85+ RETURN_NULL ();
86+ }
87+
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+
107+
108+ /* *
109+ * opencv_k_nearest_methods[]
110+ */
111+ const zend_function_entry opencv_k_nearest_methods[] = {
112+ PHP_ME (opencv_k_nearest, create, NULL , ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
113+ 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)
116+ PHP_FE_END
117+ };
118+ /* }}} */
119+
120+ /* *
121+ * @param type
122+ * @return
123+ */
124+ zend_object* opencv_k_nearest_handler (zend_class_entry *type)
125+ {
126+ size_t size = sizeof (opencv_k_nearest_object);
127+ opencv_k_nearest_object *obj = (opencv_k_nearest_object *)ecalloc (1 ,size);
128+ memset (obj, 0 , sizeof (opencv_k_nearest_object));
129+ zend_object_std_init (&obj->std , type);
130+ object_properties_init (&obj->std , type);
131+ obj->std .ce = type;
132+ obj->std .handlers = &opencv_k_nearest_object_handlers;
133+ return &obj->std ;
134+ }
135+
136+ void opencv_k_nearest_free_obj (zend_object *object)
137+ {
138+ opencv_k_nearest_object *obj;
139+ obj = get_opencv_k_nearest_object (object);
140+ delete obj->KNearest ;
141+ zend_object_std_dtor (object);
142+ }
143+
144+
145+ void opencv_k_nearest_init (int module_number){
146+ zend_class_entry ce;
147+ INIT_NS_CLASS_ENTRY (ce, OPENCV_ML_NS, " KNearest" , opencv_k_nearest_methods);
148+ opencv_k_nearest_ce = zend_register_internal_class_ex (&ce, opencv_k_nearest_ce);
149+
150+ opencv_k_nearest_ce->create_object = opencv_k_nearest_handler;
151+ memcpy (&opencv_k_nearest_object_handlers,
152+ zend_get_std_object_handlers (), sizeof (zend_object_handlers));
153+ opencv_k_nearest_object_handlers.clone_obj = NULL ;
154+ opencv_k_nearest_object_handlers.free_obj = opencv_k_nearest_free_obj;
155+ }
0 commit comments