【问题标题】:Error when rectangular region of interest goes out of bounds - opencv矩形感兴趣区域超出范围时出错 - opencv
【发布时间】:2018-12-02 19:31:41
【问题描述】:

我的脚本检测到人脸,然后使用 dlib 相关跟踪器算法对其进行跟踪。

我正在尝试使用cv2.imshow(track_index, face_img) 在单独的窗口中显示每个跟踪的人脸,其中face_image 是使用dlib.rectangle 坐标从视频中捕获的帧裁剪的人脸感兴趣区域。

部分代码如下:

                    #get the updated tracker position
                    pos = tracker.get_position()
                    pos = dlib.rectangle(
                        int(pos.left()),
                        int(pos.top()),
                        int(pos.right()),
                        int(pos.bottom()),
                    )
                    #draw a bounding box around the tracked face
                    cv2.rectangle(image, (pos.left(), pos.top()), (pos.right(), pos.bottom()),
                                  (100, 200, 100))
                    #crop the face from the frame
                    face_img = image[pos.top():pos.bottom(),pos.left():pos.right()]
                    #refers to the number of the track created
                    track_index = "track no.{}".format(trc - i)
                    font = cv2.FONT_HERSHEY_TRIPLEX
                    cv2.putText(image, track_index, (pos.left(), pos.bottom() +12), font, 0.5, (255, 255, 0))
                    #show the tracked face
                    cv2.imshow(track_index, face_img)

这可以正常工作,直到面部超出边界或第一次出现在框架边界的一半之外。在这种情况下,程序会停止并引发大小错误。

Traceback (most recent call last):
  File "/home/Developing space/facetrack/hog_detect_face_track.py", line 44, in <module>
    cv2.imshow(track_index, face_img)
cv2.error: OpenCV(3.4.1) /io/opencv/modules/highgui/src/window.cpp:356: error: (-215) size.width>0 && size.height>0 in function imshow

如何强制框架边框内的 ROI 阻止此错误发生?

【问题讨论】:

    标签: python opencv face-detection dlib imshow


    【解决方案1】:

    对 ROI 进行边界检查。

    h,w = image.shape[:2]
    face_img = image[max(0,pos.top()):min(pos.bottom(),h),max(0,pos.left()):min(pos.right(),w)]
    

    【讨论】:

      猜你喜欢
      • 2013-02-28
      • 2017-04-10
      • 2013-03-03
      • 1970-01-01
      • 2012-04-25
      • 2013-10-19
      • 2017-09-28
      • 2014-12-23
      • 2017-08-27
      相关资源
      最近更新 更多