From b5e390144010ba074aacff388f920938d95f2d2d Mon Sep 17 00:00:00 2001 From: junhuanchen Date: Fri, 2 Dec 2022 18:02:55 +0800 Subject: [PATCH] [0.5.3] support image.binary from openmv. --- .github/workflows/maixpy3_build.yml | 2 +- .github/workflows/maixpy3_pypi.yml | 2 +- ext_modules/_maix_image/_maix_image.cpp | 59 +++++++++++++++++++- ext_modules/_maix_image/include/maix_image.h | 2 + ext_modules/_maix_image/py_maix_image.cpp | 1 + maix/__init__.py | 2 +- 6 files changed, 64 insertions(+), 4 deletions(-) diff --git a/.github/workflows/maixpy3_build.yml b/.github/workflows/maixpy3_build.yml index 92a2ea1..e667bc8 100644 --- a/.github/workflows/maixpy3_build.yml +++ b/.github/workflows/maixpy3_build.yml @@ -15,7 +15,7 @@ jobs: git submodule update --init --recursive - uses: actions/setup-python@v2 with: - python-version: 3.8.5 + python-version: 3.8 - name: Install dependencies run: | sudo apt update diff --git a/.github/workflows/maixpy3_pypi.yml b/.github/workflows/maixpy3_pypi.yml index 1ccc9bc..4498507 100644 --- a/.github/workflows/maixpy3_pypi.yml +++ b/.github/workflows/maixpy3_pypi.yml @@ -17,7 +17,7 @@ jobs: git submodule update --init --recursive - uses: actions/setup-python@v2 with: - python-version: 3.8.5 + python-version: 3.8 - name: Install dependencies run: | sudo apt update diff --git a/ext_modules/_maix_image/_maix_image.cpp b/ext_modules/_maix_image/_maix_image.cpp index 2bcb358..efcf278 100644 --- a/ext_modules/_maix_image/_maix_image.cpp +++ b/ext_modules/_maix_image/_maix_image.cpp @@ -516,7 +516,7 @@ maix_image &maix_image::_resize(int dst_w, int dst_h, int func, int padding, std new_w = dst_w, new_h = new_w * src_h / src_w; // new_h / src_h = new_w / src_w => new_h = new_w * src_h / src_w top = (dst_h - new_h) / 2, bottom = dst_h - new_h - top; } - else + else { // Division loses precision new_h = dst_h, new_w = new_h * src_w / src_h; left = (dst_w - new_w) / 2, right = dst_w - new_w - left; @@ -923,6 +923,63 @@ maix_image &maix_image::_mean(const int ksize, bool threshold, int offset, bool return *this; } +maix_image &maix_image::_binary(std::vector> &thresholds_src, bool invert, bool zero, maix_image &mask) +{ + + if (NULL == this->_img) + { + py::print("[image] is empty !"); + return *this; + } + + image_t img_tmp = {}, *arg_img = &img_tmp; + arg_img->w = this->_img->width; + arg_img->h = this->_img->height; + arg_img->pixels = (uint8_t *)this->_img->data; + arg_img->pixfmt = PIXFORMAT_RGB888; + + list_t thresholds; + list_init(&thresholds, sizeof(color_thresholds_list_lnk_data_t)); + for (auto src : thresholds_src) + { + color_thresholds_list_lnk_data_t tmp_ct; + tmp_ct.LMin = src[0]; + tmp_ct.LMax = src[1]; + tmp_ct.AMin = src[2]; + tmp_ct.AMax = src[3]; + tmp_ct.BMin = src[4]; + tmp_ct.BMax = src[5]; + list_push_back(&thresholds, &tmp_ct); + } + + image_t *mask_img = NULL; + if (NULL != mask._img) // check if mask is NULL + { + if (this->_img->width == mask._img->width && this->_img->height == mask._img->height) // check if mask has same size with image + { + mask_img = (image_t *)malloc(sizeof(image_t)); + if (mask_img) + { + mask_img->w = mask._img->width; + mask_img->h = mask._img->height; + mask_img->pixels = (uint8_t *)mask._img->data; + mask_img->pixfmt = PIXFORMAT_RGB888; + } + } + else + printf("The size of mask is different with input image,use default mask!"); + } + + fb_alloc_mark(); + imlib_binary(arg_img, arg_img, &thresholds, invert, zero, mask_img); + fb_alloc_free_till_mark(); + + if (mask_img != NULL) + free(mask_img), mask_img = NULL; + + return *this; +} + py::list maix_image::_imlib_get_statistics(std::vector roi_src, std::vector> &thresholds_src, bool invert, maix_image &other_src, int bins, int l_bins, int a_bins, int b_bins) { diff --git a/ext_modules/_maix_image/include/maix_image.h b/ext_modules/_maix_image/include/maix_image.h index 6a5d9a9..d536c7e 100644 --- a/ext_modules/_maix_image/include/maix_image.h +++ b/ext_modules/_maix_image/include/maix_image.h @@ -216,6 +216,8 @@ class maix_image : virtual public any_image, public maix_vision, public maix_cus maix_image &_mean(const int ksize, bool threshold, int offset, bool invert, maix_image &mask); + maix_image &_binary(std::vector> &thresholds_src, bool invert, bool zero, maix_image &mask); + maix_image &_opencv_Canny(double threshold1, double threshold2, int apertureSize, bool L2gradient); }; #endif \ No newline at end of file diff --git a/ext_modules/_maix_image/py_maix_image.cpp b/ext_modules/_maix_image/py_maix_image.cpp index 81578df..8350568 100644 --- a/ext_modules/_maix_image/py_maix_image.cpp +++ b/ext_modules/_maix_image/py_maix_image.cpp @@ -144,6 +144,7 @@ PYBIND11_MODULE(_maix_image, mo) // .def("gamma_corr",&maix_image::_gamma_corr,py::arg("gamma")=1.0,py::arg("contrast")=1.0,py::arg("brightness")=0.0) .def("lens_corr", &maix_image::_lens_corr, py::arg("strength") = 1.8, py::arg("zoom") = 1.0, py::arg("x_corr") = 0.0, py::arg("y_corr") = 0.0) .def("mean", &maix_image::_mean, py::arg("ksize") = 1, py::arg("threshold") = 0, py::arg("offset") = 0, py::arg("invert") = 0, py::arg("mask") = maix_image()) + .def("binary", &maix_image::_binary, py::arg("thresholds"), py::arg("invert") = 0, py::arg("zero") = 0, py::arg("mask") = maix_image()) .def("find_rects", &maix_image::_imlib_find_rects, py::arg("roi") = std::vector{0, 0, 0, 0}, py::arg("threshold"), py::arg("is_xywh") = 0) .def("find_lines", &maix_image::_imlib_find_lines, py::arg("roi") = std::vector{0, 0, 0, 0}, py::arg("x_stride") = 2, py::arg("y_stride") = 1, py::arg("threshold") = 1000, py::arg("theta_margin") = 25, py::arg("rho_margin") = 25) .def("find_circles", &maix_image::_imlib_find_circles, py::arg("roi") = std::vector{0, 0, 0, 0}, py::arg("x_stride") = 2, py::arg("y_stride") = 1, py::arg("threshold") = 2000, py::arg("x_margin") = 10, py::arg("y_margin") = 10, py::arg("r_margin") = 10, py::arg("r_min") = 2, py::arg("r_max") = 0, py::arg("r_step") = 2) diff --git a/maix/__init__.py b/maix/__init__.py index 9d7d2ef..bfb5651 100644 --- a/maix/__init__.py +++ b/maix/__init__.py @@ -1,5 +1,5 @@ -version='0.5.2' +version='0.5.3' __all__ = ['display', 'camera', 'image']