Python和openCV:HOG描述符检测多尺度返回负边界框

Posted

技术标签:

【中文标题】Python和openCV:HOG描述符检测多尺度返回负边界框【英文标题】:Python and openCV : HOG descriptor detect multiscale returns negative bounding box 【发布时间】:2018-01-08 22:58:18 【问题描述】:

我正在使用 OpenCV 的 HOG 检测器来检测视频中的行人。但是detectMultiScale() 返回的边界框只有一个负值。到目前为止,我在互联网上找不到任何有用或有用的东西来理解和解决这个问题。我什至不知道为什么会出现这个问题。这就是输出。

RECTS:  [[183  -6  68 137]
[ 76  -7  76 152]]
WEIGHTS:  [[ 1.21099767]
[ 0.37004868]]

这是我的代码:

hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

webcam = cv2.VideoCapture ('/home/irum/Desktop/Test-Videos/pedistrianTestVideoLONG.mp4')




while True:
    # read each frame
    ret, frame = webcam.read()
    # resize it
    image = imutils.resize(frame, width=min(300, frame.shape[1]))
    orig = image.copy()

    # detect people in the frame
    (rects, weights) = hog.detectMultiScale(image, winStride=(4, 4),
        padding=(8, 8), scale=1.1)
    print('RECTS: ',rects)
    print('WEIGHTS: ',weights)
    print('LENGTH: ',len(rects))

    # draw the original bounding boxes
    #for (x, y, w, h) in rects:
    for i in range(len(rects)):

        body_i = rects[i]
        print('BODY_I: ',body_i)

        (x, y, w, h) = [v * 1 for v in body_i]
        print ('DETECTION (x, y, w, h)',x, y, w, h)

        cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 0, 255), 2)

        # apply non-maxima suppression
        rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects])
        pick = non_max_suppression(rects, probs=None, overlapThresh=0.65)

        # draw the final bounding boxes

        for i in range(len(pick)):

            g += 1 

            body_p = pick[i]

            (xA, yA, xB, yB) = [int(v * 1) for v in body_p]
            print('DETECTION NMS (xA, yA, xB, yB)', xA, yA, xB, yB)

            # rect on scaled image
            cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2) 

            # rects to map on original frame
            (x1, y1, w1, h1) = [int(v * 4.28) for v in body_p]
            print('(x1, y1, w1, h1) ' ,x1, y1, w1, h1)

            cv2.rectangle(frame, (x1, y1), (w1, h1), (0, 45, 255), 2)

            # Crop body from Original frame
            body_big = frame[y1:h1, x1:w1]

            print('DISPLAY')
            cv2.imshow("BODY", body_big)

            # Save body
            save_body_path = '/home/irum/Desktop/pedestrian-detection/BIG_BODY' 

            cur_date = (time.strftime("%Y-%m-%d"))
            cur_time = (time.strftime("%H:%M:%S"))
            new_pin =cur_date+"-"+cur_time
            filename1 = 'BIG'
            filename2 = str(g)+str(filename1)+'-'+str(new_pin)
            #print ("IMAGE TO SEND: ",filename2)

            sampleFile = ('%s/%s.jpg' % (save_body_path, filename2))
            #print ("sampleFile",sampleFile)

            cv2.imwrite('%s/%s.jpg' % (save_body_path, filename2), body_big)
            #pyplot.imsave('%s.jpg' % (sampleFile), body_big)



    # show the output images
    cv2.imshow("Before NMS", orig)
    cv2.imshow("After NMS", image)
    cv2.imshow("BIG BODY", frame)
    # cv2.imshow("FACE", body_big2)
    key = cv2.waitKey(10)
    if key == 27:
        break

【问题讨论】:

【参考方案1】:

在互联网上尝试了几乎所有方法后,当我在任何地方都找不到答案时,我开始自己分析这个问题并找到了解决方案。

(rects, weights) = hog.detectMultiScale(image, winStride=(4, 4),
        padding=(0, 0), scale=1.1)

我将参数padding 的值更改为(0,0),它解决了我的问题。而且我认为问题是因为当由于给定填充而在框架的角落检测到身体时,它想在裁剪时从框架的侧面占用一些空间,但没有任何可用空间,所以它给了我负面影响.

【讨论】:

以上是关于Python和openCV:HOG描述符检测多尺度返回负边界框的主要内容,如果未能解决你的问题,请参考以下文章

OpenCV 例程 300篇248. 特征描述之HOG描述符

OpenCV 例程 300篇248. 特征描述之HOG描述符

OpenCV HOG 描述符参数

SIFT尺度参数,解释

使用HOG描述符peopledetect.py时出错

Python+OpenCV行人密度计数检测系统