Skip to content
This repository was archived by the owner on Jul 26, 2025. It is now read-only.

Commit 70d251c

Browse files
opatinytargos
andauthored
fix(getMask): remove useless option (#292)
*feat(getMask): rename innerBorders option to solidFill Closes: getMask options and behaviour #252 * test: add tophat test to remove background * test: fix tophat test script * fix(getMask): solidFill default value * test(getmbr): fix solidFill option * Update .gitignore Co-authored-by: Michaël Zasso <[email protected]>
1 parent 383d96b commit 70d251c

File tree

6 files changed

+67
-18
lines changed

6 files changed

+67
-18
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,4 @@ src/**/*.png
130130

131131
# ignore images in test scripts folder
132132
scripts/**.png
133+
scripts/**.jpg

demo/components/testFunctions/testGetMask.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function testGetContourMask(image: Image): Image {
1515
const roi = rois.sort((a, b) => b.surface - a.surface)[0];
1616

1717
let roiMask = roi.getMask({
18-
innerBorders: false,
18+
solidFill: true,
1919
});
2020

2121
return image.paintMask(roiMask, {

scripts/tophatTest.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { join } from 'path';
2+
3+
import {
4+
RoiMapManager,
5+
fromMask,
6+
read,
7+
write,
8+
RoiKind,
9+
readSync,
10+
writeSync,
11+
} from '../src/index';
12+
13+
const disk12 = [
14+
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
15+
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
16+
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0],
17+
[0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
18+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0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
33+
[0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
34+
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0],
35+
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
36+
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
37+
];
38+
39+
const side = 101;
40+
41+
const square = new Array(side).fill(new Array(side).fill(1));
42+
43+
const kernel = square;
44+
45+
const image = readSync('./background.jpg');
46+
47+
const grey = image.grey().invert();
48+
49+
console.log('glaargh');
50+
const topHat = grey.topHat({ kernel });
51+
console.log('tophat succeeded');
52+
53+
writeSync(join(__dirname, './background-grey.png'), grey);
54+
writeSync(join(__dirname, './background-topHat.png'), topHat);

src/maskAnalysis/__tests__/getMbr.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ test('draw mbr on large image', () => {
215215

216216
const roi = rois.sort((a, b) => b.surface - a.surface)[0];
217217

218-
const roiMask = roi.getMask({ innerBorders: false });
218+
const roiMask = roi.getMask({ solidFill: true });
219219
let mbr = roiMask.getMbr();
220220

221221
let result = rgbaImage.paintMask(roiMask, {

src/roi/__tests__/getMask.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ test('innerBorders false', () => {
7676

7777
const roi = roiMapManager.getRois({ kind: 'white' })[0];
7878
const roiMask = getMask(roi, {
79-
innerBorders: false,
79+
solidFill: true,
8080
});
8181

8282
expect(roiMask).toMatchMaskData([
@@ -96,7 +96,7 @@ test('innerBorders true', () => {
9696

9797
const roi = roiMapManager.getRois({ kind: 'white' })[0];
9898
const roiMask = getMask(roi, {
99-
innerBorders: true,
99+
solidFill: false,
100100
});
101101

102102
expect(roiMask).toMatchMaskData([
@@ -118,7 +118,7 @@ test('larger mask, innerBorders false', () => {
118118

119119
const roi = roiMapManager.getRois({ kind: 'white' })[0];
120120
const roiMask = getMask(roi, {
121-
innerBorders: false,
121+
solidFill: true,
122122
});
123123

124124
expect(roiMask).toMatchMaskData([
@@ -140,8 +140,7 @@ test('allowCorners true', () => {
140140

141141
const roi = roiMapManager.getRois({ kind: 'white' })[0];
142142
const roiMask = getMask(roi, {
143-
allowCorners: true,
144-
innerBorders: false,
143+
solidFill: true,
145144
});
146145

147146
expect(roiMask).toMatchMaskData([

src/roi/getMask.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,22 @@ import { Roi } from './Roi';
44

55
export interface GetMaskOptions {
66
/**
7-
* Whether the inner borders should be returned too.
7+
* Should the ROI holes be filled in the resulting mask?
88
*
99
* @default false
1010
*/
11-
innerBorders?: boolean;
12-
/**
13-
* Consider pixels connected by corners? This option is only useful if filled = false.
14-
*
15-
* @default false
16-
*/
17-
allowCorners?: boolean;
11+
solidFill?: boolean;
1812
}
13+
1914
/**
20-
* Generate a mask of an ROI. You can specify the kind of mask you want using the `kind` option.
15+
* Generate a mask of an ROI.
2116
*
2217
* @param roi - The ROI to generate a mask for.
2318
* @param options - Get mask options.
2419
* @returns The ROI mask.
2520
*/
2621
export function getMask(roi: Roi, options: GetMaskOptions = {}): Mask {
27-
const { innerBorders = true } = options;
22+
const { solidFill = false } = options;
2823
let mask = new Mask(roi.width, roi.height, { origin: roi.origin });
2924

3025
for (let row = 0; row < roi.height; row++) {
@@ -40,7 +35,7 @@ export function getMask(roi: Roi, options: GetMaskOptions = {}): Mask {
4035
}
4136
}
4237

43-
if (!innerBorders) {
38+
if (solidFill) {
4439
mask.solidFill({ out: mask });
4540
}
4641

0 commit comments

Comments
 (0)