cv.WaitKey()
cv.DestroyAllWindows()
+ 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 PreliminaryTests(OpenCVTests):
self.assert_(nd == 3)
self.assert_((nc * nr * nd) == elems)
- return # XXX - blocked by fixes for #166, #150
-
# Now test ReshapeMatND
- mat = cv.CreateMatND([2, 2, 2], cv.CV_32F)
- print cv.ReshapeMatND(mat, 0, []);
+ mat = cv.CreateMatND([24], cv.CV_32F)
+ cv.Set(mat, 1.0)
+ self.assertEqual(cv.GetDims(cv.ReshapeMatND(mat, 0, [])), (24, 1))
+ self.assertEqual(cv.GetDims(cv.ReshapeMatND(mat, 0, [1])), (6, 4))
def test_Save(self):
for o in [ cv.CreateImage((128,128), cv.IPL_DEPTH_8U, 1), cv.CreateMat(16, 16, cv.CV_32FC1) ]:
def convert(numpydims):
""" Create a numpy array with specified dims, return the OpenCV CvMat """
- a1 = numpy.array([1] * reduce(operator.__mul__, numpydims)).reshape(*numpydims)
+ a1 = numpy.array([1] * reduce(operator.__mul__, numpydims)).reshape(*numpydims).astype(numpy.float32)
return cv.fromarray(a1)
def row_col_chan(m):
- (col, row) = cv.GetSize(m)
+ col = m.cols
+ row = m.rows
chan = cv.CV_MAT_CN(cv.GetElemType(m))
return (row, col, chan)
# multi-dimensional NumPy array
na = numpy.ones([7,9,2,1,8])
cm = cv.fromarray(na, True)
- print cv.GetDims(cm)
+ self.assertEqual(cv.GetDims(cm), (7,9,2,1,8))
+
+ # Using an array object for a CvArr parameter
+ ones = numpy.ones((640, 480))
+ r = numpy.ones((640, 480))
+ cv.AddS(ones, 7, r)
+ self.assert_(numpy.alltrue(r == (8 * ones)))
else:
print "SKIPPING test_numpy - numpy support not built"
im = cv.CreateImage((128, 128), cv.IPL_DEPTH_8U, 1)
cv.Resize(cv.GetImage(self.get_sample("samples/c/lena.jpg", 0)), im)
dst = cv.CloneImage(im)
+
+ # Check defaults by asserting that all these operations produce the same image
+ funs = [
+ lambda: cv.Dilate(im, dst),
+ lambda: cv.Dilate(im, dst, None),
+ lambda: cv.Dilate(im, dst, iterations = 1),
+ lambda: cv.Dilate(im, dst, element = None),
+ lambda: cv.Dilate(im, dst, iterations = 1, element = None),
+ lambda: cv.Dilate(im, dst, element = None, iterations = 1),
+ ]
+ src_h = self.hashimg(im)
+ hashes = set()
+ for f in funs:
+ f()
+ hashes.add(self.hashimg(dst))
+ self.assertNotEqual(src_h, self.hashimg(dst))
+ # Source image should be untouched
+ self.assertEqual(self.hashimg(im), src_h)
+ # All results should be same
+ self.assertEqual(len(hashes), 1)
+
+ # self.snap(dst)
shapes = [eval("cv.CV_SHAPE_%s" % s) for s in ['RECT', 'CROSS', 'ELLIPSE']]
elements = [cv.CreateStructuringElementEx(sz, sz, sz / 2 + 1, sz / 2 + 1, shape) for sz in [3, 4, 7, 20] for shape in shapes]
elements += [cv.CreateStructuringElementEx(7, 7, 3, 3, cv.CV_SHAPE_CUSTOM, [1] * 49)]
for op in ["OPEN", "CLOSE", "GRADIENT", "TOPHAT", "BLACKHAT"]:
cv.MorphologyEx(im, dst, temp, e, eval("cv.CV_MOP_%s" % op), iter)
- def failing_test_getmat_nd(self):
- # 1D CvMatND should yield 1D CvMat
+ def test_getmat_nd(self):
+ # 1D CvMatND should yield (N,1) CvMat
matnd = cv.CreateMatND([13], cv.CV_8UC1)
- self.assertEqual(cv.GetDims(cv.GetMat(matnd, allowND = True)), (13,))
+ self.assertEqual(cv.GetDims(cv.GetMat(matnd, allowND = True)), (13, 1))
# 2D CvMatND should yield 2D CvMat
- matnd = cv.CreateMatND([11,12], cv.CV_8UC1)
+ matnd = cv.CreateMatND([11, 12], cv.CV_8UC1)
self.assertEqual(cv.GetDims(cv.GetMat(matnd, allowND = True)), (11, 12))
- # 3D CvMatND should yield 1D CvMat
- matnd = cv.CreateMatND([8,8,8], cv.CV_8UC1)
- self.assertEqual(cv.GetDims(cv.GetMat(matnd, allowND = True)), (8 * 8 * 8,))
+ # 3D CvMatND should yield (N,1) CvMat
+ matnd = cv.CreateMatND([7, 8, 9], cv.CV_8UC1)
+ self.assertEqual(cv.GetDims(cv.GetMat(matnd, allowND = True)), (7 * 8 * 9, 1))
def test_clipline(self):
self.assert_(cv.ClipLine((100,100), (-100,0), (500,0)) == ((0,0), (99,0)))
a = self.get_sample("samples/c/lena.jpg", 0)
eig_image = cv.CreateImage(cv.GetSize(a), cv.IPL_DEPTH_32F, 1)
temp_image = cv.CreateImage(cv.GetSize(a), cv.IPL_DEPTH_32F, 1)
- pts = cv.GoodFeaturesToTrack(a, eig_image, temp_image, 100, 0.04, 2, use_harris=1)
+ pts = cv.GoodFeaturesToTrack(a, eig_image, temp_image, 100, 0.04, 2, useHarris=1)
hull = cv.ConvexHull2(pts, cv.CreateMemStorage(), return_points = 1)
cv.FitLine(hull, cv.CV_DIST_L2, 0, 0.01, 0.01)