@@ -81,23 +81,24 @@ describe('core', () => {
81
81
82
82
describe ( 'kmeans' , ( ) => {
83
83
funcShouldRequireArgs ( ( ) => cv . kmeans ( ) ) ;
84
- const points = [
84
+ const points2 = [
85
85
[ 0 , 0 ] , [ 1000 , 900 ] , [ - 1000 , - 900 ] , [ - 1100 , - 1000 ] , [ 1100 , 1000 ] , [ 10 , 10 ]
86
86
] . map ( ( [ x , y ] ) => new cv . Point ( x , y ) ) ;
87
+
87
88
const k = 3 ;
88
89
const termCriteria = new cv . TermCriteria ( cv . termCriteria . COUNT , 100 , 0.8 ) ;
89
90
const attempts = 10 ;
90
91
const flags = cv . KMEANS_RANDOM_CENTERS ;
91
92
92
93
it ( 'should return labels and centers' , ( ) => {
93
- const ret = cv . kmeans ( points , k , termCriteria , attempts , flags ) ;
94
+ const ret = cv . kmeans ( points2 , k , termCriteria , attempts , flags ) ;
94
95
95
96
expect ( ret ) . to . have . property ( 'labels' ) . to . be . an ( 'array' ) . lengthOf ( 6 ) ;
96
97
expect ( ret ) . to . have . property ( 'centers' ) . to . be . an ( 'array' ) . lengthOf ( k ) ;
97
98
} ) ;
98
99
99
100
it ( 'should return correct labels' , ( ) => {
100
- const ret = cv . kmeans ( points , k , termCriteria , attempts , flags ) ;
101
+ const ret = cv . kmeans ( points2 , k , termCriteria , attempts , flags ) ;
101
102
102
103
const l0 = ret . labels [ 0 ] ;
103
104
const l1 = ret . labels [ 1 ] ;
@@ -106,7 +107,7 @@ describe('core', () => {
106
107
} ) ;
107
108
108
109
it ( 'should return correct centers' , ( ) => {
109
- const ret = cv . kmeans ( points , k , termCriteria , attempts , flags ) ;
110
+ const ret = cv . kmeans ( points2 , k , termCriteria , attempts , flags ) ;
110
111
111
112
const l0 = ret . labels [ 0 ] ;
112
113
const l1 = ret . labels [ 1 ] ;
@@ -118,8 +119,30 @@ describe('core', () => {
118
119
expect ( ret . centers [ l2 ] . x ) . to . equal ( - 1050 ) ;
119
120
expect ( ret . centers [ l2 ] . y ) . to . equal ( - 950 ) ;
120
121
} ) ;
121
- } ) ;
122
+
123
+ // related to https://github.com/justadudewhohacks/opencv4nodejs/issues/379
124
+ const points3 = [
125
+ [ 255 , 0 , 0 ] , [ 255 , 0 , 0 ] , [ 255 , 0 , 255 ] , [ 255 , 0 , 255 ] , [ 255 , 255 , 255 ]
126
+ ] . map ( ( [ r , g , b ] ) => new cv . Vec3 ( r , g , b ) ) ;
127
+
128
+ it ( 'should return correct centers with Vec3' , ( ) => {
129
+ const ret = cv . kmeans ( points3 , k , termCriteria , attempts , flags ) ;
122
130
131
+ const l0 = ret . labels [ 0 ] ;
132
+ const l1 = ret . labels [ 1 ] ;
133
+ const l2 = ret . labels [ 2 ] ;
134
+ expect ( ret . centers [ l0 ] . x ) . to . equal ( 255 ) ;
135
+ expect ( ret . centers [ l0 ] . y ) . to . equal ( 0 ) ;
136
+ expect ( ret . centers [ l0 ] . z ) . to . equal ( 0 ) ;
137
+ expect ( ret . centers [ l1 ] . x ) . to . equal ( 255 ) ;
138
+ expect ( ret . centers [ l1 ] . y ) . to . equal ( 0 ) ;
139
+ expect ( ret . centers [ l1 ] . z ) . to . equal ( 255 ) ;
140
+ expect ( ret . centers [ l2 ] . x ) . to . equal ( 255 ) ;
141
+ expect ( ret . centers [ l2 ] . y ) . to . equal ( 255 ) ;
142
+ expect ( ret . centers [ l2 ] . y ) . to . equal ( 255 ) ;
143
+ } ) ;
144
+ } ) ;
145
+
123
146
describe ( 'cartToPolar' , ( ) => {
124
147
const x = new cv . Mat ( [ [ 0 , 1 , 100 ] ] , cv . CV_32F ) ;
125
148
const y = new cv . Mat ( [ [ 0 , 1 , 100 ] ] , cv . CV_32F ) ;
0 commit comments