Skip to content Skip to sidebar Skip to footer

How To Find The Colored Circles In An Image Opencv Python For Game Development

I have an image of an layout I'm tyring to find out the somewhat coloured circles this piece of code is not able to identify the circles This is the image I need to identify the c

Solution 1:

You could try by filtering out your circles by transforming the image into HSV color space - cv2.COLOR_BGR2HSV. Then you can search your for your colors with cv2.inRange() and draw them on a mask (different mask for every color)

Something like this:

enter image description here

enter image description here

enter image description here

After you have the circles filtered out, you can search for contours with cv2.findContours() and find its position (you can search for it's center point or its extreme points) to determen where on the image you should put your smaller images. Note that I have resised your smaller images manually and if you whould want to have different sizes of the same picture you will have to modify the code. Also if you would like to keep transparency you should play with the chanells of your images and change the code. This is just an example on how I would approach the task.

Example code (without transparency and with same size pictures):

import cv2
import numpy as np

img = cv2.imread('thermal2.png')
dog = cv2.imread('resize1.jpg')
donkey = cv2.imread('resize2.jpg')
monkey = cv2.imread('resize3.png')

resize1 = cv2.resize(dog, (35, 40))
resize2 = cv2.resize(donkey, (60, 35))
resize3 = cv2.resize(monkey, (40, 40))

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

lower_blue = np.array([110,50,50])
upper_blue = np.array([130,255,255])
lower_red = np.array([0,50,50])
upper_red = np.array([10,255,255])
lower_yellow = np.array([30,50,50])
upper_yellow = np.array([50,255,255])

mask_blue = cv2.inRange(hsv, lower_blue, upper_blue)
mask_red = cv2.inRange(hsv, lower_red, upper_red)
mask_yellow = cv2.inRange(hsv, lower_yellow, upper_yellow)

res_blue = cv2.bitwise_and(img,img, mask=mask_blue)
res_red = cv2.bitwise_and(img,img, mask=mask_red)
res_yellow = cv2.bitwise_and(img,img, mask=mask_yellow)

gray_blue = cv2.cvtColor(res_blue, cv2.COLOR_BGR2GRAY)
gray_red = cv2.cvtColor(res_red, cv2.COLOR_BGR2GRAY)
gray_yellow = cv2.cvtColor(res_yellow, cv2.COLOR_BGR2GRAY)

_,thresh_blue = cv2.threshold(gray_blue,10,255,cv2.THRESH_BINARY)
_,thresh_red = cv2.threshold(gray_red,10,255,cv2.THRESH_BINARY)
_,thresh_yellow = cv2.threshold(gray_yellow,10,255,cv2.THRESH_BINARY)

_, contours_blue, hierarhy1 = cv2.findContours(thresh_blue,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
_, contours_red, hierarhy2 = cv2.findContours(thresh_red,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
_, contours_yellow, hierarhy3 = cv2.findContours(thresh_yellow,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

for c in contours_red:
    size = cv2.contourArea(c)
    if size > 30:
        cXY_left = tuple(c[c[:, :, 0].argmin()][0])
        cXY_top = tuple(c[c[:, :, 1].argmin()][0])
        cX = cXY_left[0]
        cY = cXY_top[1]
        img[cY:cY+resize1.shape[0], cX:cX+resize1.shape[1]]=resize1

for c in contours_blue:
    size = cv2.contourArea(c)
    if size > 30:
        cXY_left = tuple(c[c[:, :, 0].argmin()][0])
        cXY_top = tuple(c[c[:, :, 1].argmin()][0])
        cX = cXY_left[0]
        cY = cXY_top[1]
        img[cY:cY+resize2.shape[0], cX:cX+resize2.shape[1]]=resize2

for c in contours_yellow:
    size = cv2.contourArea(c)
    if size > 30:
        cXY_left = tuple(c[c[:, :, 0].argmin()][0])
        cXY_top = tuple(c[c[:, :, 1].argmin()][0])
        cX = cXY_left[0]
        cY = cXY_top[1]
        img[cY:cY+resize3.shape[0], cX:cX+resize3.shape[1]]=resize3

cv2.imshow('blue', res_blue)
cv2.imshow('red', res_red)
cv2.imshow('yellow', res_yellow)
cv2.imshow('img',img)

Result:

enter image description here


Post a Comment for "How To Find The Colored Circles In An Image Opencv Python For Game Development"