test hist funcs
This commit is contained in:
parent
27f41d9725
commit
2ce472a153
@ -12,6 +12,7 @@ EXTRA_DIST = \
|
||||
test_colour.py \
|
||||
test_create.py \
|
||||
test_draw.py \
|
||||
test_cum.py \
|
||||
test_convolution.py \
|
||||
test_conversion.py
|
||||
|
||||
|
124
test/test_histogram.py
Executable file
124
test/test_histogram.py
Executable file
@ -0,0 +1,124 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import unittest
|
||||
import math
|
||||
|
||||
#import logging
|
||||
#logging.basicConfig(level = logging.DEBUG)
|
||||
|
||||
from gi.repository import Vips
|
||||
|
||||
# an expanding zip ... if either of the args is a scalar or a one-element list,
|
||||
# duplicate it down the other side
|
||||
def zip_expand(x, y):
|
||||
# handle singleton list case
|
||||
if isinstance(x, list) and len(x) == 1:
|
||||
x = x[0]
|
||||
if isinstance(y, list) and len(y) == 1:
|
||||
y = y[0]
|
||||
|
||||
if isinstance(x, list) and isinstance(y, list):
|
||||
return list(zip(x, y))
|
||||
elif isinstance(x, list):
|
||||
return [[i, y] for i in x]
|
||||
elif isinstance(y, list):
|
||||
return [[x, j] for j in y]
|
||||
else:
|
||||
return [[x, y]]
|
||||
|
||||
class TestHistogram(unittest.TestCase):
|
||||
# test a pair of things which can be lists for approx. equality
|
||||
def assertAlmostEqualObjects(self, a, b, places = 4, msg = ''):
|
||||
# print 'assertAlmostEqualObjects %s = %s' % (a, b)
|
||||
for x, y in zip_expand(a, b):
|
||||
self.assertAlmostEqual(x, y, places = places, msg = msg)
|
||||
|
||||
def test_hist_cum(self):
|
||||
im = Vips.Image.identity()
|
||||
|
||||
sum = im.avg() * 256
|
||||
|
||||
cum = im.hist_cum()
|
||||
|
||||
p = cum.getpoint(255, 0)
|
||||
self.assertEqual(p[0], sum)
|
||||
|
||||
def test_hist_equal(self):
|
||||
im = Vips.Image.new_from_file("images/IMG_4618.jpg")
|
||||
|
||||
im2 = im.hist_equal()
|
||||
|
||||
self.assertEqual(im.width, im2.width)
|
||||
self.assertEqual(im.height, im2.height)
|
||||
|
||||
self.assertTrue(im.avg() < im2.avg())
|
||||
self.assertTrue(im.deviate() < im2.deviate())
|
||||
|
||||
def test_hist_ismonotonic(self):
|
||||
im = Vips.Image.identity()
|
||||
self.assertTrue(im.hist_ismonotonic())
|
||||
|
||||
def test_hist_local(self):
|
||||
im = Vips.Image.new_from_file("images/IMG_4618.jpg")
|
||||
|
||||
im2 = im.hist_local(10, 10)
|
||||
|
||||
self.assertEqual(im.width, im2.width)
|
||||
self.assertEqual(im.height, im2.height)
|
||||
|
||||
self.assertTrue(im.avg() < im2.avg())
|
||||
self.assertTrue(im.deviate() < im2.deviate())
|
||||
|
||||
def test_hist_match(self):
|
||||
im = Vips.Image.identity()
|
||||
im2 = Vips.Image.identity()
|
||||
|
||||
matched = im.hist_match(im2)
|
||||
|
||||
self.assertEqual((im - matched).abs().max(), 0.0)
|
||||
|
||||
def test_hist_norm(self):
|
||||
im = Vips.Image.identity()
|
||||
im2 = im.hist_norm()
|
||||
|
||||
self.assertEqual((im - im2).abs().max(), 0.0)
|
||||
|
||||
def test_hist_plot(self):
|
||||
im = Vips.Image.identity()
|
||||
im2 = im.hist_plot()
|
||||
|
||||
self.assertEqual(im2.width, 256)
|
||||
self.assertEqual(im2.height, 256)
|
||||
self.assertEqual(im2.format, Vips.BandFormat.UCHAR)
|
||||
self.assertEqual(im2.bands, 1)
|
||||
|
||||
def test_hist_map(self):
|
||||
im = Vips.Image.identity()
|
||||
|
||||
im2 = im.maplut(im)
|
||||
|
||||
self.assertEqual((im - im2).abs().max(), 0.0)
|
||||
|
||||
def test_hist_map(self):
|
||||
im = Vips.Image.new_from_file("images/IMG_4618.jpg").extract_band(1)
|
||||
|
||||
pc = im.percent(90)
|
||||
|
||||
msk = im <= pc
|
||||
n_set = (msk.avg() * msk.width * msk.height) / 255.0
|
||||
pc_set = 100 * n_set / (msk.width * msk.height)
|
||||
|
||||
self.assertAlmostEqual(pc_set, 90, places = 0)
|
||||
|
||||
def test_stdif(self):
|
||||
im = Vips.Image.new_from_file("images/IMG_4618.jpg")
|
||||
|
||||
im2 = im.stdif(10, 10)
|
||||
|
||||
self.assertEqual(im.width, im2.width)
|
||||
self.assertEqual(im.height, im2.height)
|
||||
|
||||
self.assertTrue(im.avg() < im2.avg())
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
@ -15,6 +15,8 @@ python2 test_colour.py
|
||||
python2 test_conversion.py
|
||||
python2 test_convolution.py
|
||||
python2 test_create.py
|
||||
python2 test_draw.py
|
||||
python2 test_histogram.py
|
||||
|
||||
echo "testing with python3 ..."
|
||||
|
||||
@ -23,3 +25,5 @@ python3 test_arithmetic.py
|
||||
python3 test_conversion.py
|
||||
python3 test_convolution.py
|
||||
python3 test_create.py
|
||||
python3 test_draw.py
|
||||
python3 test_histogram.py
|
||||
|
Loading…
Reference in New Issue
Block a user