【问题标题】:Object detection within image using cv2 and Python使用 cv2 和 Python 在图像中进行对象检测
【发布时间】:2021-01-14 21:35:56
【问题描述】:

我有这张图片

我有兴趣在此图像中找到对象(即点)并在每个对象周围绘制一个矩形。我开始知道cv2 并发现这很容易做到。所以到目前为止,我已经通过一些快速的谷歌搜索编写了以下代码:

import numpy as np
import matplotlib.pyplot as plt
import cv2 

print( cv2.__version__ )

# source data
img_file= "data1.png"

# create an OpenCV image
img= cv2.imread(img_file)

plt.imshow(img, cmap='gray')

# Define the classifiers

# pre-trained classifiers 
Point_classifier="haarcascade_eye.xml"   

# convert color image to grey image

gray_img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# CREATE TRACKER

point_tracker=cv2.CascadeClassifier(Point_classifier)

# detect points
points= point_tracker.detectMultiScale(gray_img)
print(points)

Here I used `haarcascade_eye.xml` it looks similar to the point feature of the image, Is this correct? or do I need to use another classifier for this object?

但结果并不如预期。我期待像这样的图,其中每个点都应单独标记为矩形。

对此或我在代码中出错的地方有任何帮助。提前致谢。

【问题讨论】:

    标签: python computer-vision object-detection cv2


    【解决方案1】:

    haarcascade_eye.xml 用于检测人眼。左眼和右眼甚至还有单独的。不要使用它。

    您的代码行 points= point_tracker.detectMultiScale(gray_img) 失败,因为在您的图像中未检测到眼睛。在这种情况下,您应该使用 SimpleBlobDetector。在SimpleBlobDetector中,您可以根据您的要求过滤阈值、面积、圆度、凸度、惯性等。我试过了,但我的内核一直在死机,也许是因为它是一个大图像。试试小一点的。

    但我们可以尝试其他方法 - 我找到了轮廓,然后根据面积对其进行过滤以避免小点。

    import matplotlib.pyplot as plt
    import cv2 
    
    # source data
    img_file= "data1.jpg"
    
    # create an OpenCV image
    img= cv2.imread(img_file)
    
    # convert color image to grey image
    gray_img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    
    im_gauss = cv2.GaussianBlur(gray_img, (5, 5), 0)
    ret, thresh = cv2.threshold(im_gauss, 127, 255, 0)
    # get contours
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    
    margin = 40
    # calculate area and filter 
    for con in contours:
        area = cv2.contourArea(con)
        if 100 < area < 500:
            x,y,w,h = cv2.boundingRect(con)
            cv2.rectangle(img, (x-margin, y-margin), (x + w+margin, y + h+margin), (0,255,0), 2)
            
    plt.imshow(img, cmap='gray')
    

    如果您想将 3-4 个点组合为一个 - 您可以尝试:

    1- 当边界框重叠时创建一个大边界框。

    2- 闭合操作 - 在找到轮廓之前(先膨胀后腐蚀)。

    【讨论】:

    • 感谢它为我工作。但是当数据点靠得很近时,很难看到矩形区域。有什么方法可以让我们轻松识别这些数据点?另外,当我缩放图像分辨率变差时,有什么办法可以解决这个问题。我的意思是获得高质量的最终图像。
    • 很高兴为您提供帮助。是的,附近的数据点很痛苦。没有现成的解决方案。尝试我在答案末尾列出的两个选项。如果它对您有用,请接受答案。而不是缩放,以更大的分辨率显示图像(使用 plt 的 figsize)。我们在处理的任何地方都没有触及原始图像,因此它与分辨率下降无关
    • 很抱歉再次打扰您。如何计算每个点的每个矩形的坐标(顶部、底部、宽度、长度)?
    猜你喜欢
    • 2014-03-03
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 2022-07-12
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    相关资源
    最近更新 更多