Skip to content

Commit d280f46

Browse files
authored
fix: scale threshold when slots number is changed (#450)
* fix: scale threshold when slots number is changed * fix: fix threshold test with custom slots * refactor: use slots instead of options.slots
1 parent f597aea commit d280f46

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

src/operations/__tests__/threshold.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ test('16 bits image simple with default number of slots 2**16', () => {
124124
data: new Uint16Array([0, 100, 20000, 30000]),
125125
});
126126
const threshold = image.threshold({ slots: 2 ** image.bitDepth });
127-
const defaultThreshold = image.threshold();
127+
const defaultSlotsThreshold = image.threshold();
128128

129-
expect(threshold).toEqual(defaultThreshold);
129+
expect(threshold).toEqual(defaultSlotsThreshold);
130130
});
131131

132132
test('16 bits image simple with custom number of slots 2**8', () => {
@@ -136,7 +136,7 @@ test('16 bits image simple with custom number of slots 2**8', () => {
136136
data: new Uint16Array([0, 100, 20000, 30000]),
137137
});
138138
const threshold = image.threshold({ slots: 2 ** 8 });
139-
const defaultThreshold = image.threshold();
139+
const defaultSlotsThreshold = image.threshold();
140140

141-
expect(threshold).not.toEqual(defaultThreshold);
141+
expect(threshold).toEqual(defaultSlotsThreshold);
142142
});

src/operations/threshold.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,38 +87,39 @@ export function computeThreshold(
8787
);
8888
}
8989
const histogram = image.histogram({ slots });
90+
const scale = slots ? 2 ** image.bitDepth / slots : 1;
9091

9192
switch (algorithm) {
9293
case 'huang':
93-
return huang(histogram);
94+
return huang(histogram) * scale;
9495
case 'intermodes':
95-
return intermodes(histogram);
96+
return intermodes(histogram) * scale;
9697
case 'isodata':
97-
return isodata(histogram);
98+
return isodata(histogram) * scale;
9899
case 'li':
99-
return li(histogram, image.size);
100+
return li(histogram, image.size) * scale;
100101
case 'maxEntropy':
101-
return maxEntropy(histogram, image.size);
102+
return maxEntropy(histogram, image.size) * scale;
102103
case 'mean':
103-
return mean(histogram, image.size);
104+
return mean(histogram, image.size) * scale;
104105
case 'minimum':
105-
return minimum(histogram);
106+
return minimum(histogram) * scale;
106107
case 'minError':
107-
return minError(histogram, image.size);
108+
return minError(histogram, image.size) * scale;
108109
case 'moments':
109-
return moments(histogram, image.size);
110+
return moments(histogram, image.size) * scale;
110111
case 'otsu':
111-
return otsu(histogram, image.size);
112+
return otsu(histogram, image.size) * scale;
112113
case 'percentile':
113-
return percentile(histogram);
114+
return percentile(histogram) * scale;
114115
case 'renyiEntropy':
115-
return renyiEntropy(histogram, image.size);
116+
return renyiEntropy(histogram, image.size) * scale;
116117
case 'shanbhag':
117-
return shanbhag(histogram, image.size);
118+
return shanbhag(histogram, image.size) * scale;
118119
case 'triangle':
119-
return triangle(histogram);
120+
return triangle(histogram) * scale;
120121
case 'yen':
121-
return yen(histogram, image.size);
122+
return yen(histogram, image.size) * scale;
122123
default:
123124
throw new RangeError(`invalid threshold algorithm: ${algorithm}`);
124125
}

0 commit comments

Comments
 (0)