# I added my own function to the py\_image.c file and call it using the img object. However, the LED flashes white rapidly when I call this function in the script. What's the reason?

**URL:** <https://forums.openmv.io/t/i-added-my-own-function-to-the-py-image-c-file-and-call-it-using-the-img-object-however-the-led-flashes-white-rapidly-when-i-call-this-function-in-the-script-whats-the-reason/11661>\
**Category:** OpenMV Boards\
**Created:** [May 21, 2026, 9:23am UTC](https://forums.openmv.io/t/i-added-my-own-function-to-the-py-image-c-file-and-call-it-using-the-img-object-however-the-led-flashes-white-rapidly-when-i-call-this-function-in-the-script-whats-the-reason/11661 "2026-05-21T09:23:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![liangdeyi](https://avatars.discourse-cdn.com/v4/letter/l/13edae/32.png) [@liangdeyi](https://forums.openmv.io/u/liangdeyi)\
**Post date:** [May 21, 2026, 9:23am UTC](https://forums.openmv.io/t/i-added-my-own-function-to-the-py-image-c-file-and-call-it-using-the-img-object-however-the-led-flashes-white-rapidly-when-i-call-this-function-in-the-script-whats-the-reason/11661/1 "2026-05-21T09:23:31Z")

</div>

```auto
#define EDGE_COUNT_THRESHOLD 15 // 跳变个数阈值

static mp_obj_t py_image_in_place_detection(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {

int result = 0;

image_t \*arg_img = py_image_cobj(args\[0\]);

printf("1111111");

mp_obj_list_t \*roi_list = MP_OBJ_TO_PTR(args\[1\]);

int x = mp_obj_get_int(roi_list->items\[0\]);

int y = mp_obj_get_int(roi_list->items\[1\]);

int w = mp_obj_get_int(roi_list->items\[2\]);

int h = mp_obj_get_int(roi_list->items\[3\]);

if (w <= 0 || h <= 0 || x < 0 || y < 0) {

    printf("result: 0 (invalid roi)\\n");

    return mp_obj_new_int(0);

}

int img_width = arg_img->w;

uint8_t \*base = (uint8_t \*)arg_img->pixels;

    int edge_count = 0;

    for (int yy = 0; yy < h; yy++) {

        uint8_t \*row_ptr = base + (y + yy) \* img_width;

        int last_pix = -1;

        for (int xx = 0; xx < w; xx++) {

            int pix = imlib_get_pixel_fast(arg_img, row_ptr, x + xx);

            if (last_pix != -1 && pix != last_pix) {

                edge_count++;

            }

            last_pix = pix;

        }

    }

    // 纵向跳变

    for (int xx = 0; xx < w; xx++) {

        int last_pix = -1;

        int px = x + xx;

        for (int yy = 0; yy < h; yy++) {

            uint8_t \*row_ptr = base + (y + yy) \* img_width;

            int pix = imlib_get_pixel_fast(arg_img, row_ptr, px);

            if (last_pix != -1 && pix != last_pix) {

                edge_count++;

            }

            last_pix = pix;

        }

    }

    result = (edge_count > EDGE_COUNT_THRESHOLD) ? 1 : 0;

    return mp_obj_new_int(result);

}

static MP_DEFINE_CONST_FUN_OBJ_KW(py_image_in_place_detection_obj, 1, py_image_in_place_detection);

{MP_ROM_QSTR(MP_QSTR_in_place_detection), MP_ROM_PTR(&py_image_in_place_detection_obj)},

QDEF1(MP_QSTR_in_place_detection, 28662, 18, “in_place_detection”)

```

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [May 21, 2026, 7:19pm UTC](https://forums.openmv.io/t/i-added-my-own-function-to-the-py-image-c-file-and-call-it-using-the-img-object-however-the-led-flashes-white-rapidly-when-i-call-this-function-in-the-script-whats-the-reason/11661/2 "2026-05-21T19:19:18Z")

</div>

That means you corrupted memory or crashed the program, you kind of need to debug your own C code. We don’t provide help support for that.

See the tutorial on this for how to use a debugger: [10. Building, flashing, and debugging the firmware - OpenMV MicroPython 1.28 documentation](https://docs.openmv.io/openmvcam/tutorial/firmware_development.html)
