I am trying to get the values of all filled boxes, I have attached the code below right now sometimes it misses filled boxes too can anyone let me know how can i improve their accuracy, also how can I give id to all circles so then i get the id of each circle and assign some value to each id, also i have to check which id circle is filled and which not so then i can assign the value of every filled cirle.
So basically what I am trying I want the all-filled circle value in id, value pair and check which one is filled and which one is not, and then fill all the fix circle values into DB.
# Imports:
import numpy as np
import cv2
# Image path:
fileName = "bb.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 = 800
minAspectRatio = 0.9
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, 3)
# Show the results of the filter:
cv2.imshow("Bounding Rects", inputImage)