3 """A simple TestCase class for testing the adaptors.py module.
5 2007-11-xx, Vicent Mas <vmas@carabos.com> Carabos Coop. V.
6 2007-11-08, minor modifications for distribution, Mark Asbach <asbach@ient.rwth-aachen.de>
22 class AdaptorsTestCase(unittest.TestCase):
23 def test00_array_interface(self):
24 """Check if PIL supports the array interface."""
25 self.assert_(PIL.Image.VERSION>='1.1.6',
26 """The installed PIL library doesn't support the array """
27 """interface. Please, update to version 1.1.6b2 or higher.""")
30 def test01_PIL2NumPy(self):
31 """Test the adaptors.PIL2NumPy function."""
33 a = adaptors.PIL2NumPy(self.pil_image)
34 self.assert_(a.flags['WRITEABLE'] == True,
35 'PIL2NumPy should return a writeable array.')
36 b = numpy.asarray(self.pil_image)
37 self.assert_((a == b).all() == True,
38 'The returned numpy array has not been properly constructed.')
41 def test02_NumPy2PIL(self):
42 """Test the adaptors.NumPy2PIL function."""
44 a = numpy.asarray(self.pil_image)
45 b = adaptors.NumPy2PIL(a)
46 self.assert_(self.pil_image.tostring() == b.tostring(),
47 'The returned image has not been properly constructed.')
50 def test03_Ipl2PIL(self):
51 """Test the adaptors.Ipl2PIL function."""
53 i = adaptors.Ipl2PIL(self.ipl_image)
54 self.assert_(self.pil_image.tostring() == i.tostring(),
55 'The returned image has not been properly constructed.')
58 def test04_PIL2Ipl(self):
59 """Test the adaptors.PIL2Ipl function."""
61 i = adaptors.PIL2Ipl(self.pil_image)
62 self.assert_(self.ipl_image.imageData == i.imageData,
63 'The returned image has not been properly constructed.')
66 def test05_Ipl2NumPy(self):
67 """Test the adaptors.Ipl2NumPy function."""
69 a = adaptors.Ipl2NumPy(self.ipl_image)
70 a_1d = numpy.reshape(a, (a.size, ))
71 # For 3-channel IPL images the order of channels will be BGR
72 # but NumPy array order of channels will be RGB so a conversion
73 # is needed before we can compare both images
74 if self.ipl_image.nChannels == 3:
75 rgb = cv.cvCreateImage(cv.cvSize(self.ipl_image.width, self.ipl_image.height), self.ipl_image.depth, 3)
76 cv.cvCvtColor(self.ipl_image, rgb, cv.CV_BGR2RGB)
77 self.assert_(a_1d.tostring() == rgb.imageData,
78 'The returned image has not been properly constructed.')
80 self.assert_(a_1d.tostring() == self.ipl_image.imageData,
81 'The returned image has not been properly constructed.')
84 def test06_NumPy2Ipl(self):
85 """Test the adaptors.NumPy2Ipl function."""
87 a = adaptors.Ipl2NumPy(self.ipl_image)
88 b = adaptors.NumPy2Ipl(a)
89 self.assert_(self.ipl_image.imageData == b.imageData,
90 'The returned image has not been properly constructed.')
92 def load_image( self, fname ):
93 self.ipl_image = highgui.cvLoadImage(fname, 4|2)
94 self.pil_image = PIL.Image.open(fname, 'r')
96 class AdaptorsTestCase1(AdaptorsTestCase):
98 self.load_image( os.path.join(cvtestutils.datadir(),'images','cvSetMouseCallback.jpg'))
100 class AdaptorsTestCase2(AdaptorsTestCase):
102 self.load_image( os.path.join(cvtestutils.datadir(),'images','baboon.jpg'))
106 cases.append( unittest.TestLoader().loadTestsFromTestCase( AdaptorsTestCase1 ) )
107 cases.append( unittest.TestLoader().loadTestsFromTestCase( AdaptorsTestCase2 ) )
109 return unittest.TestSuite(cases)
111 if __name__ == '__main__':
112 unittest.TextTestRunner(verbosity=2).run(suite())