Accessing extra_fb's pixels

Discussion related to "under the hood" OpenMV topics.
IAmCoder
Posts: 7
Joined: Sun Jan 05, 2020 2:43 pm

Accessing extra_fb's pixels

Postby IAmCoder » Sun Jan 05, 2020 3:00 pm

I am trying to compare two images. When I try to access the pixels from extra_fb, the device crashes / restarts. I can access the pixels from img just fine. Do I need to do anything to arg_msk so I can access the pixels?

Code: Select all

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE) 
sensor.set_framesize(sensor.QQVGA) 
extra_fb = sensor.alloc_extra_fb(sensor.width(), sensor.height(), sensor.GRAYSCALE)

while(True):
    clock.tick()
    img = sensor.snapshot()
    diff2 = img.difference2(extra_fb)
    extra_fb.replace(sensor.snapshot())
    print(clock.fps(), diff2)

Code: Select all

static mp_obj_t py_image_difference2(uint n_args, const mp_obj_t *args, mp_map_t *kw_args)
{
    image_t *arg_img = py_helper_arg_to_image_mutable(args[0]);

    image_t *arg_msk =
        py_helper_keyword_to_image_mutable_mask(n_args, args, 2, kw_args);

    fb_alloc_mark();

    int pixels = 0;

    for (int y = 0, yy = arg_img->h; y < yy; y++) {
        uint8_t *row_ptr1 = IMAGE_COMPUTE_GRAYSCALE_PIXEL_ROW_PTR(arg_img, y);
        uint8_t *row_ptr2 = IMAGE_COMPUTE_GRAYSCALE_PIXEL_ROW_PTR(arg_msk, y);
        for (int x = 0, xx = arg_img->w; x < xx; x++) {
            int pixel1 = IMAGE_GET_GRAYSCALE_PIXEL_FAST(row_ptr1, x);
            int pixel2 = IMAGE_GET_GRAYSCALE_PIXEL_FAST(row_ptr2, x);
            
            // works
            if (pixel1 >= 32) {
                pixels++;
            }
            
            // crashes when uncommented
            /*if (pixel2 >= 32) {
                pixels++;
            }*/
        };
    }

    fb_alloc_free_till_mark();

    return mp_obj_new_int(pixels);
}
User avatar
kwagyeman
Posts: 4431
Joined: Sun May 24, 2015 2:10 pm

Re: Accessing extra_fb's pixels

Postby kwagyeman » Mon Jan 06, 2020 12:44 am

Code: Select all

image_t *arg_msk =
        py_helper_keyword_to_image_mutable_mask(n_args, args, 2, kw_args);
Is probably NULL. I think the 2 should be a 1. Or, pass mask=extra_fb in your python code.
Nyamekye,
IAmCoder
Posts: 7
Joined: Sun Jan 05, 2020 2:43 pm

Re: Accessing extra_fb's pixels

Postby IAmCoder » Mon Jan 06, 2020 1:58 pm

Elite, changing it to 1 worked, thanks!

Return to “Technical Discussion”

Who is online

Users browsing this forum: jam and 11 guests