Update to 2.0.0 tree from current Fremantle build
[opencv] / tests / swig_python / test_adaptors.py
1 #!/usr/bin/env python
2
3 """A simple TestCase class for testing the adaptors.py module.
4
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>
7 """
8
9 import unittest
10 import os
11
12 import PIL.Image
13 import numpy
14
15 import cvtestutils
16 import cv
17 import highgui
18 import adaptors
19
20 import sys
21
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.""")
28
29
30     def test01_PIL2NumPy(self):
31         """Test the adaptors.PIL2NumPy function."""
32
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.')
39
40
41     def test02_NumPy2PIL(self):
42         """Test the adaptors.NumPy2PIL function."""
43
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.')
48
49
50     def test03_Ipl2PIL(self):
51         """Test the adaptors.Ipl2PIL function."""
52     
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.')
56
57
58     def test04_PIL2Ipl(self):
59         """Test the adaptors.PIL2Ipl function."""
60
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.')
64
65
66     def test05_Ipl2NumPy(self):
67         """Test the adaptors.Ipl2NumPy function."""
68     
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.')
79         else:
80             self.assert_(a_1d.tostring() == self.ipl_image.imageData,
81                 'The returned image has not been properly constructed.')
82
83
84     def test06_NumPy2Ipl(self):
85         """Test the adaptors.NumPy2Ipl function."""
86
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.')
91
92     def load_image( self, fname ):
93         self.ipl_image = highgui.cvLoadImage(fname, 4|2)
94         self.pil_image = PIL.Image.open(fname, 'r')
95
96 class AdaptorsTestCase1(AdaptorsTestCase):
97     def setUp( self ):
98         self.load_image( os.path.join(cvtestutils.datadir(),'images','cvSetMouseCallback.jpg'))
99
100 class AdaptorsTestCase2(AdaptorsTestCase):
101     def setUp( self ):
102         self.load_image( os.path.join(cvtestutils.datadir(),'images','baboon.jpg'))
103
104 def suite():
105     cases=[]
106     cases.append( unittest.TestLoader().loadTestsFromTestCase( AdaptorsTestCase1 ) )
107     cases.append( unittest.TestLoader().loadTestsFromTestCase( AdaptorsTestCase2 ) )
108
109     return unittest.TestSuite(cases)
110
111 if __name__ == '__main__':
112         unittest.TextTestRunner(verbosity=2).run(suite())
113