Page 1 of 1

Count the white pixels on image

Posted: Fri Jan 17, 2020 3:10 pm
by joao-pinto94
Hi,
Theres any exemple of using greyscale YUV color space converted to monochrome and return the number of white points on image?

With the best Regards,
João

Re: Count the white pixels on image

Posted: Fri Jan 17, 2020 3:39 pm
by kwagyeman
Call the find_blobs(), pass color thresholds which look for white, then sum the pixels() value for each blob up. See the grayscale color tracking example to get started.

Re: Count the white pixels on image

Posted: Sat Jan 18, 2020 7:20 am
by joao-pinto94
Hi kwagyeman,

Thank you for the answer. Currenctly my code is:

Code: Select all

import sensor, image, time, math
thresholds = (245, 255)

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 3000) #Just to understand if the sensor needs time before start
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()
while(True):
    clock.tick()
    img = sensor.snapshot()
    for blob in img.find_blobs([thresholds], pixels_threshold=10, area_threshold=10, merge=True):
        x = blob.pixels()

        img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=90, color=127)
    sensor.flush()
    print(clock.fps(), x)
    
But some times i recive the error saying the 'x' isn't defined.
Could you please help me and understand if this is the better code for the solution?

With the best regards,
Joao

Re: Count the white pixels on image

Posted: Sat Jan 18, 2020 3:04 pm
by kwagyeman

Code: Select all

import sensor, image, time, math
thresholds = (245, 255)

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 3000) #Just to understand if the sensor needs time before start
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()
while(True):
    clock.tick()
    img = sensor.snapshot()
    blobs = img.find_blobs([thresholds], pixels_threshold=10, area_threshold=10, merge=True)
    for blob in blobs:
        img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=90, color=127)
    white_pixels = sum(blob.pixels() for blob in blobs)
    print(clock.fps(), white_pixels)
You should read up on python lists and how to work with them. This is what makes python so powerful on the OpenMV Cam.

Re: Count the white pixels on image

Posted: Wed Mar 25, 2020 10:33 am
by joao-pinto94
Thank you everyone.

Could you please explain how to define a color to find? if i want to search for black or red for exemple.

With the best regards,
Joao

Re: Count the white pixels on image

Posted: Wed Mar 25, 2020 11:23 pm
by kwagyeman
Please see the color tracking examples. Color tracking is done in the LAB color space. Tracking is done by thresholding the image with min/maxes for the 3 LAB color channels. This means you need to pass a 6-tuple value to methods to track colors.

The color tracking examples explain this.