1
1
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
+ } ) ;
3
55
4
56
describe ( 'warping tests' , ( ) => {
5
57
it ( 'resize without rotation' , ( ) => {
@@ -14,7 +66,7 @@ describe('warping tests', () => {
14
66
{ column : 0 , row : 2 } ,
15
67
] ;
16
68
const matrix = getPerspectiveWarp ( points ) ;
17
- const result = image . transform ( matrix , { inverse : true } ) ;
69
+ const result = image . transform ( matrix . matrix , { inverse : true } ) ;
18
70
expect ( result . width ) . not . toBeLessThan ( 2 ) ;
19
71
expect ( result . height ) . not . toBeLessThan ( 2 ) ;
20
72
expect ( result . width ) . not . toBeGreaterThan ( 3 ) ;
@@ -35,7 +87,7 @@ describe('warping tests', () => {
35
87
{ column : 0 , row : 1 } ,
36
88
] ;
37
89
const matrix = getPerspectiveWarp ( points ) ;
38
- const result = image . transform ( matrix , { inverse : true } ) ;
90
+ const result = image . transform ( matrix . matrix , { inverse : true } ) ;
39
91
expect ( result . width ) . not . toBeLessThan ( 3 ) ;
40
92
expect ( result . height ) . not . toBeLessThan ( 1 ) ;
41
93
expect ( result . width ) . not . toBeGreaterThan ( 4 ) ;
@@ -60,7 +112,7 @@ describe('openCV comparison', () => {
60
112
width : 1080 ,
61
113
height : 810 ,
62
114
} ) ;
63
- const result = image . transform ( matrix , {
115
+ const result = image . transform ( matrix . matrix , {
64
116
inverse : true ,
65
117
interpolationType : 'nearest' ,
66
118
} ) ;
@@ -87,16 +139,16 @@ describe('openCV comparison', () => {
87
139
'opencv/test_perspective_warp_card_nearest.png' ,
88
140
) ;
89
141
const points = [
90
- { column : 145 , row : 460 } ,
91
142
{ column : 55 , row : 140 } ,
92
143
{ column : 680 , row : 38 } ,
93
144
{ column : 840 , row : 340 } ,
145
+ { column : 145 , row : 460 } ,
94
146
] ;
95
147
const matrix = getPerspectiveWarp ( points , {
96
148
width : 700 ,
97
149
height : 400 ,
98
150
} ) ;
99
- const result = image . transform ( matrix , {
151
+ const result = image . transform ( matrix . matrix , {
100
152
inverse : true ,
101
153
interpolationType : 'nearest' ,
102
154
width : 700 ,
@@ -118,29 +170,40 @@ describe('openCV comparison', () => {
118
170
expect ( result . height ) . toEqual ( openCvResult . height ) ;
119
171
expect ( croppedPiece ) . toEqual ( croppedPieceOpenCv ) ;
120
172
} ) ;
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' ) ;
123
175
const openCvResult = testUtils . load (
124
- 'opencv/test_perspective_warp_plants_linear .png' ,
176
+ 'opencv/test_perspective_warp_poker_cards_nearest .png' ,
125
177
) ;
126
178
127
179
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 } ,
132
184
] ;
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 , {
138
187
inverse : true ,
139
188
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 ,
140
202
} ) ;
141
203
142
204
expect ( result . width ) . toEqual ( openCvResult . width ) ;
143
205
expect ( result . height ) . toEqual ( openCvResult . height ) ;
206
+ expect ( cropped ) . toEqual ( croppedCV ) ;
144
207
} ) ;
145
208
} ) ;
146
209
0 commit comments