Adds histogram support to 3 channels images

Finishes support for 3 channels images.
Adds a function allowing to extract only one desired channel
Updates the example to extract all channels.

Support for 16 and 32 bits images should be tested.

Function for 3c to 1c to be added quickly!

Reported-by: Julien Lengrand-Lambert <julien@lengrand.fr>
Signed-off-by: Julien Lengrand-Lambert <julien@lengrand.fr>
This commit is contained in:
Julien Lengrand-Lambert
2011-12-19 14:49:35 +01:00
parent fe6ac64b4b
commit e9bdac2f15
2 changed files with 323 additions and 0 deletions

View File

@@ -4,4 +4,126 @@ Created on Dec 11, 2011
@author: Julien Lengrand-Lambert
@email: julien@lengrand.fr
'''
import unittest
import cv
import tippy.statistics as st
class Test(unittest.TestCase):
def setUp(self):
"""
This method is called before each test
"""
self.img_1c = cv.LoadImage("data/tippy.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE) # 1 channel image
self.img_3c = cv.LoadImage("data/tippy.jpg", cv.CV_LOAD_IMAGE_COLOR) # 3 channel image
self.img_2c = cv.CreateImage((10, 10), cv.IPL_DEPTH_8U, 2) # fake 2 channels image
self.img_16s = cv.CreateImage((15, 15), cv.IPL_DEPTH_16S, 1) # Non 8 bits image
self.val = 10 # non Image
self.neg_val = -10 # non Image
self.string = "A" # non Image
def tearDown(self):
"""
This method is called after each test
"""
pass
#---
## TESTS
def test_compute_histogram(self):
# testing input image
self.assertRaises(TypeError, lambda:st.Histogram(self.val) )
self.assertRaises(TypeError, lambda:st.Histogram(self.img_16s) )
self.assertRaises(ValueError, lambda:st.Histogram(self.img_2c) )
# testing bin_fact
self.assertRaises(ValueError,
lambda: st.Histogram(self.img_1c, self.neg_val))
self.assertRaises(TypeError,
lambda: st.Histogram(self.img_1c, self.string))
# testing min_range. It can be either negative or even.
self.assertRaises(TypeError,
lambda: st.Histogram(self.img_1c, self.val, self.string))
<<<<<<< HEAD
# testing output
hist_1c = st.Histogram(self.img_1c)
self.assertEquals(self.img_1c.nChannels, hist_1c.channels)
hist_3c = st.Histogram(self.img_3c)
self.assertEquals(self.img_3c.nChannels, hist_3c.channels)
def test_to_tables(self):
# testing output size
hist = st.Histogram(self.img_3c)
histable = hist.to_tables()
self.assertEqual(3, len(histable))
self.assertEqual(256, len(histable[0]))
def test_channel_to_image(self):
hist = st.Histogram(self.img_3c)
# testing channel value
self.assertRaises(ValueError,
lambda: hist.channel_to_image(4))
self.assertRaises(TypeError,
lambda: hist.channel_to_image(self.string))
self.assertRaises(TypeError,
lambda: hist.channel_to_image(self.neg_val))
=======
# testing output
hist_1c = st.Histogram(self.img_1c)
self.assertEquals(self.img_1c.nChannels, hist_1c.channels)
hist_3c = st.Histogram(self.img_3c)
self.assertEquals(self.img_3c.nChannels, hist_3c.channels)
def test_hist2table(self):
# testing output size
hist = st.Histogram(self.img_3c)
histable = hist.hist2table()
self.assertEqual(3, len(histable))
self.assertEqual(256, len(histable[0]))
def test_hist2image(self):
hist = st.Histogram(self.img_1c)
>>>>>>> 335108ecf6cb601da09717b94407e54e677b9cee
# testing scale inputs
self.assertRaises(TypeError,
lambda: hist.channel_to_image(1, self.string))
self.assertRaises(TypeError,
lambda: hist.channel_to_image(1, self.neg_val))
self.assertRaises(TypeError,
lambda: hist.channel_to_image(1, self.val, self.string))
self.assertRaises(TypeError,
lambda: hist.channel_to_image(1, self.val, self.neg_val))
# testing range inputs
self.assertRaises(TypeError,
lambda: hist.channel_to_image(1, self.val, self.val, self.string))
self.assertRaises(TypeError,
lambda: hist.channel_to_image(1, self.val, self.val, self.neg_val))
def test_to_images(self):
hist = st.Histogram(self.img_3c)
# testing scale inputs
self.assertRaises(TypeError,
lambda: hist.to_images(self.string))
self.assertRaises(TypeError,
lambda: hist.to_images(self.neg_val))
self.assertRaises(TypeError,
lambda: hist.to_images(self.val, self.string))
self.assertRaises(TypeError,
lambda: hist.to_images(self.val, self.neg_val))
# testing range inputs
self.assertRaises(TypeError,
lambda: hist.to_images(self.val, self.val, self.string))
self.assertRaises(TypeError,
lambda: hist.to_images(self.val, self.val, self.neg_val))
# testing outputs
self.assertEqual(hist.channels, len(hist.to_images()))
#if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
#unittest.main()