diff --git a/tippy/statistics.py b/tippy/statistics.py new file mode 100644 index 0000000..0bffd99 --- /dev/null +++ b/tippy/statistics.py @@ -0,0 +1,63 @@ +''' +Contains functions related to statistics processing. + +Created on Dec 11, 2011 + +@author: Julien Lengrand-Lambert +@email: julien@lengrand.fr +''' + +import cv +import sys + +def compute_histogram(img, bin_fact=cv.IPL_DEPTH_8U, min_range=0): + """ + This function is designed to create the color histogram of the image in + input. + Returns a list of histograms, with one item by channel of input image. + The number of bins is 2^bin_fact (256 by default) + min_range is the lowest bin taken. max_range is automatically calculated with + the input image depth. (2^img_depth) + + NOTE : Supports only 1 channel / 8 bits images for now. + More should be added soon. + """ + try: + cv.GetSize(img) + except TypeError: + raise TypeError("(%s) img : IplImage expected!" % (sys._getframe().f_code.co_name)) + + # img test + if not(img.depth == cv.IPL_DEPTH_8U): + raise TypeError("(%s) 8U image expected!" % (sys._getframe().f_code.co_name)) + elif not(img.nChannels is 1): + raise TypeError("(%s) 1C image expected!" % (sys._getframe().f_code.co_name)) + + # bin_fact test + if (bin_fact <= 0 or ((bin_fact % 2) != 0)): + raise ValueError("(%s) Positive odd integer expected!" % (sys._getframe().f_code.co_name)) + elif type(bin_fact) != int: + raise TypeError("(%s) Positive odd integer expected!" % (sys._getframe().f_code.co_name)) + + # min_range test + if type(min_range) != int: + raise TypeError("(%s) Positive odd integer expected!" % (sys._getframe().f_code.co_name)) + + bins = pow(2, bin_fact) + max_range = pow(2, img.depth) + ranges = [min_range, max_range] + + dims = [bins] + all_ranges = [ranges] + + hist = cv.CreateHist( dims, cv.CV_HIST_ARRAY, all_ranges, uniform=1) + cv.CalcHist([img], hist) + + return hist + +def hist2table(hist): + """ + Transform an histogram into a (numpy) table of values. + This way, it is easier to process. + """ + pass \ No newline at end of file diff --git a/tippy/tests/test_statistics.py b/tippy/tests/test_statistics.py new file mode 100644 index 0000000..be06566 --- /dev/null +++ b/tippy/tests/test_statistics.py @@ -0,0 +1,50 @@ +''' +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_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 + # st.compute_histogram(self.img_1c) + self.assertRaises(TypeError, lambda: st.compute_histogram(self.val) ) + self.assertRaises(TypeError, lambda: st.compute_histogram(self.img_3c) ) + + # testing bin_fact + self.assertRaises(ValueError, lambda: st.compute_histogram(self.img_1c, self.neg_val) ) + self.assertRaises(TypeError, lambda: st.compute_histogram(self.img_1c, self.string) ) + + # testing min_range. It can be either negative or even. + self.assertRaises(TypeError, lambda: st.compute_histogram(self.img_1c, self.val, self.string) ) + +#if __name__ == "__main__": + #import sys;sys.argv = ['', 'Test.testName'] + #unittest.main() \ No newline at end of file