Ivan95
Posts: 3
Joined: Mon Jul 30, 2018 7:14 am

Colour Tracking Robot

Mon Jul 30, 2018 7:19 am

Hi! I faced some problem to do this project. I need your guys's help. Thank you!

Code: Select all

while (True):
    ret, frame = cap.read()
    _, frame = cap.read()
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    lower_blue = np.array([110,80,70])
    upper_blue = np.array([130,255,255])
    mask0 = cv2.inRange(hsv, lower_blue, upper_blue)

    lower_blue = np.array([80,75,65])
    upper_blue = np.array([110,255,255])
    mask1 = cv2.inRange(hsv, lower_blue, upper_blue)

    mask=mask0+mask1
    res = cv2.bitwise_and(frame,frame, mask= mask)
    
    opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
    x, y, w, h =cv2.boundingRect(opening)
    cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 3)
    cv2.circle(frame, (x+w/2, y+h/2), 5, (0, 0, 255), -1)

   if (object < x+w/2):
        right()
        sleep(2)

    elif (object > y+h/2):
        left()
        sleep(2)

    else:
        stop()
        sleep(2)

    cv2.imshow('frame', frame)
    cv2.imshow('mask',mask)
    cv2.imshow('res',res)

    
        
    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break
GPIO.cleanup()
cv2.destroyAllWindows()
this is my incomplete code. Can someone help me and point out the error?

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 23352
Joined: Sat Jul 30, 2011 7:41 pm

Re: Colour Tracking Robot

Mon Jul 30, 2018 8:33 am

It would help if you specified what you are trying to do, and what is actually happening....
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
"My grief counseller just died, luckily, he was so good, I didn't care."

Ivan95
Posts: 3
Joined: Mon Jul 30, 2018 7:14 am

Re: Colour Tracking Robot

Mon Jul 30, 2018 8:42 am

My project will have a motor to turn a timing belt which attached a camera. The camera will detect a blue colour object, when the object is moving left or right, the motor will be following the order. I think I should defined what is the " object". But i dont know what should i define.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 23352
Joined: Sat Jul 30, 2011 7:41 pm

Re: Colour Tracking Robot

Mon Jul 30, 2018 9:26 am

Ivan95 wrote:
Mon Jul 30, 2018 8:42 am
My project will have a motor to turn a timing belt which attached a camera. The camera will detect a blue colour object, when the object is moving left or right, the motor will be following the order. I think I should defined what is the " object". But i dont know what should i define.
And what is actually happening with the code?
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
"My grief counseller just died, luckily, he was so good, I didn't care."

Ivan95
Posts: 3
Joined: Mon Jul 30, 2018 7:14 am

Re: Colour Tracking Robot

Mon Jul 30, 2018 10:15 am

How do I can set the number of pixel at the center of an image/frame? I need a coordinates at the center of the captured image in the frame/window. Tq

User avatar
OutoftheBOTS
Posts: 711
Joined: Tue Aug 01, 2017 10:06 am

Re: Colour Tracking Robot

Tue Jul 31, 2018 3:18 am

Ok I will see if I can help you a little.

Code: Select all

    ret, frame = cap.read()
    _, frame = cap.read()
first why do you capture the image twice??

Code: Select all

hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
here you conveter from RGB to HSV colour space. This is a good thing as colour detection is easier in HSV colour space. I think that you don't understand nthe difference between RGB and HSV. 24bit(3 byte) RGB is easy to understand first byte for how much red, second byte for how much green and last byte for how much blue. 24 bit (3byte) HSV is completely different: first byte is Hue (Hue is the colour it is on the hue colour wheel), the second byte is Saturation (how saturated is the colour, i.e is it light fluro lime green or is light khaki green) and the last byte is value (how light or dark is the colour i.e light green or dark green).

Most people doing simple colour detection will look for a pixel to be within a certain range of Hue but allow for full range of S and V (0 - 255)

Hue is a 360 degrees colour wheel but because 360 won't fit into 1 byte (0 - 255) OpenCV divided it in half to you get a range of 0 - 189.
HSV.JPG
HSV.JPG (40.29 KiB) Viewed 404 times
So if you want to find every bit of blue on the colour wheel this will be from 180 to 270 and if we divide this in half fro Open CV we get 90 - 135.
So if we set a lower range [90, 0, 0] and an upper range [135, 255, 255] this will detect every single bit of blue in any saturation or shade . You may choose to narrow this range as it might find more than your looking for.

Code: Select all

mask0 = cv2.inRange(hsv, lower_blue, upper_blue)
This will create a binary mask image called mask0. So above we looked at 24 bit RGB and 24bit HSV this image will be a 1 bit binary image.
It will go over every pixel in the hsv image and if that pixel is within range it will turn the pixel in mask0 on and if not in-range it will turn the pixel in mask0 off.

Once you have the mask there is a number of ways or processing this mask to get what you want. I normally use find contours then bounding rect. see my video here https://www.youtube.com/watch?v=tpwokAPiqfs

Return to “Automation, sensing and robotics”