Ask Your Question

Is it possible to detect a circle with grey color and blue dot in center

asked 2019-11-15 09:46:37 -0500

Is it possible to detect a grey circle in an image with blue dot at the center?

image description

I'm new to open CV can someone please guide?

Thanks, Testmann

edit retag flag offensive close merge delete


If you can guarantee that the blue dot is the only element on the map with that colour, then you don’t need OpenCV. Just average the location of all the blue pixels to get the centre.

sjhalayka gravatar imagesjhalayka ( 2019-11-15 15:38:25 -0500 )edit

Thanks for the response. I want to check the small blue circle along with the outer grey circle

Testmann gravatar imageTestmann ( 2019-11-15 20:19:29 -0500 )edit

Ok. Let me think on it some more.

sjhalayka gravatar imagesjhalayka ( 2019-11-15 20:41:59 -0500 )edit

So, to be clear, what are the data you’re looking for? Location and radius of blue and grey circles?

sjhalayka gravatar imagesjhalayka ( 2019-11-15 20:44:13 -0500 )edit

Can you guarantee that this blue colour is only used for the one and only element?

sjhalayka gravatar imagesjhalayka ( 2019-11-15 21:12:10 -0500 )edit

Are you using Python, or C++, or...?

sjhalayka gravatar imagesjhalayka ( 2019-11-15 21:43:58 -0500 )edit

Blue is the only colour used and I'm using Python

Testmann gravatar imageTestmann ( 2019-11-17 01:31:41 -0500 )edit

2 answers

Sort by » oldest newest most voted

answered 2019-11-18 09:53:48 -0500

supra56 gravatar image


#!/usr/bin/env python37
#Raspberry pi 3/4, OpenCV 4.1.2
#Date: November 18th 2019

import cv2 as cv
import numpy as np

def select_blue(image):
    hsv = cv.cvtColor(image, cv.COLOR_RGB2HSV)
    # blue color mask
    lower = np.uint8([100,50,50])
    upper = np.uint8([130,255,255])
    blue_mask = cv.inRange(image, lower, upper)

    return blue_mask # cv.bitwise_and(image, image, mask = mask)

while True:
    frame = cv.imread('map.png')
    hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)

    mask =  select_blue(hsv)

    element = cv.getStructuringElement(cv.MORPH_RECT,(3,3))
    mask = cv.erode(mask,element, iterations=2)
    mask = cv.dilate(mask,element,iterations=2)
    mask = cv.erode(mask,element)

    contours, hierarchy = cv.findContours(mask, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
    maximumArea = 0
    bestContour = None
    for contour in contours:
        currentArea = cv.contourArea(contour)
        if currentArea > maximumArea:
            bestContour = contour
            maximumArea = currentArea
    if bestContour is not None:
        x,y,w,h = cv.boundingRect(bestContour)
        cv.rectangle(frame, (x,y),(x+w,y+h), (0,0,255), 1)        

    cv.imwrite('map.jpg', frame)    

    if cv.waitKey(1) & 0xFF == ord('q'):


Output: detect_map

edit flag offensive delete link more

answered 2019-11-16 01:02:57 -0500

you can try the code here which produces a result image like below

image description

also you can support the idea of merging related algorithms in OpenCV see related issue

edit flag offensive delete link more


This is very near. I want to detect only the circle which has a blue dot/circle. Is there Python code for this?

Testmann gravatar imageTestmann ( 2019-11-17 01:33:05 -0500 )edit

Thanks for the effort!

Testmann gravatar imageTestmann ( 2019-11-17 01:33:14 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2019-11-15 09:46:37 -0500

Seen: 245 times

Last updated: Nov 18 '19