@@ -13,7 +13,8 @@ describe('warping tests', () => {
13
13
{ column : 1 , row : 2 } ,
14
14
{ column : 0 , row : 2 } ,
15
15
] ;
16
- const result = getPerspectiveWarp ( image , points ) ;
16
+ const matrix = getPerspectiveWarp ( points ) ;
17
+ const result = image . transform ( matrix , { inverse : true } ) ;
17
18
expect ( result . width ) . not . toBeLessThan ( 2 ) ;
18
19
expect ( result . height ) . not . toBeLessThan ( 2 ) ;
19
20
expect ( result . width ) . not . toBeGreaterThan ( 3 ) ;
@@ -33,18 +34,21 @@ describe('warping tests', () => {
33
34
{ column : 2 , row : 1 } ,
34
35
{ column : 0 , row : 1 } ,
35
36
] ;
36
- const result = getPerspectiveWarp ( image , points ) ;
37
+ const matrix = getPerspectiveWarp ( points ) ;
38
+ const result = image . transform ( matrix , { inverse : true } ) ;
37
39
expect ( result . width ) . not . toBeLessThan ( 3 ) ;
38
40
expect ( result . height ) . not . toBeLessThan ( 1 ) ;
39
41
expect ( result . width ) . not . toBeGreaterThan ( 4 ) ;
40
- expect ( result . height ) . not . toBeGreaterThan ( 2 ) ;
42
+ expect ( result . height ) . not . toBeGreaterThan ( 4 ) ;
41
43
} ) ;
44
+ } ) ;
42
45
43
- test ( 'openCV comparison' , ( ) => {
46
+ describe ( 'openCV comparison' , ( ) => {
47
+ test ( 'nearest interpolation plants' , ( ) => {
44
48
const image = testUtils . load ( 'various/plants.png' ) ;
45
49
46
50
const openCvResult = testUtils . load (
47
- 'opencv/test_perspective_warp_plants .png' ,
51
+ 'opencv/test_perspective_warp_plants_nearest .png' ,
48
52
) ;
49
53
50
54
const points = [
@@ -53,21 +57,100 @@ describe('warping tests', () => {
53
57
{ column : 911.5 , row : 786 } ,
54
58
{ column : 154.5 , row : 611 } ,
55
59
] ;
56
- const result = getPerspectiveWarp ( image , points , {
60
+ const matrix = getPerspectiveWarp ( points , {
57
61
width : 1080 ,
58
62
height : 810 ,
59
63
} ) ;
64
+ const result = image . transform ( matrix , {
65
+ inverse : true ,
66
+ interpolationType : 'nearest' ,
67
+ } ) ;
60
68
const croppedPieceOpenCv = openCvResult . crop ( {
61
69
origin : { column : 45 , row : 0 } ,
62
- width : 400 ,
63
- height : 400 ,
70
+ width : 100 ,
71
+ height : 100 ,
64
72
} ) ;
65
73
66
74
const croppedPiece = result . crop ( {
67
75
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 ,
69
104
height : 400 ,
70
105
} ) ;
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
+ } ) ;
71
154
72
155
expect ( result . width ) . toEqual ( openCvResult . width ) ;
73
156
expect ( result . height ) . toEqual ( openCvResult . height ) ;
0 commit comments