Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv
Commits
205ccddf
Commit
205ccddf
authored
Jan 23, 2017
by
Alexander Alekhin
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #8053 from pwuertz:umat-copy-python
parents
533d399d
c659f94d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
77 additions
and
22 deletions
+77
-22
cv2.cpp
modules/python/src2/cv2.cpp
+66
-22
test.py
modules/python/test/test.py
+11
-0
No files found.
modules/python/src2/cv2.cpp
View file @
205ccddf
...
@@ -417,34 +417,74 @@ typedef struct {
...
@@ -417,34 +417,74 @@ typedef struct {
UMat
*
um
;
UMat
*
um
;
}
cv2_UMatWrapperObject
;
}
cv2_UMatWrapperObject
;
// UMatWrapper init - takes one optional argument, that converts to Mat, that converts to UMat and stored inside.
static
bool
PyObject_IsUMat
(
PyObject
*
o
);
// If no argument given - empty UMat created.
// UMatWrapper init - try to map arguments from python to UMat constructors
static
int
UMatWrapper_init
(
cv2_UMatWrapperObject
*
self
,
PyObject
*
args
,
PyObject
*
kwds
)
static
int
UMatWrapper_init
(
cv2_UMatWrapperObject
*
self
,
PyObject
*
args
,
PyObject
*
kwds
)
{
{
self
->
um
=
new
UMat
();
self
->
um
=
NULL
;
{
PyObject
*
np_mat
=
NULL
;
// constructor ()
const
char
*
kwlist
[]
=
{
NULL
};
static
char
*
kwlist
[]
=
{
new
char
[
3
],
NULL
};
if
(
PyArg_ParseTupleAndKeywords
(
args
,
kwds
,
""
,
(
char
**
)
kwlist
))
{
strcpy
(
kwlist
[
0
],
"mat"
);
self
->
um
=
new
UMat
();
return
0
;
if
(
!
PyArg_ParseTupleAndKeywords
(
args
,
kwds
,
"|O"
,
kwlist
,
&
np_mat
))
}
return
-
1
;
PyErr_Clear
();
if
(
np_mat
)
{
Mat
m
;
if
(
!
pyopencv_to
(
np_mat
,
m
,
ArgInfo
(
"UMatWrapper.np_mat"
,
0
)))
return
-
1
;
m
.
copyTo
(
*
self
->
um
);
}
}
{
return
0
;
// constructor (rows, cols, type)
const
char
*
kwlist
[]
=
{
"rows"
,
"cols"
,
"type"
,
NULL
};
int
rows
,
cols
,
type
;
if
(
PyArg_ParseTupleAndKeywords
(
args
,
kwds
,
"iii"
,
(
char
**
)
kwlist
,
&
rows
,
&
cols
,
&
type
))
{
self
->
um
=
new
UMat
(
rows
,
cols
,
type
);
return
0
;
}
PyErr_Clear
();
}
{
// constructor (m, rowRange, colRange)
const
char
*
kwlist
[]
=
{
"m"
,
"rowRange"
,
"colRange"
,
NULL
};
PyObject
*
obj
=
NULL
;
int
y0
=
-
1
,
y1
=
-
1
,
x0
=
-
1
,
x1
=
-
1
;
if
(
PyArg_ParseTupleAndKeywords
(
args
,
kwds
,
"O(ii)|(ii)"
,
(
char
**
)
kwlist
,
&
obj
,
&
y0
,
&
y1
,
&
x0
,
&
x1
)
&&
PyObject_IsUMat
(
obj
))
{
UMat
*
um_other
=
((
cv2_UMatWrapperObject
*
)
obj
)
->
um
;
Range
rowRange
(
y0
,
y1
);
Range
colRange
=
(
x0
>=
0
&&
x1
>=
0
)
?
Range
(
x0
,
x1
)
:
Range
::
all
();
self
->
um
=
new
UMat
(
*
um_other
,
rowRange
,
colRange
);
return
0
;
}
PyErr_Clear
();
}
{
// constructor (m)
const
char
*
kwlist
[]
=
{
"m"
,
NULL
};
PyObject
*
obj
=
NULL
;
if
(
PyArg_ParseTupleAndKeywords
(
args
,
kwds
,
"O"
,
(
char
**
)
kwlist
,
&
obj
))
{
// constructor (UMat m)
if
(
PyObject_IsUMat
(
obj
))
{
UMat
*
um_other
=
((
cv2_UMatWrapperObject
*
)
obj
)
->
um
;
self
->
um
=
new
UMat
(
*
um_other
);
return
0
;
}
// python specific constructor from array like object
Mat
m
;
if
(
pyopencv_to
(
obj
,
m
,
ArgInfo
(
"UMatWrapper.np_mat"
,
0
)))
{
self
->
um
=
new
UMat
();
m
.
copyTo
(
*
self
->
um
);
return
0
;
}
}
PyErr_Clear
();
}
PyErr_SetString
(
PyExc_TypeError
,
"no matching UMat constructor found/supported"
);
return
-
1
;
}
}
static
void
UMatWrapper_dealloc
(
cv2_UMatWrapperObject
*
self
)
static
void
UMatWrapper_dealloc
(
cv2_UMatWrapperObject
*
self
)
{
{
delete
self
->
um
;
if
(
self
->
um
)
delete
self
->
um
;
#if PY_MAJOR_VERSION >= 3
#if PY_MAJOR_VERSION >= 3
Py_TYPE
(
self
)
->
tp_free
((
PyObject
*
)
self
);
Py_TYPE
(
self
)
->
tp_free
((
PyObject
*
)
self
);
#else
#else
...
@@ -529,8 +569,12 @@ static PyTypeObject cv2_UMatWrapperType = {
...
@@ -529,8 +569,12 @@ static PyTypeObject cv2_UMatWrapperType = {
#endif
#endif
};
};
static
bool
PyObject_IsUMat
(
PyObject
*
o
)
{
return
(
o
!=
NULL
)
&&
PyObject_TypeCheck
(
o
,
&
cv2_UMatWrapperType
);
}
static
bool
pyopencv_to
(
PyObject
*
o
,
UMat
&
um
,
const
ArgInfo
info
)
{
static
bool
pyopencv_to
(
PyObject
*
o
,
UMat
&
um
,
const
ArgInfo
info
)
{
if
(
o
!=
NULL
&&
PyObject_TypeCheck
(
o
,
&
cv2_UMatWrapperType
)
)
{
if
(
PyObject_IsUMat
(
o
)
)
{
um
=
*
((
cv2_UMatWrapperObject
*
)
o
)
->
um
;
um
=
*
((
cv2_UMatWrapperObject
*
)
o
)
->
um
;
return
true
;
return
true
;
}
}
...
...
modules/python/test/test.py
View file @
205ccddf
...
@@ -123,6 +123,17 @@ class Hackathon244Tests(NewOpenCVTests):
...
@@ -123,6 +123,17 @@ class Hackathon244Tests(NewOpenCVTests):
boost
.
getMaxDepth
()
# from ml::DTrees
boost
.
getMaxDepth
()
# from ml::DTrees
boost
.
isClassifier
()
# from ml::StatModel
boost
.
isClassifier
()
# from ml::StatModel
def
test_umat_construct
(
self
):
data
=
np
.
random
.
random
([
512
,
512
])
# UMat constructors
data_um
=
cv2
.
UMat
(
data
)
# from ndarray
data_sub_um
=
cv2
.
UMat
(
data_um
,
[
0
,
256
],
[
0
,
256
])
# from UMat
data_dst_um
=
cv2
.
UMat
(
256
,
256
,
cv2
.
CV_64F
)
# from size/type
# simple test
cv2
.
multiply
(
data_sub_um
,
2.
,
dst
=
data_dst_um
)
assert
np
.
allclose
(
2.
*
data
[:
256
,
:
256
],
data_dst_um
.
get
())
def
test_umat_matching
(
self
):
def
test_umat_matching
(
self
):
img1
=
self
.
get_sample
(
"samples/data/right01.jpg"
)
img1
=
self
.
get_sample
(
"samples/data/right01.jpg"
)
img2
=
self
.
get_sample
(
"samples/data/right02.jpg"
)
img2
=
self
.
get_sample
(
"samples/data/right02.jpg"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment