Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pixel counting
#1
Hey all -

I am guessing this is going to include numpy but it may just be a simple math function.

I need to count the number of pixels from a red dot (a reflector) to the center of the picture. The red dot can move as the camera moves toward or away from the reflector.

2 questions:

1) Which is better, numpy or just a simply counting like pixel =+ 1?
2) How can I dentify the proper color?
likes this post
Reply
#2
Got a sample picture?

Are there other objects with red colour in the picture?

Is the red in cv2 just (0,0,255,255) or some hue or range of red?

Basically, I think you need to find the bounding box of the red spot, then count the red pixels in the bb.

This little function was for finding blue spots on dice, you can adjust for colour. It finds the leftmost blue pixel and makes that pixel transparent, so you don't find it again! You probably don't need that, if you only have 1 red spot.

def find_left_blue(x, min_y, max_y, output):
    for y in range(min_y, max_y):
        pixel = output[y,x]
        # pixel is BGR adjust values here for other colours
        # after finding a blue spot, make it transparent, then skip transparent pixels
        if pixel[3] == 0:
            continue
        elif pixel[0] > 251 and pixel[1] < 10 and pixel[2] < 10:
            print(f'found a left blue pixel at {y, x, pixel}')            
            return y,x
Another function finds the rightmost pixel. Then you have the bb of the blue spot, because it is a circle, and can extract the ROI (Region Of Interest) then count the red pixels in there.
Reply
#3
I will have to check on the color.

The reflector is bouncing back a red light from a laser pointer, and as the robot moves the red light moves, but centered is a set location.
Reply
#4
If you post a sample picture, the rest should be easy!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020