I used inRange
function to detect a colorful blob in a mp4 video, which I found the idea from this stackoverflow link:
dark = (130, 140, 30) #BGR
light = (200, 190, 100) #BGR
mask = cv2.inRange(frame, dark, light)
try:
center = get_center(mask)
cv2.circle(canvas, center, 6, light, -1)
except (ValueError, ZeroDivisionError):
print("Error")
pass
cv2.imshow('mask green', mask)
and
def get_center(mask):
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
blob = max(contours, key=lambda el: cv2.contourArea(el))
M = cv2.moments(blob)
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
return center
The problem is that this idea is not very reliable for my case; because the object I want to find its position, moves in the room and it becomes lighter or darker with changes in light intensity.
Update In the image below, all the pieces are exactly the same shape with the same color, but they become dark and light due to light and shadow.
Another thing I need to say is that I have to identify 4 pieces with three different colors (green, pink and blue). The image above is sample for just one of these pieces.
I also changed the color space like this with no luck:
frame1=frame.copy()
frame1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2HSV)
#mask= cv2.inRange(frame, (low_H, low_S, low_V), (high_H, high_S, high_V))
mask = cv2.inRange(frame1, (160, 30, 20), (180, 100, 80))