Image differencing three separate parts of camera view

Discussion related to "under the hood" OpenMV topics.
Sakuyal
Posts: 6
Joined: Wed Aug 15, 2018 7:10 pm

Image differencing three separate parts of camera view

Postby Sakuyal » Fri Aug 17, 2018 6:35 pm

Hey! It's me. Back at it again with a slightly better problem.

My cohort and I are looking into using the OpenMV's image differencing feature to determine when someone moves into or out of specified spaces. While this is easy enough to do with the whole screen (or a fraction of it
via windowing), I haven't been able to find anything in the documentation that would allow the differencing and histogram-related actions to be used on multiple different fractions of the screen at the same time. In an ideal world, we'd like to do all of our image differencing on just one of these cameras with three defined rectangles we'd like to perform the image differencing algorithm on.

Is this possible currently?

Thanks in advance. :geek:
User avatar
iabdalkader
Posts: 629
Joined: Sun May 24, 2015 3:53 pm

Re: Image differencing three separate parts of camera view

Postby iabdalkader » Fri Aug 17, 2018 7:04 pm

I think you can use mask arg to do what you want (difference parts of the image), otherwise I don't see ROI implemented for those functions.
User avatar
kwagyeman
Posts: 2462
Joined: Sun May 24, 2015 2:10 pm

Re: Image differencing three separate parts of camera view

Postby kwagyeman » Fri Aug 17, 2018 7:27 pm

You need to provide a mask image. This can be 1 bit per pixel. However, loading mask images from disk is not implemented yet. Just creating them in ram. So, do this. Load an an image from disk, binary it using the binary method with the bitmap creation options, and do that for each bitmap you need to make. You may also use the to_bitmap() option to create bitmaps. Note that these methods allocate the bitmap in RAM on the heap so the number is limited.

After you create these images use them with difference as a mask.

I will be out of internet service this weekend. Ibrahim can help with any further details.
Nyamekye,
Sakuyal
Posts: 6
Joined: Wed Aug 15, 2018 7:10 pm

Re: Image differencing three separate parts of camera view

Postby Sakuyal » Tue Aug 21, 2018 2:26 pm

Hey, thanks for the help so far.

I've created a 1-bit-per-pixel bitmap in Photoshop and am trying to load it to RAM--however, I keep getting an error saying that the image is corrupt. I tried to create the same 1-bit image in paint and got an error saying that the image is in an unsupported format. I get the same unsupported format error when I try to load in a .jpg image taken with the example snapshot code... so I'm not sure where I'm going wrong!

Here's the line for reference:
maskImg = image.Image("/masktest.bmp", copy_to_fb=True)
User avatar
kwagyeman
Posts: 2462
Joined: Sun May 24, 2015 2:10 pm

Re: Image differencing three separate parts of camera view

Postby kwagyeman » Tue Aug 21, 2018 3:36 pm

Yes, we don't support loading 1bit per pixel images. Adding support isnt that much work in the C code but I don't have time for this right now. That's why I mentioned that you have to have the camera conversation the image to 1bit per pixel itself. So save the image as the pgm or ppm file and then you can you use the to_bitmap() method to covert the image to a mask.
Nyamekye,
Sakuyal
Posts: 6
Joined: Wed Aug 15, 2018 7:10 pm

Re: Image differencing three separate parts of camera view

Postby Sakuyal » Tue Aug 21, 2018 6:23 pm

Got it working, thanks!
User avatar
kwagyeman
Posts: 2462
Joined: Sun May 24, 2015 2:10 pm

Re: Image differencing three separate parts of camera view

Postby kwagyeman » Tue Aug 21, 2018 7:28 pm

Could you post a snippet of code for others?
Nyamekye,
Sakuyal
Posts: 6
Joined: Wed Aug 15, 2018 7:10 pm

Re: Image differencing three separate parts of camera view

Postby Sakuyal » Thu Aug 23, 2018 12:46 pm

Sure.

I exported a .pbm in Photoshop and used a converter to turn it into a .pgm. Then I applied the mask to the image before frame differencing.

Code: Select all

while(True):
    clock.tick() # Track elapsed milliseconds between snapshots().
    img = sensor.snapshot().b_and("/masktest.pgm") # Take a picture and return the image.
    frame_count += 1
    if (frame_count > BG_UPDATE_FRAMES):
        frame_count = 0
        img.save("temp/bg.bmp")

    # Replace the image with the "abs(NEW-OLD)" frame difference.
    img.difference("temp/bg.bmp")
The only line you really have to change is the one where img is declared. Plan on attempting multiple differencing operations today--I can post the results of that here later as well.
Sakuyal
Posts: 6
Joined: Wed Aug 15, 2018 7:10 pm

Re: Image differencing three separate parts of camera view

Postby Sakuyal » Fri Aug 24, 2018 3:58 pm

Forgot to post yesterday, but here's what I ended up doing. Discovered that there was a way to reduce the region-of-interest for the histogram and so I ultimately didn't need any masks at all.
Here's an example line of how I implemented that:

Code: Select all

diffLeft = img.get_histogram(roi=(0, 215, 100, 25)).get_percentile(0.99).value() - img.get_histogram(roi=(0, 215, 100, 25)).get_percentile(0.90).value()

Return to “Technical Discussion”

Who is online

Users browsing this forum: No registered users and 5 guests