include_directories(${PYTHON_INCLUDE_PATH})
include_directories(
- "${CMAKE_CURRENT_SOURCE_DIR}/src1"
"${CMAKE_CURRENT_SOURCE_DIR}/src2"
"${OpenCV_SOURCE_DIR}/modules/core/include"
"${OpenCV_SOURCE_DIR}/modules/imgproc/include"
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3")
endif()
-add_custom_command(
- OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/generated0.i
- COMMAND ${PYTHON_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/src1/gen.py" "${CMAKE_CURRENT_SOURCE_DIR}/src1"
- DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src1/api
- DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src1/defs
- DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src1/gen.py
- )
-
-
-set(cv_target "opencv_python")
-add_library(${cv_target} SHARED src1/cv.cpp ${CMAKE_CURRENT_BINARY_DIR}/generated0.i)
-target_link_libraries(${cv_target} ${PYTHON_LIBRARIES} opencv_core opencv_imgproc opencv_video opencv_ml opencv_features2d opencv_highgui opencv_calib3d opencv_objdetect opencv_legacy opencv_contrib)
-
-set_target_properties(${cv_target} PROPERTIES PREFIX "")
-set_target_properties(${cv_target} PROPERTIES OUTPUT_NAME "cv")
-
-execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import distutils.sysconfig; print distutils.sysconfig.get_config_var('SO')"
- RESULT_VARIABLE PYTHON_CVPY_PROCESS
- OUTPUT_VARIABLE CVPY_SUFFIX
- OUTPUT_STRIP_TRAILING_WHITESPACE)
-
-set_target_properties(${cv_target} PROPERTIES SUFFIX ${CVPY_SUFFIX})
-
-if (MSVC AND NOT BUILD_SHARED_LIBS)
- set_target_properties(${cv_target} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:atlthunk.lib /NODEFAULTLIB:atlsd.lib /DEBUG")
-endif()
-
-set(cvpymodules ${cv_target})
-
-if(PYTHON_USE_NUMPY)
-
set(cv2_generated_hdrs
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_funcs.h"
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_func_tab.h"
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_type_reg.h"
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_const_reg.h")
+
add_custom_command(
- OUTPUT ${cv2_generated_hdrs}
- COMMAND ${PYTHON_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/src2/gen2.py" ${CMAKE_CURRENT_BINARY_DIR} ${opencv_hdrs}
- DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src2/gen2.py
- DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src2/hdr_parser.py
- DEPENDS ${opencv_hdrs})
+ OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/generated0.i
+ COMMAND ${PYTHON_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/src2/gen.py" "${CMAKE_CURRENT_SOURCE_DIR}/src2"
+ DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src2/api
+ DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src2/defs
+ DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src2/gen.py
+ )
-set(cv2_target "opencv2_python")
-add_library(${cv2_target} SHARED src2/cv2.cpp src2/opencv_extra_api.hpp ${cv2_generated_hdrs})
+add_custom_command(
+ OUTPUT ${cv2_generated_hdrs}
+ COMMAND ${PYTHON_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/src2/gen2.py" ${CMAKE_CURRENT_BINARY_DIR} ${opencv_hdrs}
+ DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src2/gen2.py
+ DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src2/hdr_parser.py
+ DEPENDS ${opencv_hdrs})
+
+set(cv2_target "opencv_python")
+add_library(${cv2_target} SHARED src2/cv2.cpp ${CMAKE_CURRENT_BINARY_DIR}/generated0.i src2/opencv_extra_api.hpp ${cv2_generated_hdrs} src2/cv2.cv.hpp)
target_link_libraries(${cv2_target} ${PYTHON_LIBRARIES} opencv_core opencv_imgproc opencv_video opencv_ml opencv_features2d opencv_highgui opencv_calib3d opencv_objdetect opencv_legacy opencv_contrib)
-
+
set_target_properties(${cv2_target} PROPERTIES PREFIX "")
set_target_properties(${cv2_target} PROPERTIES OUTPUT_NAME "cv2")
+
+execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import distutils.sysconfig; print distutils.sysconfig.get_config_var('SO')"
+ RESULT_VARIABLE PYTHON_CVPY_PROCESS
+ OUTPUT_VARIABLE CVPY_SUFFIX
+ OUTPUT_STRIP_TRAILING_WHITESPACE)
+
set_target_properties(${cv2_target} PROPERTIES SUFFIX ${CVPY_SUFFIX})
if (MSVC AND NOT BUILD_SHARED_LIBS)
set_target_properties(${cv2_target} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:atlthunk.lib /NODEFAULTLIB:atlsd.lib /DEBUG")
endif()
-set(cvpymodules ${cvpymodules} ${cv2_target})
-
-endif()
+set(cvpymodules ${cv2_target})
if(WIN32)
install(TARGETS ${cvpymodules}
ARCHIVE DESTINATION "Python${PYTHON_VERSION_MAJOR_MINOR}/Lib/site-packages" COMPONENT main
)
else()
- #install(FILES ${LIBRARY_OUTPUT_PATH}/cv${CVPY_SUFFIX} DESTINATION ${PYTHON_PACKAGES_PATH})
install(TARGETS ${cvpymodules}
RUNTIME DESTINATION ${PYTHON_PACKAGES_PATH} COMPONENT main
LIBRARY DESTINATION ${PYTHON_PACKAGES_PATH} COMPONENT main
-#include <Python.h>
+#include "opencv2/legacy/legacy.hpp"
+#include "opencv2/legacy/compat.hpp"
-#include <assert.h>
-
-#include "opencv/cxcore.h"
-#include "opencv/cv.h"
-#include "opencv/cvaux.h"
-#include "opencv/cvwimage.h"
-#include "opencv/highgui.h"
-
-#define MODULESTR "cv"
-
-static PyObject *opencv_error;
+#define OLD_MODULESTR "cv2.cv"
struct memtrack_t {
PyObject_HEAD
/************************************************************************/
-static int failmsg(const char *fmt, ...)
-{
- char str[1000];
-
- va_list ap;
- va_start(ap, fmt);
- vsnprintf(str, sizeof(str), fmt, ap);
- va_end(ap);
-
- PyErr_SetString(PyExc_TypeError, str);
- return 0;
-}
-
-/************************************************************************/
-
/* These get/setters are polymorphic, used in both iplimage and cvmat */
static PyObject *PyObject_FromCvScalar(CvScalar s, int type)
static PyTypeObject iplimage_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /*size*/
- MODULESTR".iplimage", /*name*/
+ OLD_MODULESTR".iplimage", /*name*/
sizeof(iplimage_t), /*basicsize*/
};
static PyTypeObject cvmat_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /*size*/
- MODULESTR".cvmat", /*name*/
+ OLD_MODULESTR".cvmat", /*name*/
sizeof(cvmat_t), /*basicsize*/
};
static PyTypeObject cvmatnd_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /*size*/
- MODULESTR".cvmatnd", /*name*/
+ OLD_MODULESTR".cvmatnd", /*name*/
sizeof(cvmatnd_t), /*basicsize*/
};
static PyTypeObject cvhistogram_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /*size*/
- MODULESTR".cvhistogram", /*name*/
+ OLD_MODULESTR".cvhistogram", /*name*/
sizeof(cvhistogram_t), /*basicsize*/
};
static PyTypeObject cvlineiterator_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /*size*/
- MODULESTR".cvlineiterator", /*name*/
+ OLD_MODULESTR".cvlineiterator", /*name*/
sizeof(cvlineiterator_t), /*basicsize*/
};
static PyTypeObject memtrack_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /*size*/
- MODULESTR".memtrack", /*name*/
+ OLD_MODULESTR".memtrack", /*name*/
sizeof(memtrack_t), /*basicsize*/
};
static PyTypeObject cvmemstorage_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /*size*/
- MODULESTR".cvmemstorage", /*name*/
+ OLD_MODULESTR".cvmemstorage", /*name*/
sizeof(cvmemstorage_t), /*basicsize*/
};
static PyTypeObject cvfont_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /*size*/
- MODULESTR".cvfont", /*name*/
+ OLD_MODULESTR".cvfont", /*name*/
sizeof(cvfont_t), /*basicsize*/
};
static PyTypeObject cvrng_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /*size*/
- MODULESTR".cvrng", /*name*/
+ OLD_MODULESTR".cvrng", /*name*/
sizeof(cvrng_t), /*basicsize*/
};
static PyTypeObject cvcontourtree_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /*size*/
- MODULESTR".cvcontourtree", /*name*/
+ OLD_MODULESTR".cvcontourtree", /*name*/
sizeof(cvcontourtree_t), /*basicsize*/
};
static PyTypeObject cvsubdiv2dedge_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /*size*/
- MODULESTR".cvsubdiv2dedge", /*name*/
+ OLD_MODULESTR".cvsubdiv2dedge", /*name*/
sizeof(cvsubdiv2dedge_t), /*basicsize*/
};
static PyTypeObject cvseq_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /*size*/
- MODULESTR".cvseq", /*name*/
+ OLD_MODULESTR".cvseq", /*name*/
sizeof(cvseq_t), /*basicsize*/
};
static PyTypeObject cvset_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /*size*/
- MODULESTR".cvset", /*name*/
+ OLD_MODULESTR".cvset", /*name*/
sizeof(cvset_t), /*basicsize*/
};
static PyTypeObject cvsubdiv2d_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /*size*/
- MODULESTR".cvsubdiv2d", /*name*/
+ OLD_MODULESTR".cvsubdiv2d", /*name*/
sizeof(cvsubdiv2d_t), /*basicsize*/
};
static PyTypeObject cvsubdiv2dpoint_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /*size*/
- MODULESTR".cvsubdiv2dpoint", /*name*/
+ OLD_MODULESTR".cvsubdiv2dpoint", /*name*/
sizeof(cvsubdiv2dpoint_t), /*basicsize*/
};
return shareDataND(o, cva, pn);
}
-static void OnMouse(int event, int x, int y, int flags, void* param)
-{
- PyGILState_STATE gstate;
- gstate = PyGILState_Ensure();
-
- PyObject *o = (PyObject*)param;
- PyObject *args = Py_BuildValue("iiiiO", event, x, y, flags, PyTuple_GetItem(o, 1));
-
- PyObject *r = PyObject_Call(PyTuple_GetItem(o, 0), args, NULL);
- if (r == NULL)
- PyErr_Print();
- else
- Py_DECREF(r);
- Py_DECREF(args);
- PyGILState_Release(gstate);
-}
-
-static PyObject *pycvSetMouseCallback(PyObject *self, PyObject *args, PyObject *kw)
-{
- const char *keywords[] = { "window_name", "on_mouse", "param", NULL };
- char* name;
- PyObject *on_mouse;
- PyObject *param = NULL;
-
- if (!PyArg_ParseTupleAndKeywords(args, kw, "sO|O", (char**)keywords, &name, &on_mouse, ¶m))
- return NULL;
- if (!PyCallable_Check(on_mouse)) {
- PyErr_SetString(PyExc_TypeError, "on_mouse must be callable");
- return NULL;
- }
- if (param == NULL) {
- param = Py_None;
- }
- ERRWRAP(cvSetMouseCallback(name, OnMouse, Py_BuildValue("OO", on_mouse, param)));
- Py_RETURN_NONE;
-}
-
-void OnChange(int pos, void *param)
-{
- PyGILState_STATE gstate;
- gstate = PyGILState_Ensure();
-
- PyObject *o = (PyObject*)param;
- PyObject *args = Py_BuildValue("(i)", pos);
- PyObject *r = PyObject_Call(PyTuple_GetItem(o, 0), args, NULL);
- if (r == NULL)
- PyErr_Print();
- Py_DECREF(args);
- PyGILState_Release(gstate);
-}
-
-static PyObject *pycvCreateTrackbar(PyObject *self, PyObject *args)
-{
- PyObject *on_change;
- char* trackbar_name;
- char* window_name;
- int *value = new int;
- int count;
-
- if (!PyArg_ParseTuple(args, "ssiiO", &trackbar_name, &window_name, value, &count, &on_change))
- return NULL;
- if (!PyCallable_Check(on_change)) {
- PyErr_SetString(PyExc_TypeError, "on_change must be callable");
- return NULL;
- }
- ERRWRAP(cvCreateTrackbar2(trackbar_name, window_name, value, count, OnChange, Py_BuildValue("OO", on_change, Py_None)));
- Py_RETURN_NONE;
-}
-
static PyObject *pycvFindContours(PyObject *self, PyObject *args, PyObject *kw)
{
CvArr* image;
#include "generated0.i"
-static PyMethodDef methods[] = {
+static PyMethodDef old_methods[] = {
#if PYTHON_USE_NUMPY
{"fromarray", (PyCFunction)pycvfromarray, METH_KEYWORDS, "fromarray(array) -> cvmatnd"},
#endif
- //{"CalcOpticalFlowFarneback", (PyCFunction)pycvCalcOpticalFlowFarneback, METH_KEYWORDS, "CalcOpticalFlowFarneback(prev, next, flow, pyr_scale=0.5, levels=3, win_size=15, iterations=3, poly_n=7, poly_sigma=1.5, flags=0) -> None"},
- //{"_HOGComputeDescriptors", (PyCFunction)pycvHOGComputeDescriptors, METH_KEYWORDS, "_HOGComputeDescriptors(image, win_stride=block_stride, locations=None, padding=(0,0), win_size=(64,128), block_size=(16,16), block_stride=(8,8), cell_size=(8,8), nbins=9, gammaCorrection=true) -> list_of_descriptors"},
- //{"_HOGDetect", (PyCFunction)pycvHOGDetect, METH_KEYWORDS, "_HOGDetect(image, svm_classifier, win_stride=block_stride, locations=None, padding=(0,0), win_size=(64,128), block_size=(16,16), block_stride=(8,8), cell_size=(8,8), nbins=9, gammaCorrection=true) -> list_of_points"},
- //{"_HOGDetectMultiScale", (PyCFunction)pycvHOGDetectMultiScale, METH_KEYWORDS, "_HOGDetectMultiScale(image, svm_classifier, win_stride=block_stride, scale=1.05, group_threshold=2, padding=(0,0), win_size=(64,128), block_size=(16,16), block_stride=(8,8), cell_size=(8,8), nbins=9, gammaCorrection=true) -> list_of_points"},
-
{"FindDataMatrix", pyfinddatamatrix, METH_VARARGS},
{"temp_test", temp_test, METH_VARARGS},
/************************************************************************/
/* Module init */
-static int to_ok(PyTypeObject *to)
+PyObject* init_cv()
{
- to->tp_alloc = PyType_GenericAlloc;
- to->tp_new = PyType_GenericNew;
- to->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
- return (PyType_Ready(to) == 0);
-}
-
-#define MKTYPE(NAME) NAME##_specials(); if (!to_ok(&NAME##_Type)) return
-
-using namespace cv;
-
-extern "C"
-#if defined WIN32 || defined _WIN32
-__declspec(dllexport)
-#endif
-
-void initcv()
-{
-#if PYTHON_USE_NUMPY
- import_array();
-#endif
-
PyObject *m, *d;
-
cvSetErrMode(CV_ErrModeParent);
+ #define MKTYPE(NAME) NAME##_specials(); to_ok(&NAME##_Type)
+
MKTYPE(cvcontourtree);
MKTYPE(cvfont);
MKTYPE(cvhistogram);
#include "generated4.i"
- m = Py_InitModule(MODULESTR"", methods);
+ #undef MKTYPE
+
+ m = Py_InitModule(OLD_MODULESTR, old_methods);
d = PyModule_GetDict(m);
PyDict_SetItemString(d, "__version__", PyString_FromString("$Rev: 4557 $"));
-
- opencv_error = PyErr_NewException((char*)MODULESTR".error", NULL, NULL);
PyDict_SetItemString(d, "error", opencv_error);
// Couple of warnings about strict aliasing here. Not clear how to fix.
PUBLISH(GC_INIT_WITH_RECT);
PUBLISH(GC_INIT_WITH_MASK);
PUBLISH(GC_EVAL);
-
+
#include "generated2.i"
+
+#undef PUBLISH
+#undef PUBLISHU
+#undef PUBLISH2
+
+ return m;
}
#include "test_precomp.hpp"
-CV_TEST_MAIN("cv")
+//CV_TEST_MAIN("cv")
+
+#if 1
+using namespace cv;
+
+int main(int, char**)
+{
+ Mat prevImg = imread("/Users/vp/work/ocv/opencv_extra/testdata/cv/optflow/rock_1.bmp", 0);
+ Mat nextImg = imread("/Users/vp/work/ocv/opencv_extra/testdata/cv/optflow/rock_2.bmp", 0);
+ FileStorage fs("/Users/vp/work/ocv/opencv_extra/testdata/cv/optflow/lk_prev.dat", FileStorage::READ);
+ vector<Point2f> u, v;
+ Mat _u;
+ fs["points"] >> _u;
+ _u.reshape(2, 0).copyTo(u);
+ vector<uchar> status;
+ vector<float> err;
+ double tmin = 0;
+
+ for( int k = 0; k < 3; k++ )
+ {
+ double t = (double)getTickCount();
+#if 1
+ calcOpticalFlowPyrLK(prevImg, nextImg, u, v, status, err, Size(11,11),
+ 5, TermCriteria(TermCriteria::MAX_ITER+TermCriteria::EPS, 30, 0.01), 100);
+#else
+ {
+ CvMat _prevImg = prevImg;
+ CvMat _nextImg = nextImg;
+ v.resize(u.size());
+ status.resize(u.size());
+ err.resize(u.size());
+ cvCalcOpticalFlowPyrLK(&_prevImg, &_nextImg, 0, 0, (CvPoint2D32f*)&u[0], (CvPoint2D32f*)&v[0], (int)u.size(),
+ cvSize(21, 21), 4, (char*)&status[0],
+ &err[0], cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.01), 0);
+ }
+#endif
+ t = (double)getTickCount() - t;
+ tmin = k == 0 ? t : std::min(tmin, t);
+ }
+ printf("time = %gms\n", tmin*1000./getTickFrequency());
+
+ Mat color;
+ cvtColor(prevImg, color, CV_GRAY2BGR);
+ for( size_t i = 0; i < u.size(); i++ )
+ {
+ Point2f ui = u[i], vi = v[i];
+ if( cvIsNaN(v[i].x) || cvIsNaN(v[i].y) || !status[i] )
+ {
+ const float r = 2.f;
+ line(color, Point2f(u[i].x-r,u[i].y-r), Point2f(u[i].x+r,u[i].y+r), Scalar(0, 0, 255), 1);
+ line(color, Point2f(u[i].x-r,u[i].y+r), Point2f(u[i].x+r,u[i].y-r), Scalar(0, 0, 255), 1);
+ continue;
+ }
+ line(color, ui, vi, Scalar(0, 255, 0), 1);
+ }
+ imshow("flow", color);
+ waitKey();
+ return 0;
+}
+#endif
+