Skip to content

Commit e26bb21

Browse files
authored
Merge pull request #37 from edwards-sj/add-imdecode-support
Add imdecode support
2 parents a1f33ef + 0a2f957 commit e26bb21

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

opencv.cc

+1
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ const zend_function_entry opencv_functions[] = {
250250
ZEND_NS_NAMED_FE(OPENCV_DNN_NS, readNetFromCaffe, ZEND_FN(opencv_dnn_read_net_from_caffe), NULL)
251251
ZEND_NS_NAMED_FE(OPENCV_DNN_NS, readNetFromTorch, ZEND_FN(opencv_dnn_read_net_from_torch), NULL)
252252
ZEND_NS_NAMED_FE(OPENCV_DNN_NS, readNetFromTensorflow, ZEND_FN(opencv_dnn_read_net_from_tensorflow), NULL)
253+
ZEND_NS_NAMED_FE(OPENCV_NS, imdecode, ZEND_FN(opencv_imdecode), NULL)
253254
PHP_FE_END /* Must be the last line in opencv_functions[] */
254255
};
255256
/* }}} */

source/opencv2/opencv_imgcodecs.cc

+34
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,40 @@ PHP_FUNCTION(opencv_imwrite){
8080
RETURN_TRUE;
8181
}
8282

83+
/**
84+
* CV\imdecode
85+
* @param execute_data
86+
* @param return_value
87+
*/
88+
PHP_FUNCTION(opencv_imdecode)
89+
{
90+
long flags;
91+
char *buf;
92+
long buf_len;
93+
flags = IMREAD_COLOR;//flags default value
94+
95+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &buf,&buf_len, &flags) == FAILURE) {
96+
RETURN_NULL();
97+
}
98+
zval instance;
99+
object_init_ex(&instance,opencv_mat_ce);
100+
opencv_mat_object *obj = Z_PHP_MAT_OBJ_P(&instance);
101+
102+
Mat im = imdecode(Mat(1, buf_len, CV_8UC1, buf), (int)flags);
103+
if(im.empty() || !im.data){
104+
char *error_message = (char*)malloc(strlen("Can not load image") + 1);
105+
strcpy(error_message,"Can not load image");
106+
opencv_throw_exception(error_message);//throw exception
107+
free(error_message);
108+
}
109+
110+
obj->mat = new Mat(im);
111+
112+
//update php Mat object property
113+
opencv_mat_update_property_by_c_mat(&instance, obj->mat);
114+
115+
RETURN_ZVAL(&instance,0,0); //return php Mat object
116+
}
83117

84118
void opencv_imgcodecs_init(int module_number)
85119
{

source/opencv2/opencv_imgcodecs.h

+1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@
2020
extern void opencv_imgcodecs_init(int module_number);
2121
PHP_FUNCTION(opencv_imread);
2222
PHP_FUNCTION(opencv_imwrite);
23+
PHP_FUNCTION(opencv_imdecode);
2324

2425
#endif //OPENCV_OPENCV_IMCODECS_H

tests/faceRecognizerDecode.phpt

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
LBPHFaceRecognizer with Decode
3+
--SKIPIF--
4+
<?php if (!extension_loaded("opencv")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
use CV\Face\LBPHFaceRecognizer;
8+
use function CV\{imdecode, cvtColor, equalizeHist};
9+
use const CV\{COLOR_BGR2GRAY};
10+
11+
$faceRecognizer = LBPHFaceRecognizer::create();
12+
13+
//hihozhou
14+
$str = file_get_contents('./tests/face_recognizer.jpg');
15+
$src = imdecode($str);
16+
$gray = cvtColor($src, COLOR_BGR2GRAY);
17+
equalizeHist($gray, $gray);
18+
19+
$faceRecognizer->train([$gray], [1]);
20+
21+
//Obama
22+
$str = file_get_contents('./tests/Obama.png');
23+
$src = imdecode($str);
24+
$gray = cvtColor($src, COLOR_BGR2GRAY);
25+
equalizeHist($gray, $gray);
26+
27+
//Obama grey
28+
$faceRecognizer->train([$gray], [41]);
29+
30+
$str = file_get_contents('./tests/Obama_gray.png');
31+
$src = imdecode($str);
32+
$gray = cvtColor($src, COLOR_BGR2GRAY);
33+
equalizeHist($gray, $gray);
34+
35+
$faceLabel = $faceRecognizer->predict($gray, $faceConfidence);
36+
37+
$faceLabel = $faceRecognizer->predict($gray);
38+
39+
echo "{$faceLabel}";
40+
?>
41+
--EXPECT--
42+
41

0 commit comments

Comments
 (0)