Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

DZone's Guide to

# Tracking Blue Objects With OpenCV and Python

### The idea is simple: track blue objects. We'll use OpenCV, an amazing open-source computer vision library, and Python to do it.

· AI Zone ·
Free Resource

Comment (0)

Save
{{ articles[0].views | formatCount}} Views

Did you know that 50- 80% of your enterprise business processes can be automated with AssistEdge?  Identify processes, deploy bots and scale effortlessly with AssistEdge.

OpenCV is an amazing open-source computer vision library. Today, we're going to hack a little bit with it. The idea is track blue objects. Why blue objects? Maybe because I've got a couple of them in my desk. Let's start.

The idea is simple. We'll create a mask. Our mask is a black and white image where each blue pixel will turn into a white one and the rest of pixels will be black.

Original frame:

Now, we only need put a bounding rectangle around the blue object.

import cv2
import numpy

cam = cv2.VideoCapture(0)
kernel = numpy.ones((5 ,5), numpy.uint8)

while (True):
rangomax = numpy.array([255, 50, 50]) # B, G, R
rangomin = numpy.array([51, 0, 0])
# reduce the noise

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)

cv2.imshow('camera', frame)

k = cv2.waitKey(1) & 0xFF

if k == 27:
break

And that's all. A nice hack for a Sunday morning!

The source code is on GitHub!

Consuming AI in byte sized applications is the best way to transform digitally. #BuiltOnAI, EdgeVerve’s business application, provides you with everything you need to plug & play AI into your enterprise.  Learn more.

Topics:
ai ,object tracking ,opencv ,python ,tutorial

Comment (0)

Save
{{ articles[0].views | formatCount}} Views

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.