OpenTLD VarianceFilter 中导致的段错误

Segfault caused in OpenTLD VarianceFilter

我将 OpenTLD C++ 实现用作库 - 仅包括 libopentld 文件夹。我已经多次成功编译了主要的可执行文件,并且运行顺利。但是使用该库似乎有一个奇怪的特定错误。

我为默认的 opentld 和我自己的项目使用 opencv 3.0。

运行 -g -O0 并通过 gdb 给出以下输出:

Program terminated with signal SIGSEGV, Segmentation fault.
#0  calcVariance (off=0x7f3e060f45b0, this=0x15568a0) at libs/opentld/src/libopentld/tld/VarianceFilter.cpp:67
67      float mX  = (ii1[off[3]] - ii1[off[2]] - ii1[off[1]] + ii1[off[0]]) / (float) off[5]; //Sum of Area divided by area
(gdb) bt
#0  calcVariance (off=0x7f3e060f45b0, this=0x15568a0) at libs/opentld/src/libopentld/tld/VarianceFilter.cpp:67
#1  tld::VarianceFilter::filter (this=0x15568a0, i=23100) at libs/opentld/src/libopentld/tld/VarianceFilter.cpp:89
#2  0x00000000004141cd in tld::DetectorCascade::detect (this=0x1556780, img=...) at libs/opentld/src/libopentld/tld/DetectorCascade.cpp:317
#3  0x00000000004115bc in tld::TLD::initialLearning (this=0x15437c0) at libs/opentld/src/libopentld/tld/TLD.cpp:248
#4  0x0000000000411e0c in tld::TLD::selectObject (this=<optimized out>, img=..., bb=bb@entry=0x7ffcbe8caa70)

当我调用 TLD::selectObject(img, roi) 时,这发生在堆栈中。

我已经隔离了数组访问,看起来 off[5] 是罪魁祸首,但我不确定。似乎他们都访问了没有为他们定义的内存。在 IntegralImage 中,从未定义宽度和高度,但数据数组的大小是 width*height 的约定大小。 (我正在记录的数组访问似乎超出了该范围)

我不知道为什么这适用于普通的可执行文件,但不能从我自己的程序中调用。我已经看了很多次,将普通的剥离成几个电话,但它仍然有效。是否可能与仅使用 Mat 对象而不是 IplImage 有关?

这是我调用 opentld 的代码:

using namespace cv;
Target OpenTLD::findTarget(cv::Mat HSV, bool restart) {
  Target t;
  cvtColor(HSV, t.image, COLOR_HSV2RGB);
  Mat BGR;
  cvtColor(t.image, BGR, COLOR_RGB2BGR);
  Mat grey(HSV.size(), CV_8UC1);
  int ch[] = {2, 0};
  mixChannels(&HSV, 1, &grey, 1, ch, 1);

  if (restart) {
    started = true;
    Rect roi = selectedROI();
    tld->detectorCascade->imgWidth = HSV.cols;
    tld->detectorCascade->imgHeight = HSV.rows;
    tld->detectorCascade->imgWidthStep = HSV.step;
    tld->processImage(BGR);
    tld->selectObject(grey, &roi);
  } else if (started) {
    t.roi = ROI(*tld->currBB);
    tld->processImage(BGR);
  }

  return t;
}

我已验证图像和 ROI 是有效值。

这是由于 HSV.step 给出了错误的值。我使用了宽度值,它工作得很好。