Skip to content

Commit 04520ab

Browse files
committed
refactor: modify perspectiveWarp and transform function
1 parent 9f63ba7 commit 04520ab

File tree

8 files changed

+176
-215
lines changed

8 files changed

+176
-215
lines changed

src/geometry/__tests__/getPerspectiveWarp.test.ts

Lines changed: 92 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ describe('warping tests', () => {
1313
{ column: 1, row: 2 },
1414
{ column: 0, row: 2 },
1515
];
16-
const result = getPerspectiveWarp(image, points);
16+
const matrix = getPerspectiveWarp(points);
17+
const result = image.transform(matrix, { inverse: true });
1718
expect(result.width).not.toBeLessThan(2);
1819
expect(result.height).not.toBeLessThan(2);
1920
expect(result.width).not.toBeGreaterThan(3);
@@ -33,18 +34,21 @@ describe('warping tests', () => {
3334
{ column: 2, row: 1 },
3435
{ column: 0, row: 1 },
3536
];
36-
const result = getPerspectiveWarp(image, points);
37+
const matrix = getPerspectiveWarp(points);
38+
const result = image.transform(matrix, { inverse: true });
3739
expect(result.width).not.toBeLessThan(3);
3840
expect(result.height).not.toBeLessThan(1);
3941
expect(result.width).not.toBeGreaterThan(4);
40-
expect(result.height).not.toBeGreaterThan(2);
42+
expect(result.height).not.toBeGreaterThan(4);
4143
});
44+
});
4245

43-
test('openCV comparison', () => {
46+
describe('openCV comparison', () => {
47+
test('nearest interpolation plants', () => {
4448
const image = testUtils.load('various/plants.png');
4549

4650
const openCvResult = testUtils.load(
47-
'opencv/test_perspective_warp_plants.png',
51+
'opencv/test_perspective_warp_plants_nearest.png',
4852
);
4953

5054
const points = [
@@ -53,21 +57,100 @@ describe('warping tests', () => {
5357
{ column: 911.5, row: 786 },
5458
{ column: 154.5, row: 611 },
5559
];
56-
const result = getPerspectiveWarp(image, points, {
60+
const matrix = getPerspectiveWarp(points, {
5761
width: 1080,
5862
height: 810,
5963
});
64+
const result = image.transform(matrix, {
65+
inverse: true,
66+
interpolationType: 'nearest',
67+
});
6068
const croppedPieceOpenCv = openCvResult.crop({
6169
origin: { column: 45, row: 0 },
62-
width: 400,
63-
height: 400,
70+
width: 100,
71+
height: 100,
6472
});
6573

6674
const croppedPiece = result.crop({
6775
origin: { column: 45, row: 0 },
68-
width: 400,
76+
width: 100,
77+
height: 100,
78+
});
79+
80+
expect(result.width).toEqual(openCvResult.width);
81+
expect(result.height).toEqual(openCvResult.height);
82+
expect(croppedPiece).toEqual(croppedPieceOpenCv);
83+
});
84+
test('nearest interpolation card', () => {
85+
const image = testUtils.load('various/card.png');
86+
87+
const openCvResult = testUtils.load(
88+
'opencv/test_perspective_warp_card_nearest.png',
89+
);
90+
const points = [
91+
{ column: 55, row: 140 },
92+
{ column: 680, row: 38 },
93+
{ column: 840, row: 340 },
94+
{ column: 145, row: 460 },
95+
];
96+
const matrix = getPerspectiveWarp(points, {
97+
width: 700,
98+
height: 400,
99+
});
100+
const result = image.transform(matrix, {
101+
inverse: true,
102+
interpolationType: 'nearest',
103+
width: 700,
69104
height: 400,
70105
});
106+
const croppedPieceOpenCv = openCvResult.crop({
107+
origin: { column: 45, row: 0 },
108+
width: 5,
109+
height: 5,
110+
});
111+
112+
const croppedPiece = result.crop({
113+
origin: { column: 45, row: 0 },
114+
width: 5,
115+
height: 5,
116+
});
117+
118+
expect(result.width).toEqual(openCvResult.width);
119+
expect(result.height).toEqual(openCvResult.height);
120+
expect(croppedPiece).toEqual(croppedPieceOpenCv);
121+
});
122+
test('nearest interpolation plants', () => {
123+
const image = testUtils.load('various/plants.png');
124+
125+
const openCvResult = testUtils.load(
126+
'opencv/test_perspective_warp_plants_linear.png',
127+
);
128+
129+
const points = [
130+
{ column: 166.5, row: 195 },
131+
{ column: 858.5, row: 9 },
132+
{ column: 911.5, row: 786 },
133+
{ column: 154.5, row: 611 },
134+
];
135+
const matrix = getPerspectiveWarp(points, {
136+
width: 1080,
137+
height: 810,
138+
});
139+
const result = image.transform(matrix, {
140+
inverse: true,
141+
interpolationType: 'nearest',
142+
});
143+
const croppedPieceOpenCv = openCvResult.crop({
144+
origin: { column: 45, row: 0 },
145+
width: 5,
146+
height: 5,
147+
});
148+
149+
const croppedPiece = result.crop({
150+
origin: { column: 45, row: 0 },
151+
width: 5,
152+
height: 5,
153+
});
71154

72155
expect(result.width).toEqual(openCvResult.width);
73156
expect(result.height).toEqual(openCvResult.height);

0 commit comments

Comments
 (0)