diff --git a/modules/python/src2/cv2.cpp b/modules/python/src2/cv2.cpp index 28cf00eac2..02e1b46424 100644 --- a/modules/python/src2/cv2.cpp +++ b/modules/python/src2/cv2.cpp @@ -224,9 +224,42 @@ static int pyopencv_to(const PyObject* o, Mat& m, const ArgInfo info, bool allow return true; } + if( PyInt_Check(o) ) + { + double v[] = {PyInt_AsLong((PyObject*)o), 0., 0., 0.}; + m = Mat(4, 1, CV_64F, v).clone(); + return true; + } + if( PyFloat_Check(o) ) + { + double v[] = {PyFloat_AsDouble((PyObject*)o), 0., 0., 0.}; + m = Mat(4, 1, CV_64F, v).clone(); + return true; + } + if( PyTuple_Check(o) ) + { + int i, sz = (int)PyTuple_Size((PyObject*)o); + m = Mat(sz, 1, CV_64F); + for( i = 0; i < sz; i++ ) + { + PyObject* oi = PyTuple_GET_ITEM(o, i); + if( PyInt_Check(oi) ) + m.at(i) = (double)PyInt_AsLong(oi); + else if( PyFloat_Check(oi) ) + m.at(i) = (double)PyFloat_AsDouble(oi); + else + { + failmsg("%s is not a numerical tuple", info.name); + m.release(); + return false; + } + } + return true; + } + if( !PyArray_Check(o) ) { - failmsg("%s is not a numpy array", info.name); + failmsg("%s is not a numpy array, neither a scalar", info.name); return false; } @@ -236,6 +269,7 @@ static int pyopencv_to(const PyObject* o, Mat& m, const ArgInfo info, bool allow typenum == NPY_BYTE ? CV_8S : typenum == NPY_USHORT ? CV_16U : typenum == NPY_SHORT ? CV_16S : + typenum == NPY_INT ? CV_32S : typenum == NPY_INT32 ? CV_32S : typenum == NPY_FLOAT ? CV_32F : typenum == NPY_DOUBLE ? CV_64F : -1; @@ -245,7 +279,7 @@ static int pyopencv_to(const PyObject* o, Mat& m, const ArgInfo info, bool allow if( typenum == NPY_INT64 || typenum == NPY_UINT64 || type == NPY_LONG ) { needcopy = needcast = true; - new_typenum = NPY_INT32; + new_typenum = NPY_INT; type = CV_32S; } else @@ -442,7 +476,12 @@ static bool pyopencv_to(PyObject* obj, int& value, const char* name = " (void)name; if(!obj || obj == Py_None) return true; - value = (int)PyInt_AsLong(obj); + if(PyInt_Check(obj)) + value = (int)PyInt_AsLong(obj); + else if(PyLong_Check(obj)) + value = (int)PyLong_AsLong(obj); + else + return false; return value != -1 || !PyErr_Occurred(); } @@ -707,7 +746,14 @@ template struct pyopencvVecConverter PyObject* item_ij = items_i[j]; if( PyInt_Check(item_ij)) { - int v = PyInt_AsLong(item_ij); + int v = (int)PyInt_AsLong(item_ij); + if( v == -1 && PyErr_Occurred() ) + break; + data[j] = saturate_cast<_Cp>(v); + } + else if( PyLong_Check(item_ij)) + { + int v = (int)PyLong_AsLong(item_ij); if( v == -1 && PyErr_Occurred() ) break; data[j] = saturate_cast<_Cp>(v); diff --git a/modules/python/test/test2.py b/modules/python/test/test2.py new file mode 100644 index 0000000000..c7491ad974 --- /dev/null +++ b/modules/python/test/test2.py @@ -0,0 +1,54 @@ +#/usr/bin/env python + +import unittest +import random +import time +import math +import sys +import array +import urllib +import tarfile +import hashlib +import os +import getopt +import operator +import functools +import numpy as np +import cv2 +import cv2.cv as cv + +class NewOpenCVTests(unittest.TestCase): + + def get_sample(self, filename, iscolor = cv.CV_LOAD_IMAGE_COLOR): + if not filename in self.image_cache: + filedata = urllib.urlopen("https://raw.github.com/Itseez/opencv/master/" + filename).read() + self.image_cache[filename] = cv2.imdecode(np.fromstring(filedata, dtype=np.uint8), iscolor) + return self.image_cache[filename] + + def setUp(self): + self.image_cache = {} + + def hashimg(self, im): + """ Compute a hash for an image, useful for image comparisons """ + return hashlib.md5(im.tostring()).digest() + +# Tests to run first; check the handful of basic operations that the later tests rely on + +class Hackathon244Tests(NewOpenCVTests): + + def test_int_array(self): + a = np.array([-1, 2, -3, 4, -5]) + absa0 = np.abs(a) + self.assert_(cv2.norm(a, cv2.NORM_L1) == 15) + absa1 = cv2.absdiff(a, 0) + self.assert_(cv2.norm(absa1, absa0, cv2.NORM_INF) == 0) + + def test_imencode(self): + a = np.zeros((480, 640), dtype=np.uint8) + flag, ajpg = cv2.imencode("img_q90.jpg", a, [cv2.IMWRITE_JPEG_QUALITY, 90]) + self.assert_(flag == True and ajpg.dtype == np.uint8 and ajpg.shape[0] > 1 and ajpg.shape[1] == 1) + +if __name__ == '__main__': + print "testing", cv.__version__ + random.seed(0) + unittest.main()