Update the changelog
[opencv] / tests / 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 python
16 from python import cv
17 from python import highgui
18 from python import adaptors
19
20 class AdaptorsTestCase(unittest.TestCase):
21     def test00_array_interface(self):
22         """Check if PIL supports the array interface."""
23         self.assert_(PIL.Image.VERSION>='1.1.6',
24             """The installed PIL library doesn't support the array """
25             """interface. Please, update to version 1.1.6b2 or higher.""")
26
27
28     def test01_PIL2NumPy(self):
29         """Test the adaptors.PIL2NumPy function."""
30
31         a = adaptors.PIL2NumPy(pil_image)
32         self.assert_(a.flags['WRITEABLE'] == True,
33             'PIL2NumPy should return a writeable array.')
34         b = numpy.asarray(pil_image)
35         self.assert_((a == b).all() == True,
36             'The returned numpy array has not been properly constructed.')
37
38
39     def test02_NumPy2PIL(self):
40         """Test the adaptors.NumPy2PIL function."""
41
42         a = numpy.asarray(pil_image)
43         b = adaptors.NumPy2PIL(a)
44         self.assert_(pil_image.tostring() == b.tostring(),
45             'The returned image has not been properly constructed.')
46
47
48     def test03_Ipl2PIL(self):
49         """Test the adaptors.Ipl2PIL function."""
50     
51         i = adaptors.Ipl2PIL(ipl_image)
52         self.assert_(pil_image.tostring() == i.tostring(),
53             'The returned image has not been properly constructed.')
54
55
56     def test04_PIL2Ipl(self):
57         """Test the adaptors.PIL2Ipl function."""
58
59         i = adaptors.PIL2Ipl(pil_image)
60         self.assert_(ipl_image.imageData == i.imageData,
61             'The returned image has not been properly constructed.')
62
63
64     def test05_Ipl2NumPy(self):
65         """Test the adaptors.Ipl2NumPy function."""
66     
67         a = adaptors.Ipl2NumPy(ipl_image)
68         a_1d = numpy.reshape(a, (a.size, ))
69         # For 3-channel IPL images  the order of channels will be BGR
70         # but NumPy array order of channels will be RGB so a conversion
71         # is needed before we can compare both images
72         if ipl_image.nChannels == 3:
73             rgb = cv.cvCreateImage(cv.cvSize(ipl_image.width, ipl_image.height), ipl_image.depth, 3)
74             cv.cvCvtColor(ipl_image, rgb, cv.CV_BGR2RGB)
75             self.assert_(a_1d.tostring() == rgb.imageData,
76                 'The returned image has not been properly constructed.')
77         else:
78             self.assert_(a_1d.tostring() == ipl_image.imageData,
79                 'The returned image has not been properly constructed.')
80
81
82     def test06_NumPy2Ipl(self):
83         """Test the adaptors.NumPy2Ipl function."""
84
85         a = adaptors.Ipl2NumPy(ipl_image)
86         b = adaptors.NumPy2Ipl(a)
87         self.assert_(ipl_image.imageData == b.imageData,
88             'The returned image has not been properly constructed.')
89
90
91 if __name__ == '__main__':
92     global pil_image, ipl_image
93     gray_sample = os.environ['top_srcdir']+'/tests/python/testdata/images/cvSetMouseCallback.jpg'
94     rgb_sample  = os.environ['top_srcdir']+'/tests/python/testdata/images/baboon.jpg'
95     for sample in (gray_sample, rgb_sample):
96         print sample
97         ipl_image = highgui.cvLoadImage(sample, 4|2)
98         pil_image = PIL.Image.open(sample, 'r')
99         suite = unittest.TestLoader().loadTestsFromTestCase(AdaptorsTestCase)
100         unittest.TextTestRunner().run(suite)
101