Count the white pixels on image

General discussion about topics related to OpenMV.
joao-pinto94
Posts: 6
Joined: Tue Dec 03, 2019 11:54 am

Count the white pixels on image

Postby joao-pinto94 » Fri Jan 17, 2020 3:10 pm

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
User avatar
kwagyeman
Posts: 4431
Joined: Sun May 24, 2015 2:10 pm

Re: Count the white pixels on image

Postby kwagyeman » Fri Jan 17, 2020 3:39 pm

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.
Nyamekye,
joao-pinto94
Posts: 6
Joined: Tue Dec 03, 2019 11:54 am

Re: Count the white pixels on image

Postby joao-pinto94 » Sat Jan 18, 2020 7:20 am

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
User avatar
kwagyeman
Posts: 4431
Joined: Sun May 24, 2015 2:10 pm

Re: Count the white pixels on image

Postby kwagyeman » Sat Jan 18, 2020 3:04 pm

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.
Nyamekye,
joao-pinto94
Posts: 6
Joined: Tue Dec 03, 2019 11:54 am

Re: Count the white pixels on image

Postby joao-pinto94 » Wed Mar 25, 2020 10:33 am

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
User avatar
kwagyeman
Posts: 4431
Joined: Sun May 24, 2015 2:10 pm

Re: Count the white pixels on image

Postby kwagyeman » Wed Mar 25, 2020 11:23 pm

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.
Nyamekye,

Return to “General Topics”

Who is online

Users browsing this forum: No registered users and 7 guests