@@ -16,8 +16,8 @@ uint8_t mat_depth_to_ndarray_type(int depth)
16
16
case CV_16U: return NDARRAY_UINT16;
17
17
case CV_16S: return NDARRAY_INT16;
18
18
case CV_32F: return NDARRAY_FLOAT;
19
- // case CV_Bool: return NDARRAY_BOOL;
20
- default : mp_raise_ValueError (MP_ERROR_TEXT (" Unsupported Mat depth" ));
19
+ case CV_Bool: return NDARRAY_BOOL;
20
+ default : mp_raise_TypeError (MP_ERROR_TEXT (" Unsupported Mat depth" ));
21
21
}
22
22
}
23
23
@@ -29,8 +29,8 @@ int ndarray_type_to_mat_depth(uint8_t type)
29
29
case NDARRAY_UINT16: return CV_16U;
30
30
case NDARRAY_INT16: return CV_16S;
31
31
case NDARRAY_FLOAT: return CV_32F;
32
- // case NDARRAY_BOOL: return CV_Bool;
33
- default : mp_raise_ValueError (MP_ERROR_TEXT (" Unsupported ndarray type" ));
32
+ case NDARRAY_BOOL: return CV_Bool;
33
+ default : mp_raise_TypeError (MP_ERROR_TEXT (" Unsupported ndarray type" ));
34
34
}
35
35
}
36
36
@@ -147,3 +147,59 @@ Mat mp_obj_to_mat(mp_obj_t obj)
147
147
148
148
return mat;
149
149
}
150
+
151
+ Size mp_obj_to_size (mp_obj_t obj)
152
+ {
153
+ // Check for None object
154
+ if (obj == mp_const_none)
155
+ {
156
+ // Create an empty Size object
157
+ return Size ();
158
+ }
159
+
160
+ // Assume the object is a ndarray, or can be converted to one. Will raise an
161
+ // exception if not
162
+ ndarray_obj_t *ndarray = ndarray_from_mp_obj (obj, 0 );
163
+
164
+ // Validate the length of the ndarray
165
+ if (ndarray->len != 2 )
166
+ {
167
+ mp_raise_TypeError (MP_ERROR_TEXT (" Size must be length 2" ));
168
+ }
169
+
170
+ // Check the type of the ndarray
171
+ if (ndarray->dtype == NDARRAY_UINT8)
172
+ {
173
+ uint8_t *data = (uint8_t *)ndarray->array ;
174
+ return Size (data[0 ], data[1 ]);
175
+ }
176
+ else if (ndarray->dtype == NDARRAY_INT8)
177
+ {
178
+ int8_t *data = (int8_t *)ndarray->array ;
179
+ return Size (data[0 ], data[1 ]);
180
+ }
181
+ else if (ndarray->dtype == NDARRAY_UINT16)
182
+ {
183
+ uint16_t *data = (uint16_t *)ndarray->array ;
184
+ return Size (data[0 ], data[1 ]);
185
+ }
186
+ else if (ndarray->dtype == NDARRAY_INT16)
187
+ {
188
+ int16_t *data = (int16_t *)ndarray->array ;
189
+ return Size (data[0 ], data[1 ]);
190
+ }
191
+ else if (ndarray->dtype == NDARRAY_FLOAT)
192
+ {
193
+ float *data = (float *)ndarray->array ;
194
+ return Size (data[0 ], data[1 ]);
195
+ }
196
+ else if (ndarray->dtype == NDARRAY_BOOL)
197
+ {
198
+ bool *data = (bool *)ndarray->array ;
199
+ return Size (data[0 ], data[1 ]);
200
+ }
201
+ else
202
+ {
203
+ mp_raise_TypeError (MP_ERROR_TEXT (" Unsupported ndarray type" ));
204
+ }
205
+ }
0 commit comments