From cd46a674d12f9b10c6c8e4d999cd8cc5b3366761 Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Mon, 28 Jan 2013 18:30:20 +0400 Subject: [PATCH 1/4] applied patch #2611 that also likely fixes #2505 --- modules/python/src2/cv2.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/python/src2/cv2.cpp b/modules/python/src2/cv2.cpp index 28cf00eac2..72d8351c68 100644 --- a/modules/python/src2/cv2.cpp +++ b/modules/python/src2/cv2.cpp @@ -236,6 +236,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 +246,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 From a519bbc6171b58ae9dd46a40bfd98e91741169f4 Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Mon, 28 Jan 2013 20:44:47 +0400 Subject: [PATCH 2/4] Extended python bindings to support scalar values and tuples in place of InputArray (i.e. Mat) - ticket #2658. Added tests for #2611, #2505, #2658 --- modules/python/test/test2.py | 49 ++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 modules/python/test/test2.py diff --git a/modules/python/test/test2.py b/modules/python/test/test2.py new file mode 100644 index 0000000000..b15d0d294e --- /dev/null +++ b/modules/python/test/test2.py @@ -0,0 +1,49 @@ +#/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) + +if __name__ == '__main__': + print "testing", cv.__version__ + random.seed(0) + unittest.main() From 2320ec76b43ea6af85de4e0fa2ef6be68b269022 Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Mon, 28 Jan 2013 20:45:00 +0400 Subject: [PATCH 3/4] Extended python bindings to support scalar values and tuples in place of InputArray (i.e. Mat) - ticket #2658. Added tests for #2611, #2505, #2658 --- modules/python/src2/cv2.cpp | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/modules/python/src2/cv2.cpp b/modules/python/src2/cv2.cpp index 72d8351c68..7e182273b5 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; } From 4044fbcb337e57d3b9bac32cb72a6a1605ec835d Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Mon, 28 Jan 2013 21:03:59 +0400 Subject: [PATCH 4/4] hopefully fixed handling of 'long' Python type in OpenCV bindings (bug #2193). added the corresponding test --- modules/python/src2/cv2.cpp | 16 ++++++++++++++-- modules/python/test/test2.py | 5 +++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/modules/python/src2/cv2.cpp b/modules/python/src2/cv2.cpp index 7e182273b5..02e1b46424 100644 --- a/modules/python/src2/cv2.cpp +++ b/modules/python/src2/cv2.cpp @@ -476,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(); } @@ -741,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 index b15d0d294e..c7491ad974 100644 --- a/modules/python/test/test2.py +++ b/modules/python/test/test2.py @@ -42,6 +42,11 @@ class Hackathon244Tests(NewOpenCVTests): 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__