File: test_histogram.py

package info (click to toggle)
vips 8.18.0-2
  • links: PTS
  • area: main
  • in suites: forky
  • size: 53,448 kB
  • sloc: ansic: 172,621; cpp: 12,257; python: 5,077; sh: 773; perl: 40; makefile: 25; javascript: 6
file content (132 lines) | stat: -rw-r--r-- 3,314 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# vim: set fileencoding=utf-8 :
import pytest

import pyvips
from helpers import *


class TestHistogram:
    def test_hist_cum(self):
        im = pyvips.Image.identity()

        sum = im.avg() * 256

        cum = im.hist_cum()

        p = cum(255, 0)
        assert p[0] == sum

    def test_hist_equal(self):
        im = pyvips.Image.new_from_file(JPEG_FILE)

        im2 = im.hist_equal()

        assert im.width == im2.width
        assert im.height == im2.height

        assert im.avg() < im2.avg()
        assert im.deviate() < im2.deviate()

    def test_hist_ismonotonic(self):
        im = pyvips.Image.identity()
        assert im.hist_ismonotonic()

    def test_hist_local(self):
        im = pyvips.Image.new_from_file(JPEG_FILE)

        im2 = im.hist_local(10, 10)

        assert im.width == im2.width
        assert im.height == im2.height

        assert im.avg() < im2.avg()
        assert im.deviate() < im2.deviate()

        im3 = im.hist_local(10, 10, max_slope=3)

        assert im.width == im3.width
        assert im.height == im3.height

        assert im3.deviate() < im2.deviate()

    def test_hist_match(self):
        im = pyvips.Image.identity()
        im2 = pyvips.Image.identity()

        matched = im.hist_match(im2)

        assert (im - matched).abs().max() == 0.0

    def test_hist_norm(self):
        im = pyvips.Image.identity()
        im2 = im.hist_norm()

        assert (im - im2).abs().max() == 0.0

    def test_hist_plot(self):
        im = pyvips.Image.identity()
        im2 = im.hist_plot()

        assert im2.width == 256
        assert im2.height == 256
        assert im2.format == pyvips.BandFormat.UCHAR
        assert im2.bands == 1

    def test_hist_map(self):
        im = pyvips.Image.identity()

        im2 = im.maplut(im)

        assert (im - im2).abs().max() == 0.0

    def test_percent(self):
        im = pyvips.Image.new_from_file(JPEG_FILE).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)

        assert pytest.approx(pc_set, 0.5) == 90

    def test_hist_entropy(self):
        im = pyvips.Image.new_from_file(JPEG_FILE).extract_band(1)

        ent = im.hist_find().hist_entropy()

        assert pytest.approx(ent, 0.01) == 6.67

    def test_stdif(self):
        im = pyvips.Image.new_from_file(JPEG_FILE)

        im2 = im.stdif(10, 10)

        assert im.width == im2.width
        assert im.height == im2.height

        # new mean should be closer to target mean
        assert abs(im.avg() - 128) > abs(im2.avg() - 128)

    def test_case(self):
        # slice into two at 128, we should get 50% of pixels in each half
        x = pyvips.Image.grey(256, 256, uchar=True)
        index = pyvips.Image.switch([x < 128, x >= 128])

        y = index.case([10, 20])
        assert y.avg() == 15

        # slice into four
        index = pyvips.Image.switch([
            x < 64,
            x >= 64 and x < 128,
            x >= 128 and x < 192,
            x >= 192
        ])
        assert index.case([10, 20, 30, 40]).avg() == 25

        # values over N should use the last value
        assert index.case([10, 20, 30]).avg() == 22.5

if __name__ == '__main__':
    pytest.main()