Skip to content

Commit 073c889

Browse files
committed
test: modify and add tests for coverage
1 parent 6de5168 commit 073c889

File tree

4 files changed

+83
-18
lines changed

4 files changed

+83
-18
lines changed

src/geometry/__tests__/getPerspectiveWarp.test.ts

Lines changed: 81 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,57 @@
11
import { Image } from '../../Image.js';
2-
import getPerspectiveWarp from '../getPerspectiveWarp.js';
2+
import getPerspectiveWarp, { order4Points } from '../getPerspectiveWarp.js';
3+
4+
describe('4 points sorting', () => {
5+
test('basic sorting test', () => {
6+
const points = [
7+
{ column: 0, row: 100 },
8+
{ column: 0, row: 0 },
9+
{ column: 100, row: 1 },
10+
{ column: 100, row: 100 },
11+
];
12+
13+
const result = order4Points(points);
14+
expect(result).toEqual([
15+
{ column: 0, row: 0 },
16+
{ column: 100, row: 1 },
17+
{ column: 100, row: 100 },
18+
{ column: 0, row: 100 },
19+
]);
20+
});
21+
test('inclined square', () => {
22+
const points = [
23+
{ column: 45, row: 0 },
24+
{ column: 0, row: 45 },
25+
{ column: 45, row: 90 },
26+
{ column: 90, row: 45 },
27+
];
28+
29+
const result = order4Points(points);
30+
expect(result).toEqual([
31+
{ column: 0, row: 45 },
32+
{ column: 90, row: 45 },
33+
{ column: 45, row: 0 },
34+
{ column: 45, row: 90 },
35+
]);
36+
});
37+
test('basic sorting test', () => {
38+
const points = [
39+
{ column: 155, row: 195 },
40+
{ column: 154, row: 611 },
41+
{ column: 858.5, row: 700 },
42+
{ column: 911.5, row: 786 },
43+
];
44+
45+
const result = order4Points(points);
46+
expect(result).toEqual([
47+
{ column: 155, row: 195 },
48+
49+
{ column: 858.5, row: 700 },
50+
{ column: 911.5, row: 786 },
51+
{ column: 154, row: 611 },
52+
]);
53+
});
54+
});
355

456
describe('warping tests', () => {
557
it('resize without rotation', () => {
@@ -14,7 +66,7 @@ describe('warping tests', () => {
1466
{ column: 0, row: 2 },
1567
];
1668
const matrix = getPerspectiveWarp(points);
17-
const result = image.transform(matrix, { inverse: true });
69+
const result = image.transform(matrix.matrix, { inverse: true });
1870
expect(result.width).not.toBeLessThan(2);
1971
expect(result.height).not.toBeLessThan(2);
2072
expect(result.width).not.toBeGreaterThan(3);
@@ -35,7 +87,7 @@ describe('warping tests', () => {
3587
{ column: 0, row: 1 },
3688
];
3789
const matrix = getPerspectiveWarp(points);
38-
const result = image.transform(matrix, { inverse: true });
90+
const result = image.transform(matrix.matrix, { inverse: true });
3991
expect(result.width).not.toBeLessThan(3);
4092
expect(result.height).not.toBeLessThan(1);
4193
expect(result.width).not.toBeGreaterThan(4);
@@ -60,7 +112,7 @@ describe('openCV comparison', () => {
60112
width: 1080,
61113
height: 810,
62114
});
63-
const result = image.transform(matrix, {
115+
const result = image.transform(matrix.matrix, {
64116
inverse: true,
65117
interpolationType: 'nearest',
66118
});
@@ -87,16 +139,16 @@ describe('openCV comparison', () => {
87139
'opencv/test_perspective_warp_card_nearest.png',
88140
);
89141
const points = [
90-
{ column: 145, row: 460 },
91142
{ column: 55, row: 140 },
92143
{ column: 680, row: 38 },
93144
{ column: 840, row: 340 },
145+
{ column: 145, row: 460 },
94146
];
95147
const matrix = getPerspectiveWarp(points, {
96148
width: 700,
97149
height: 400,
98150
});
99-
const result = image.transform(matrix, {
151+
const result = image.transform(matrix.matrix, {
100152
inverse: true,
101153
interpolationType: 'nearest',
102154
width: 700,
@@ -118,29 +170,40 @@ describe('openCV comparison', () => {
118170
expect(result.height).toEqual(openCvResult.height);
119171
expect(croppedPiece).toEqual(croppedPieceOpenCv);
120172
});
121-
test('nearest interpolation plants', () => {
122-
const image = testUtils.load('various/plants.png');
173+
test('nearest interpolation poker card', () => {
174+
const image = testUtils.load('various/poker_cards.png');
123175
const openCvResult = testUtils.load(
124-
'opencv/test_perspective_warp_plants_linear.png',
176+
'opencv/test_perspective_warp_poker_cards_nearest.png',
125177
);
126178

127179
const points = [
128-
{ column: 858.5, row: 9 },
129-
{ column: 166.5, row: 195 },
130-
{ column: 154.5, row: 611 },
131-
{ column: 911.5, row: 786 },
180+
{ column: 1100, row: 660 },
181+
{ column: 680, row: 660 },
182+
{ column: 660, row: 290 },
183+
{ column: 970, row: 290 },
132184
];
133-
const matrix = getPerspectiveWarp(points, {
134-
width: 1080,
135-
height: 810,
136-
});
137-
const result = image.transform(matrix, {
185+
const matrix = getPerspectiveWarp(points);
186+
const result = image.transform(matrix.matrix, {
138187
inverse: true,
139188
interpolationType: 'nearest',
189+
height: matrix.height,
190+
width: matrix.width,
191+
});
192+
193+
const cropped = result.crop({
194+
origin: { column: 10, row: 10 },
195+
width: 100,
196+
height: 100,
197+
});
198+
const croppedCV = openCvResult.crop({
199+
origin: { column: 10, row: 10 },
200+
width: 100,
201+
height: 100,
140202
});
141203

142204
expect(result.width).toEqual(openCvResult.width);
143205
expect(result.height).toEqual(openCvResult.height);
206+
expect(cropped).toEqual(croppedCV);
144207
});
145208
});
146209

test/TestImagePath.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export type TestImagePath =
7777
| 'opencv/test_perspective_warp_card_nearest.png'
7878
| 'opencv/test_perspective_warp_plants_linear.png'
7979
| 'opencv/test_perspective_warp_plants_nearest.png'
80+
| 'opencv/test_perspective_warp_poker_cards_nearest.png'
8081
| 'opencv/testReflect.png'
8182
| 'opencv/test_resize_bicubic_larger.png'
8283
| 'opencv/test_resize_bicubic_same.png'
@@ -100,6 +101,7 @@ export type TestImagePath =
100101
| 'various/card.png'
101102
| 'various/grayscale_by_zimmyrose.png'
102103
| 'various/plants.png'
104+
| 'various/poker_cards.png'
103105
| 'various/screws.png'
104106
| 'various/sudoku.jpg'
105107
| 'various/without-metadata.jpg';
Loading

test/img/various/poker_cards.png

737 KB
Loading

0 commit comments

Comments
 (0)