-
Notifications
You must be signed in to change notification settings - Fork 1
/
gpumeanshift.cpp
86 lines (67 loc) · 1.79 KB
/
gpumeanshift.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//#include <Python.h>
#include <boost/python.hpp>
#include <iostream>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/gpu/gpu.hpp"
#include "conversion.h"
using namespace cv;
using namespace std;
namespace py = boost::python;
PyObject*
filter(PyObject *array, int Range = 10, int Spatial = 10)
{
if(Range < 0 || Spatial < 0)
{
PyErr_SetString(PyExc_ValueError, "Spatial and range radius must be greater than 0\n");
py::throw_error_already_set();
}
if(!PyArray_Check(array))
{
PyErr_SetString(PyExc_TypeError, "First argument is not a numpy array.");
py::throw_error_already_set();
}
int typenum = PyArray_TYPE(array), new_typenum = typenum;
int type = typenum == NPY_UBYTE ? CV_8U :
typenum == NPY_USHORT ? CV_16U :
typenum == NPY_FLOAT ? CV_32F : -1;
if( type < 0 )
{
PyErr_SetString(PyExc_TypeError, "Type is not supported");
py::throw_error_already_set();
}
int ndims = PyArray_NDIM(array);
if(ndims >= 5)
{
PyErr_SetString(PyExc_TypeError, "dimensions is to high");
py::throw_error_already_set();
}else if(ndims < 3)
{
PyErr_SetString(PyExc_TypeError, " dimension to low: need 3 or 4 dims");
py::throw_error_already_set();
}
cv::Mat processedImage ;
NDArrayConverter cvt;
cv::Mat src = cvt.toMat(array);
cv::Mat dst_cpu;
if(ndims == 4)
dst_cpu = src.clone();
else
cv::cvtColor(src, dst_cpu, CV_BGR2BGRA);
cv::gpu::GpuMat d_cvImage(dst_cpu);
cv::gpu::GpuMat d_processedImage(dst_cpu);
cv::gpu::meanShiftFiltering(d_cvImage, d_processedImage, Range, Spatial);
d_processedImage.download(processedImage);
PyObject* OutPut = cvt.toNDArray(dst_cpu);
return OutPut;
}
static void init()
{
Py_Initialize();
import_array();
}
BOOST_PYTHON_MODULE(gpumeanshift)
{
init();
py::def("filter", filter );
}