• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2
3import urllib
4import cv2.cv as cv
5import Image
6import unittest
7
8class TestLoadImage(unittest.TestCase):
9    def setUp(self):
10        open("large.jpg", "w").write(urllib.urlopen("http://www.cs.ubc.ca/labs/lci/curious_george/img/ROS_bug_imgs/IMG_3560.jpg").read())
11
12    def test_load(self):
13        pilim = Image.open("large.jpg")
14        cvim = cv.LoadImage("large.jpg")
15        self.assert_(len(pilim.tostring()) == len(cvim.tostring()))
16
17class Creating(unittest.TestCase):
18    size=(640, 480)
19    repeat=100
20    def test_0_Create(self):
21        image = cv.CreateImage(self.size, cv.IPL_DEPTH_8U, 1)
22        cnt=cv.CountNonZero(image)
23        self.assertEqual(cnt, 0, msg="Created image is not black. CountNonZero=%i" % cnt)
24
25    def test_2_CreateRepeat(self):
26        cnt=0
27        for i in range(self.repeat):
28            image = cv.CreateImage(self.size, cv.IPL_DEPTH_8U, 1)
29            cnt+=cv.CountNonZero(image)
30        self.assertEqual(cnt, 0, msg="Created images are not black. Mean CountNonZero=%.3f" % (1.*cnt/self.repeat))
31
32    def test_2a_MemCreated(self):
33        cnt=0
34        v=[]
35        for i in range(self.repeat):
36            image = cv.CreateImage(self.size, cv.IPL_DEPTH_8U, 1)
37            cv.FillPoly(image, [[(0, 0), (0, 100), (100, 0)]], 0)
38            cnt+=cv.CountNonZero(image)
39            v.append(image)
40        self.assertEqual(cnt, 0, msg="Memorized images are not black. Mean CountNonZero=%.3f" % (1.*cnt/self.repeat))
41
42    def test_3_tostirng(self):
43        image = cv.CreateImage(self.size, cv.IPL_DEPTH_8U, 1)
44        image.tostring()
45        cnt=cv.CountNonZero(image)
46        self.assertEqual(cnt, 0, msg="After tostring(): CountNonZero=%i" % cnt)
47
48    def test_40_tostringRepeat(self):
49        cnt=0
50        image = cv.CreateImage(self.size, cv.IPL_DEPTH_8U, 1)
51        cv.Set(image, cv.Scalar(0,0,0,0))
52        for i in range(self.repeat*100):
53            image.tostring()
54        cnt=cv.CountNonZero(image)
55        self.assertEqual(cnt, 0, msg="Repeating tostring(): Mean CountNonZero=%.3f" % (1.*cnt/self.repeat))
56
57    def test_41_CreateToStringRepeat(self):
58        cnt=0
59        for i in range(self.repeat*100):
60            image = cv.CreateImage(self.size, cv.IPL_DEPTH_8U, 1)
61            cv.Set(image, cv.Scalar(0,0,0,0))
62            image.tostring()
63            cnt+=cv.CountNonZero(image)
64        self.assertEqual(cnt, 0, msg="Repeating create and tostring(): Mean CountNonZero=%.3f" % (1.*cnt/self.repeat))
65
66    def test_4a_MemCreatedToString(self):
67        cnt=0
68        v=[]
69        for i in range(self.repeat):
70            image = cv.CreateImage(self.size, cv.IPL_DEPTH_8U, 1)
71            cv.Set(image, cv.Scalar(0,0,0,0))
72            image.tostring()
73            cnt+=cv.CountNonZero(image)
74            v.append(image)
75        self.assertEqual(cnt, 0, msg="Repeating and memorizing after tostring(): Mean CountNonZero=%.3f" % (1.*cnt/self.repeat))
76
77if __name__ == '__main__':
78    unittest.main()
79