DEV Community

loading...

Object Detection with Color

erol profile image Erol ・2 min read

Object Detection with Color

In this project, I've worked on object detection, tried to find coordinates, width, height of object (in this project object is blue things).

Using HSV color range which is determined as Lower and Upper, I detected colorful object. Here I prefered blue objects.

# blue HSV
blueLower = (84,  98,  0)
blueUpper = (179, 255, 255)
Enter fullscreen mode Exit fullscreen mode

When I got the color range, I set capture size and then I read the capture.

First I apply Gaussian Blurring for decreasing the noises and details in capture.

#blur
blurred = cv2.GaussianBlur(imgOriginal, (11,11), 0)
Enter fullscreen mode Exit fullscreen mode

After Gaussian Blurring, I convert that into HSV color format.

# HSV
hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)
Enter fullscreen mode Exit fullscreen mode

To detect Blue Object, I define a mask.

# mask for blue
mask = cv2.inRange(hsv, blueLower, blueUpper)
Enter fullscreen mode Exit fullscreen mode

After mask, I have to clean around of masked object. Therefor I apply first Erosion and then Dilation

# deleting noises which are in area of mask
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
Enter fullscreen mode Exit fullscreen mode

After removing noises, the Contours have to be found

contours, _ = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
center = None
Enter fullscreen mode Exit fullscreen mode

If the Contours have been found, I'll get the biggest contour due to be well.

# get max contour
c = max(contours, key=cv2.contourArea)
Enter fullscreen mode Exit fullscreen mode

The Contours which are found have to be turned into rectangle deu to put rectangle their around. This cv2.minAreaRect() function returns a rectangle which is smallest to cover the area of object.

rect = cv2.minAreaRect(c)
Enter fullscreen mode Exit fullscreen mode

In the screen, I want to print the information of rectangle, therefor I need to reach its inform.

((x,y), (width, height), rotation) = rect
s = f"x {np.round(x)}, y: {np.round(y)}, width: {np.round(width)}, height: {np.round(height)}, rotation: {np.round(rotation)}"
Enter fullscreen mode Exit fullscreen mode

Using this rectangle I found, I want to get a Box. In the next, I will use this Box for drawing Rectangle.

# box
box = cv2.boxPoints(rect)
box = np.int64(box)
Enter fullscreen mode Exit fullscreen mode

Image Moment is a certain particular weighted average (moment) of the image pixels' intensities.
To find Momentum, I use Max. Contour named as "c". After that, I find Center point.

# moment
M = cv2.moments(c)
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
Enter fullscreen mode Exit fullscreen mode

Now, I will draw the center which is found.

# point in center
cv2.circle(imgOriginal, center, 5, (255, 0, 255), -1)
Enter fullscreen mode Exit fullscreen mode

After Center Point, I draw Contour

# draw contour
cv2.drawContours(imgOriginal, [box], 0, (0, 255, 255), 2)
Enter fullscreen mode Exit fullscreen mode

I want to print coordinators etc. in the screen

# print inform
cv2.putText(imgOriginal, s, (25, 50), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255, 255, 255), 2)
Enter fullscreen mode Exit fullscreen mode

And Final:

detected_img

detected2_img


Project Link: https://github.com/ierolsen/Object-Detection-with-OpenCV/blob/main/4-object-detection-with-color.py

You can find more in my GitHub: https://github.com/ierolsen/Object-Detection-with-OpenCV

Discussion

pic
Editor guide