| 520 | // ---------------------------------------------------------------------------------------- |
| 521 | |
| 522 | py::array py_sub_image ( |
| 523 | const py::array& img, |
| 524 | const rectangle& win |
| 525 | ) |
| 526 | { |
| 527 | DLIB_CASSERT(img.ndim() >= 2); |
| 528 | |
| 529 | auto width_step = img.strides(0); |
| 530 | |
| 531 | const long nr = img.shape(0); |
| 532 | const long nc = img.shape(1); |
| 533 | rectangle rect(0,0,nc-1,nr-1); |
| 534 | rect = rect.intersect(win); |
| 535 | |
| 536 | std::vector<size_t> shape(img.ndim()), strides(img.ndim()); |
| 537 | for (size_t i = 0; i < shape.size(); ++i) |
| 538 | { |
| 539 | shape[i] = img.shape(i); |
| 540 | strides[i] = img.strides(i); |
| 541 | } |
| 542 | |
| 543 | shape[0] = rect.height(); |
| 544 | shape[1] = rect.width(); |
| 545 | |
| 546 | size_t col_stride = 1; |
| 547 | for (size_t i = 1; i < strides.size(); ++i) |
| 548 | col_stride *= strides[i]; |
| 549 | |
| 550 | const void* data = (char*)img.data() + col_stride*rect.left() + rect.top()*strides[0]; |
| 551 | |
| 552 | return py::array(img.dtype(), shape, strides, data, img); |
| 553 | } |
| 554 | |
| 555 | py::array py_sub_image2 ( |
| 556 | const py::tuple& image_and_rect_tuple |