Ask Your Question
0

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

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

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

Comments

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 -0600 )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 -0600 )edit

Ok. Let me think on it some more.

sjhalayka gravatar imagesjhalayka ( 2019-11-15 20:41:59 -0600 )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 -0600 )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 -0600 )edit

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

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

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

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

2 answers

Sort by » oldest newest most voted
0

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

supra56 gravatar image

Code:

#!/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)    
    cv.imshow('frame',frame)

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

cv.destroyAllWindows()

Output: detect_map

edit flag offensive delete link more
0

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

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

Comments

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 -0600 )edit

Thanks for the effort!

Testmann gravatar imageTestmann ( 2019-11-17 01:33:14 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 1,368 times

Last updated: Nov 18 '19