'Trying to detect all the filled circle and get their value? OpenCV Python

What I am trying basically I want all the filled detected circle values, the circle will be filled with black or blue pen, I tried BLOB detection but unfortunately, I did not get the accuracy because my image has lots of filled circles it acts abnormally like it detected dot and letters too, I am attaching the code below but what actually I'm trying I want to get the value of filled circle along with blob ID, but in this image, I have a lot of data so I am trying to detect all the filled circle and give them a unique ID and then later in code I will create an array of ID and will detect which ID is filled for example if ID=22 is filled then I will assign some value to that detected ID. So here is my Image: my template image So here is my output: my output image

# Imports:
import numpy as np
import cv2
# Image path:
fileName = "Filledimage1.jpg"
#path = "D://opencvImages//"

# Read image:
inputImage = cv2.imread(fileName)

# To Grayscale:
grayscaleImage = cv2.cvtColor(inputImage, cv2.COLOR_BGR2GRAY)

# Threshold:

_, binaryImage = cv2.threshold(grayscaleImage, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY_INV)

# Get Connected Components:
output = cv2.connectedComponentsWithStats(binaryImage, 8, cv2.CV_32S)
(numLabels, labels, stats, centroids) = output

# Store target bounding boxes here:
boundingBoxes = []

# Loop through connected components:
for i in range(1, numLabels):

# Get blob properties:
x = stats[i, cv2.CC_STAT_LEFT]
y = stats[i, cv2.CC_STAT_TOP]
w = stats[i, cv2.CC_STAT_WIDTH]
h = stats[i, cv2.CC_STAT_HEIGHT]
blobArea = stats[i, cv2.CC_STAT_AREA]
(cX, cY) = centroids[i]

# Get bounding box area:
boxArea = w * h

# Compute area difference
areaDifference = boxArea - blobArea
# Compute aspect ratio:
aRatio = w / h

# Set blob filter:
minAreaDifference = 1200
minAspectRatio = 0.3
maxAspectRatio = 1.1
# Default color is red:
color = (0, 0, 255)

if areaDifference < minAreaDifference:
    if aRatio > minAspectRatio and aRatio < maxAspectRatio:
        # Got target blob
        # Color is now blue:
        color = (255, 0, 0)
        # Store bounding box in list:
        boundingBoxes.append((x, y, w, h))

# Draw rectangle:
cv2.rectangle(inputImage, (x, y), (x + w, y + h), color, 1)

# Show the results of the filter:
cv2.imshow("Bounding Rects", inputImage)
cv2.waitKey(0)


Solution 1:[1]

I'm about 95%. Actually, I almost solved your problem by add 2 extra algorithm. Also the boundingBoxes doesn't do anything, but return none. I cannot get values. Because I don't installed yet Tesseract. I don't have time. You have to play around.

import numpy as np
import cv2
# Image path:
fileName = "test_circle.png"
#path = "D://opencvImages//"

# Read image:
inputImage = cv2.imread(fileName)

# To Grayscale:
grayscaleImage = cv2.cvtColor(inputImage, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(grayscaleImage, (3, 3), 0)
bilateral = cv2.bilateralFilter(blurred,3,5,5)
_, binaryImage = cv2.threshold(bilateral, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY_INV)

# Get Connected Components:
output = cv2.connectedComponentsWithStats(binaryImage, 8, cv2.CV_32S)
(numLabels, labels, stats, centroids) = output


# Loop through connected components:
for i in range(1, numLabels):

    # Get blob properties:
    x = stats[i, cv2.CC_STAT_LEFT]
    y = stats[i, cv2.CC_STAT_TOP]
    w = stats[i, cv2.CC_STAT_WIDTH]
    h = stats[i, cv2.CC_STAT_HEIGHT]
    blobArea = stats[i, cv2.CC_STAT_AREA]
    (cX, cY) = centroids[i]

    # Get bounding box area:
    boxArea = w * h

    # Compute area difference
    areaDifference = boxArea - blobArea
    # Compute aspect ratio:
    aRatio = w / h

    # Set blob filter:
    minAreaDifference = 57
    minAspectRatio = 0.3
    maxAspectRatio = 1.1
    # Default color is red:
    color = (0, 0, 255)

    if areaDifference < minAreaDifference:
        if minAspectRatio < aRatio < maxAspectRatio:
            # Got target blob
            # Color is now blue:
            color = (255, 0, 0)
            

        # Draw rectangle:
        cv2.rectangle(inputImage, (x, y), (x + w, y + h), color, 1)   

# Show the results of the filter:
cv2.imshow("Bounding Rects", inputImage)
cv2.waitKey(0)

Output:

enter image description here

Btw, when you taking picture, make sure a good quality lighting condition.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 toyota Supra