Skip to content

Commit 7163781

Browse files
authored
Merge pull request opencv#18343 from TolyaTalamanov:at/support-return-tuple
[G-API] Support std::tuple for return type
2 parents a63cee2 + 986bc65 commit 7163781

File tree

5 files changed

+80
-3
lines changed

5 files changed

+80
-3
lines changed

modules/gapi/include/opencv2/gapi/core.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,7 @@ All output matrices must be in @ref CV_8UC1.
14461446
@sa merge3, merge4
14471447
*/
14481448
GAPI_EXPORTS std::tuple<GMat, GMat, GMat,GMat> split4(const GMat& src);
1449-
GAPI_EXPORTS std::tuple<GMat, GMat, GMat> split3(const GMat& src);
1449+
GAPI_EXPORTS_W std::tuple<GMat, GMat, GMat> split3(const GMat& src);
14501450

14511451
/** @brief Applies a generic geometrical transformation to an image.
14521452

modules/gapi/include/opencv2/gapi/gcomputation.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class GAPI_EXPORTS_W GComputation
172172
* @param in input GMat of the defined unary computation
173173
* @param out output GMat of the defined unary computation
174174
*/
175-
GComputation(GMat in, GMat out); // Unary overload
175+
GAPI_WRAP GComputation(GMat in, GMat out); // Unary overload
176176

177177
/**
178178
* @brief Defines an unary (one input -- one output) computation
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env python
2+
3+
import numpy as np
4+
import cv2 as cv
5+
6+
from tests_common import NewOpenCVTests
7+
8+
9+
# Plaidml is an optional backend
10+
pkgs = [
11+
cv.gapi.core.ocl.kernels(),
12+
cv.gapi.core.cpu.kernels(),
13+
cv.gapi.core.fluid.kernels()
14+
# cv.gapi.core.plaidml.kernels()
15+
]
16+
17+
18+
class gapi_sample_pipelines(NewOpenCVTests):
19+
20+
# NB: This test check multiple outputs for operation
21+
def test_mean_over_r(self):
22+
sz = (100, 100, 3)
23+
in_mat = np.random.randint(0, 100, sz).astype(np.uint8)
24+
25+
# # OpenCV
26+
_, _, r_ch = cv.split(in_mat)
27+
expected = cv.mean(r_ch)
28+
29+
# G-API
30+
g_in = cv.GMat()
31+
b, g, r = cv.gapi.split3(g_in)
32+
g_out = cv.gapi.mean(r)
33+
comp = cv.GComputation(g_in, g_out)
34+
35+
for pkg in pkgs:
36+
actual = comp.apply(in_mat, args=cv.compile_args(pkg))
37+
# Comparison
38+
self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF))
39+
40+
41+
if __name__ == '__main__':
42+
NewOpenCVTests.bootstrap()

modules/python/src2/cv2.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,41 @@ template<typename _Tp> static inline PyObject* pyopencv_from_generic_vec(const s
14861486
return seq;
14871487
}
14881488

1489+
template<std::size_t I = 0, typename... Tp>
1490+
inline typename std::enable_if<I == sizeof...(Tp), void>::type
1491+
convert_to_python_tuple(const std::tuple<Tp...>&, PyObject*) { }
1492+
1493+
template<std::size_t I = 0, typename... Tp>
1494+
inline typename std::enable_if<I < sizeof...(Tp), void>::type
1495+
convert_to_python_tuple(const std::tuple<Tp...>& cpp_tuple, PyObject* py_tuple)
1496+
{
1497+
PyObject* item = pyopencv_from(std::get<I>(cpp_tuple));
1498+
1499+
if (!item)
1500+
return;
1501+
1502+
PyTuple_SET_ITEM(py_tuple, I, item);
1503+
convert_to_python_tuple<I + 1, Tp...>(cpp_tuple, py_tuple);
1504+
}
1505+
1506+
1507+
template<typename... Ts>
1508+
PyObject* pyopencv_from(const std::tuple<Ts...>& cpp_tuple)
1509+
{
1510+
size_t size = sizeof...(Ts);
1511+
PyObject* py_tuple = PyTuple_New(size);
1512+
convert_to_python_tuple(cpp_tuple, py_tuple);
1513+
size_t actual_size = PyTuple_Size(py_tuple);
1514+
1515+
if (actual_size < size)
1516+
{
1517+
Py_DECREF(py_tuple);
1518+
return NULL;
1519+
}
1520+
1521+
return py_tuple;
1522+
}
1523+
14891524
template<>
14901525
PyObject* pyopencv_from(const std::pair<int, double>& src)
14911526
{

modules/python/test/test_copytomask.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
# Python 2/3 compatibility
88
from __future__ import print_function
99

10-
import cv2 as cv
1110
import numpy as np
11+
import cv2 as cv
1212
import sys
1313

1414
from tests_common import NewOpenCVTests

0 commit comments

Comments
 (0)