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 < opencv2/ml.hpp>
20+
21+ using namespace cv ::ml;
22+
23+ void opencv_ml_init (int module_number){
24+ opencv_k_nearest_init (module_number);
25+ }
26+
27+ /* ***************************************************************************************\
28+ * K-Nearest Neighbour Classifier *
29+ \****************************************************************************************/
30+
31+ zend_class_entry *opencv_k_nearest_ce;
32+
33+
34+ zend_object_handlers opencv_k_nearest_object_handlers;
35+
36+
37+ /* *
38+ * CV\ML\KNearest->create
39+ * @param execute_data
40+ * @param return_value
41+ */
42+ PHP_METHOD (opencv_k_nearest, create)
43+ {
44+ zval instance;
45+ object_init_ex (&instance, opencv_k_nearest_ce);
46+ opencv_k_nearest_object *object = Z_PHP_K_NEAREST_OBJ_P (&instance);
47+ object->KNearest = KNearest::create ();
48+ RETURN_ZVAL (&instance,0 ,0 );
49+ }
50+
51+ /* *
52+ * CV\ML\KNearest->getDefaultK
53+ * @param execute_data
54+ * @param return_value
55+ */
56+ PHP_METHOD (opencv_k_nearest, get_default_k)
57+ {
58+ opencv_k_nearest_object *obj = Z_PHP_K_NEAREST_OBJ_P (getThis ());
59+ RETURN_LONG (obj->KNearest ->getDefaultK ());
60+ }
61+
62+ /* *
63+ * CV\ML\KNearest->setDefaultK
64+ * @param execute_data
65+ * @param return_value
66+ */
67+ PHP_METHOD (opencv_k_nearest, set_default_k)
68+ {
69+ long val;
70+ if (zend_parse_parameters (ZEND_NUM_ARGS (), " l" , &val) == FAILURE) {
71+ RETURN_NULL ();
72+ }
73+ opencv_k_nearest_object *obj = Z_PHP_K_NEAREST_OBJ_P (getThis ());
74+ obj->KNearest ->setDefaultK ((int )val);
75+ RETURN_NULL ();
76+ }
77+
78+
79+ /* *
80+ * opencv_k_nearest_methods[]
81+ */
82+ const zend_function_entry opencv_k_nearest_methods[] = {
83+ PHP_ME (opencv_k_nearest, create, NULL , ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
84+ PHP_MALIAS (opencv_k_nearest, getDefaultK ,get_default_k, NULL , ZEND_ACC_PUBLIC)
85+ PHP_FE_END
86+ };
87+ /* }}} */
88+
89+ /* *
90+ * @param type
91+ * @return
92+ */
93+ zend_object* opencv_k_nearest_handler (zend_class_entry *type)
94+ {
95+ size_t size = sizeof (opencv_k_nearest_object);
96+ opencv_k_nearest_object *obj = (opencv_k_nearest_object *)ecalloc (1 ,size);
97+ memset (obj, 0 , sizeof (opencv_k_nearest_object));
98+ zend_object_std_init (&obj->std , type);
99+ object_properties_init (&obj->std , type);
100+ obj->std .ce = type;
101+ obj->std .handlers = &opencv_k_nearest_object_handlers;
102+ return &obj->std ;
103+ }
104+
105+ void opencv_k_nearest_free_obj (zend_object *object)
106+ {
107+ opencv_k_nearest_object *obj;
108+ obj = get_opencv_k_nearest_object (object);
109+ delete obj->KNearest ;
110+ zend_object_std_dtor (object);
111+ }
112+
113+
114+ void opencv_k_nearest_init (int module_number){
115+ zend_class_entry ce;
116+ INIT_NS_CLASS_ENTRY (ce, OPENCV_ML_NS, " KNearest" , opencv_k_nearest_methods);
117+ opencv_k_nearest_ce = zend_register_internal_class_ex (&ce, opencv_k_nearest_ce);
118+
119+ opencv_k_nearest_ce->create_object = opencv_k_nearest_handler;
120+ memcpy (&opencv_k_nearest_object_handlers,
121+ zend_get_std_object_handlers (), sizeof (zend_object_handlers));
122+ opencv_k_nearest_object_handlers.clone_obj = NULL ;
123+ opencv_k_nearest_object_handlers.free_obj = opencv_k_nearest_free_obj;
124+ }
0 commit comments