Skip to content

Commit

Permalink
fix: correct 16 bits image thresholding
Browse files Browse the repository at this point in the history
Closes: #374
  • Loading branch information
opatiny committed Sep 22, 2023
1 parent 6adde86 commit 8ca381f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions src/operations/__tests__/threshold.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ImageColorModel, Image } from '../..';
import { computeThreshold, threshold } from '../threshold';

test('threshold with a fixed value of 100', () => {
Expand Down Expand Up @@ -94,3 +95,24 @@ test('error threshold out of range', () => {
/threshold must be a value between 0 and 1/,
);
});

test('16 bits image simple', () => {
const image = new Image(2, 2, {
colorModel: ImageColorModel.GREY,
bitDepth: 16,
data: new Uint16Array([0, 100, 20000, 30000]),
});
const threshold = image.threshold();

expect(threshold).toMatchImageData([
[0, 0],
[1, 1],
]);
});

test('16 bits image', () => {
const image = testUtils.load('formats/grey16.png');
const threshold = image.threshold();

expect(threshold).toMatchImageSnapshot();
});
2 changes: 1 addition & 1 deletion src/operations/threshold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function computeThreshold(
'threshold can only be computed on images with one channel',
);
}
const histogram = image.histogram();
const histogram = image.histogram({slots: 2**image.bitDepth});

switch (algorithm) {
case 'huang':
Expand Down

0 comments on commit 8ca381f

Please sign in to comment.