Adds 16 bits support to histogram calculation

Modifies code to accept any encoding depth.
Updates tests to fit with code modifications.
Adds example in order to present a strange case with different encoding
depth and actual pixel values.
Adds an image that fits with the example

Reported-by: Julien Lengrand-Lambert
Signed-off-by: Julien Lengrand-Lambert <julien@lengrandlambert.fr>
This commit is contained in:
Julien Lengrand-Lambert
2011-12-29 12:08:03 +01:00
parent 19dfe78503
commit cce1449a9a
4 changed files with 21 additions and 49 deletions

View File

@@ -21,18 +21,23 @@ import cv
import tippy.statistics as st
import tippy.display_operations as do
<<<<<<< HEAD
# test with 8 bits color image
img_name = "tippy/data/tippy.jpg"
img = cv.LoadImage(img_name, cv.CV_LOAD_IMAGE_COLOR)
hist = st.Histogram(img)
imgs = hist.to_images() # list of Red, Green, Blue channels
do.display_single_image(imgs[2], "Blue channel histogram of the tippy image")
=======
img_name = "tippy/data/gnu.jpg"
img = cv.LoadImage(img_name, cv.CV_LOAD_IMAGE_COLOR)
hist = st.Histogram(img)
hist_img = hist.hist2image()
do.display_single_image(hist_img[0], "Gray-Level histogram of the gnu image")
# test 16 bits histogram, with 4096 range
# special tortuous case :
# 12 bits encoded image
# Taken as 16 bits image, only histogram range to be changed.
img_name = "tippy/data/peaks.tif"
img = cv.LoadImage(img_name, cv.CV_LOAD_IMAGE_UNCHANGED)
img_depth = 12
hist = st.Histogram(img, img_depth)
tables = hist.to_tables() # list of values. Only one item in list
print max(tables[0]) # search for the max value of histogram

BIN
tippy/data/peaks.tif Normal file

Binary file not shown.

View File

@@ -46,9 +46,9 @@ class Histogram():
% (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))
#if not(img.depth == cv.IPL_DEPTH_8U):
# raise TypeError("(%s) 8U image expected!"
# % (sys._getframe().f_code.co_name))
if img.nChannels not in [1, 3]:
raise ValueError("(%s) 1 or 3 channels image expected!"
% (sys._getframe().f_code.co_name))
@@ -68,7 +68,7 @@ class Histogram():
self.channels = img.nChannels
self.nb_bins = int(pow(2, bin_fact))
self.depth = int(img.depth)
max_range = pow(2, self.depth)
max_range = pow(2, bin_fact) + min_range - 1
self.ranges = [min_range, max_range]
img_list = []
@@ -88,13 +88,8 @@ class Histogram():
def _compute_1ch_histogram(self, img_list):
"""
<<<<<<< HEAD
DESIGNED FOR INTERNAL USE ONLY.
BE CAREFUL, NO VERIFICATIONS PERFORMED
=======
DESIGNED FOR INTERNAL USE ONLY
CAREFUL, NO VERIFICATIONS PERFORMED
>>>>>>> 335108ecf6cb601da09717b94407e54e677b9cee
"""
dims = [self.nb_bins]
all_ranges = [self.ranges]
@@ -102,11 +97,10 @@ class Histogram():
hist_list = []
for img in img_list:
hist = cv.CreateHist( dims,
cv.CV_HIST_ARRAY,
all_ranges,
uniform=1)
cv.CV_HIST_ARRAY,
all_ranges,
uniform=1)
cv.CalcHist([img], hist)
hist_list.append(hist)
return hist_list

View File

@@ -36,7 +36,7 @@ class Test(unittest.TestCase):
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(TypeError, lambda:st.Histogram(self.img_16s) )
self.assertRaises(ValueError, lambda:st.Histogram(self.img_2c) )
# testing bin_fact
self.assertRaises(ValueError,
@@ -47,7 +47,6 @@ class Test(unittest.TestCase):
# 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)
@@ -71,7 +70,6 @@ class Test(unittest.TestCase):
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)
@@ -79,31 +77,6 @@ class Test(unittest.TestCase):
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