Skip to content

Commit 66aa5fb

Browse files
committed
Add Class CV\ML\KNearest
1 parent b69c14f commit 66aa5fb

File tree

5 files changed

+174
-1
lines changed

5 files changed

+174
-1
lines changed

config.m4

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ if test "$PHP_OPENCV" != "no"; then
4848
source/opencv2/opencv_objdetect.cc \
4949
source/opencv2/opencv_videoio.cc \
5050
source/opencv2/opencv_face.cc \
51-
source/opencv2/face/opencv_facerec.cc"
51+
source/opencv2/face/opencv_facerec.cc \
52+
source/opencv2/opencv_ml.cc"
5253

5354

5455
PHP_NEW_EXTENSION(opencv, $opencv_source_file, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)

opencv.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ extern "C" {
4141
#include "source/opencv2/opencv_videoio.h"
4242
#include "source/opencv2/opencv_face.h"
4343
#include "source/opencv2/core/opencv_utility.h"
44+
#include "source/opencv2/opencv_ml.h"
4445

4546
/* If you declare any globals in php_opencv.h uncomment this:
4647
ZEND_DECLARE_MODULE_GLOBALS(opencv)
@@ -122,6 +123,7 @@ PHP_MINIT_FUNCTION(opencv)
122123
opencv_objdetect_init(module_number);
123124
opencv_videoio_init(module_number);
124125
opencv_face_init(module_number);
126+
opencv_ml_init(module_number);
125127

126128
return SUCCESS;
127129
}

php_opencv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ using namespace cv;
4848

4949
#define OPENCV_NS "CV"
5050
#define OPENCV_FACE_NS ZEND_NS_NAME(OPENCV_NS,"Face")
51+
#define OPENCV_ML_NS ZEND_NS_NAME(OPENCV_NS,"ML")
5152

5253
#define OPENCV_CONNECT(text1,text2) text1##text2
5354

source/opencv2/opencv_ml.cc

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
}

source/opencv2/opencv_ml.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
#ifndef PHP_OPENCV_ML_H
18+
#define PHP_OPENCV_ML_H
19+
20+
#include <opencv2/ml.hpp>
21+
22+
extern void opencv_ml_init(int module_number);
23+
extern void opencv_k_nearest_init(int module_number);
24+
25+
26+
/****************************************************************************************\
27+
* K-Nearest Neighbour Classifier *
28+
\****************************************************************************************/
29+
30+
extern zend_class_entry *opencv_k_nearest_ce;
31+
32+
typedef struct _opencv_k_nearest_object{
33+
zend_object std;
34+
Ptr<cv::ml::KNearest> KNearest;
35+
}opencv_k_nearest_object;
36+
37+
#define Z_PHP_K_NEAREST_OBJ_P(zv) get_opencv_k_nearest_object(Z_OBJ_P(zv))
38+
39+
static inline opencv_k_nearest_object* get_opencv_k_nearest_object(zend_object *obj) {
40+
return (opencv_k_nearest_object*)((char*)(obj) - XtOffsetOf(opencv_k_nearest_object, std));
41+
}
42+
43+
44+
45+
#endif //PHP_OPENCV_ML_H

0 commit comments

Comments
 (0)