image.and()

Hi:

I’m a total newbie with Python. I was looking at the edge detecting example and noticed there is some random pixels. I’m trying to remove these by taking a snapshot (img), doing a high pass filter, taking another snapshot (img2), and applying a high pass filter. I’m looking at doing an and between these two images,which could reduce random pixels. The IDE doesn’t seem to recognize “image.and()”

My code:

# Edge Detection Example:
#
# This example demonstrates using the morph function on an image to do edge
# detection and then thresholding and filtering that image afterwards.

import sensor, image, time
from pyb import LED

blue_led  = LED(3)

kernel_size = 1 # kernel width = (size*2)+1, kernel height = (size*2)+1
kernel = [-1, -1, -1,\
          -1, +8, -1,\
          -1, -1, -1]

# This is a high pass filter kernel. see here for more kernels:
# http://www.fmwconcepts.com/imagemagick/digital_image_filtering.pdf
thresholds = [(100, 255)] # grayscale thresholds

sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # or sensor.RGB565
sensor.set_framesize(sensor.QVGA) # or sensor.QVGA (or others)
sensor.skip_frames(10) # Let new settings take affect.
clock = time.clock() # Tracks FPS.

# On the OV7725 sensor, edge detection can be enhanced
# significantly by setting the sharpness/edge registers.
# Note: This will be implemented as a function later.
if (sensor.get_id() == sensor.OV7725):
    sensor.__write_reg(0xAC, 0xDF)
    sensor.__write_reg(0x8F, 0xFF)

#    clock.tick() # Track elapsed milliseconds between snapshots().

img = sensor.snapshot() # Take a picture and return the image.
img.morph(kernel_size, kernel)

while(True):
    img2 = sensor.snapshot() # Take another picture and return the image.
    img2.morph(kernel_size, kernel)


    img =  img2.and(img)
    print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while

    blue_led.toggle()

Any help appreciated

This should work.

img.and(img2)

We don’t really have assignments working for images. So, just write the “img.and(img2)” part by itself.

Hi Nyamekye:

I tried “img.and(img2)” but I get a syntax error. The IDE has the “and” part in light blue. Looks like it thinks it’s a “and” like in “if … and …”

It looks like we’re using a reserved word for a function name, Python probably doesn’t like that. Anyway, I see a few other problems with your script:

  • The camera doesn’t allocate a new image each time you call snapshot() it just returns an image that points to the framebuffer, so basically you’re and’ing the image with itself. If you really want a copy of the image you should call copy(), keep in mind the camera doesn’t have enough RAM to keep two QVGA frames, so this will only work with QQCIF:
img = sensor.snapshot().copy()
  • If you keep a copy of the image you’ll be and’ing with the first frame, maybe you should move that line inside the loop and refresh the image copy every n frame.

  • You need to call clock.tick() inside the loop to update the FPS counter.

  • If the image is noisy you could try smoothing the image first with a median filter, for that you don’t need a second copy:

img = sensor.snapshot().median(1)

Finally, you should try the CANNY edge detector (see 09-Feature-Detection/edges.py), it performs much better than basic morphing

Hi, sorry about and not working… that will get fixed. Until then, here’s a version of your script which uses the SD card to store the second image:

# Edge Detection Example:
#
# This example demonstrates using the morph function on an image to do edge
# detection and then thresholding and filtering that image afterwards.

import sensor, image, time
from pyb import LED

blue_led  = LED(3)

kernel_size = 1 # kernel width = (size*2)+1, kernel height = (size*2)+1
kernel = [-1, -1, -1,\
          -1, +8, -1,\
          -1, -1, -1]

# This is a high pass filter kernel. see here for more kernels:
# http://www.fmwconcepts.com/imagemagick/digital_image_filtering.pdf
thresholds = [(100, 255)] # grayscale thresholds

sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # or sensor.RGB565
sensor.set_framesize(sensor.QVGA) # or sensor.QVGA (or others)
sensor.skip_frames(10) # Let new settings take affect.
clock = time.clock() # Tracks FPS.

# On the OV7725 sensor, edge detection can be enhanced
# significantly by setting the sharpness/edge registers.
# Note: This will be implemented as a function later.
if (sensor.get_id() == sensor.OV7725):
    sensor.__write_reg(0xAC, 0xDF)
    sensor.__write_reg(0x8F, 0xFF)

#    clock.tick() # Track elapsed milliseconds between snapshots().

img = sensor.snapshot() # Take a picture and return the image.
img.morph(kernel_size, kernel)
img.invert()
img.save("temp.bmp")

while(True):
    img = sensor.snapshot() # Take another picture and return the image.
    img.morph(kernel_size, kernel)
    img.nand("temp.bmp")
    print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while
    blue_led.toggle()

You can use the sd card to save images to and then you can load them back from the SD card for some functions. We use a huge RAM buffer to do this so it’s quite fast. Notice how I’m using the invert and then NAND functions instead of AND.

(This script still runs pretty slow because you’re doing a convolution).

Tx

I was thinking about using the microSD to save images. “nand” and “xor” do not give a syntax error, but “and” and “or” do. Maybe use caps (AND, NAND, XOR, OR, …)?

kwagyeman your code works. I’m trying to do a navigation map. Identify points of interest on a vertical image and translate to a horizontal plain. Use an IR distance sensor to get distance on points of interest. Build a map.

Moe

The and/or problem will get fixed. Sorry about that. I was sure I tested this a while ago… maybe MP changed made the parsing more strict.