简体   繁体   English

试图检测所有实心圆并获得它们的价值? OpenCV Python

[英]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.我正在尝试的基本上我想要所有检测到的填充圆圈值,圆圈将用黑色或蓝色笔填充,我尝试了 BLOB 检测但不幸的是,我没有得到准确度,因为我的图像有很多填充圆圈它表现异常它也检测到点和字母,我附上了下面的代码,但实际上我在尝试什么我想获得实心圆的值以及 blob ID,但在这张图片中,我有很多数据所以我想检测所有填充的圆并给它们一个唯一的 ID,然后在代码中我将创建一个 ID 数组并检测哪个 ID 被填充,例如如果 ID=22 被填充,那么我将为检测到的 ID 分配一些值。 So here is my Image: my template image So here is my output: my output image这是我的图片:我的模板图片这是我的 output:我的 output 图片

# 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) _, 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)

I'm about 95%.我大约95%。 Actually, I almost solved your problem by add 2 extra algorithm.实际上,我几乎通过添加 2 个额外的算法解决了你的问题。 Also the boundingBoxes doesn't do anything, but return none.此外, boundingBoxes什么也不做,但不返回任何内容。 I cannot get values.我无法获得价值。 Because I don't installed yet Tesseract.因为我还没有安装 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: Output:

在此处输入图像描述

Btw, when you taking picture, make sure a good quality lighting condition.顺便说一句,当你拍照时,确保良好的照明条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM