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)
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)
After Gaussian Blurring, I convert that into HSV color format.
# HSV
hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)
To detect Blue Object, I define a mask.
# mask for blue
mask = cv2.inRange(hsv, blueLower, blueUpper)
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)
After removing noises, the Contours have to be found
contours, _ = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
center = None
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)
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)
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)}"
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)
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"]))
Now, I will draw the center which is found.
# point in center
cv2.circle(imgOriginal, center, 5, (255, 0, 255), -1)
After Center Point, I draw Contour
# draw contour
cv2.drawContours(imgOriginal, [box], 0, (0, 255, 255), 2)
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)
And Final:
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
Top comments (0)