Skip to content

Commit 998f8bf

Browse files
committed
Add conversion from mp_obj_t to cv:Size
Needed for some OpenCV functions Converts to ndarray_obj_t to handle all possible input types
1 parent c5e2600 commit 998f8bf

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

src/convert.cpp

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ uint8_t mat_depth_to_ndarray_type(int depth)
1616
case CV_16U: return NDARRAY_UINT16;
1717
case CV_16S: return NDARRAY_INT16;
1818
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"));
2121
}
2222
}
2323

@@ -29,8 +29,8 @@ int ndarray_type_to_mat_depth(uint8_t type)
2929
case NDARRAY_UINT16: return CV_16U;
3030
case NDARRAY_INT16: return CV_16S;
3131
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"));
3434
}
3535
}
3636

@@ -147,3 +147,59 @@ Mat mp_obj_to_mat(mp_obj_t obj)
147147

148148
return mat;
149149
}
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+
}

src/convert.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ int ndarray_type_to_mat_depth(uint8_t type);
1717
ndarray_obj_t *mat_to_ndarray(Mat &mat);
1818
Mat ndarray_to_mat(ndarray_obj_t *ndarray);
1919

20-
// Conversion functions between Mat and mp_obj_t. Abstracts away intermediate
20+
// Conversion functions between Mat and mp_obj_t. Abstracts away intermediate
2121
// conversions to ndarray_obj_t
2222
mp_obj_t mat_to_mp_obj(Mat &mat);
2323
Mat mp_obj_to_mat(mp_obj_t obj);
24+
25+
// Conversion functions between Size and mp_obj_t
26+
Size mp_obj_to_size(mp_obj_t obj);

0 commit comments

Comments
 (0)