开发者

Problems with CamShift on the OpenCV C++ interface

I'm somewhat new to OpenCV and for some reason, I'm not managing to get CamShift to work in C++. First of all, if anyone has a working CamShift example using the C++ interface I would really appreciate it.

Second, I'm trying to adapt the C example to C++, just to get it to work. Nothing fancy, yet. Basically, what I'm doing is this:

// -----------------------

cv::Rect rect = /* some rectangle */;
cv::Mat img = /* some image */;

int bins = 16;
int sMin = 10;
int vMin = 10;
int vMax = 250;

cv::MatND hist(1, &bins, CV_8UC1);

cv::Rect searchWindow = rect;
cv::Mat roi = img(searchWindow);
cv::Mat hsv;
cv::cvtColor(roi, hsv, CV_RGB2HSV);

cv::Mat mask;
cv::inRange(hsv, cv::Scalar(0, sMin, vMin, 0), cv::Scalar(181, 256, vMax, 0), mask);

const int channel = 0;
float range[] = {0, 181};
const float* ranges[] = {range};
cv::calcHist(&hsv, 1, &channel, mask, hist, 1, &bins, ranges, true, false);

double histMax;
cv::minMaxLoc(hist, NULL, &histMax);

hist *= histMax ? 255.0 / histMax : 0.0;

int channel = 0;
float range[] = {0, 256};
const float* ranges[] = {range};
c开发者_JS百科v::Mat bp;
cv::calcBackProject(&img, 1, &channel, hist, bp, ranges, 1, true);

cv::RotatedRect foundObject = cv::CamShift(bp, searchWindow,
   cv::TermCriteria(cv::TermCriteria::COUNT | cv::TermCriteria::EPS, 10, 1));

// -----------------------
// -----------------------
// -----------------------
// -----------------------

After this, what happens is that inside cv::CamShift(), despite the parameters having seemingly valid values, OpenCV throws a cv::Exception and crashes with the following message:

OpenCV Error: Assertion failed (box.size.width >= 0 && box.size.height >= 0 && t
hickness <= 255) in unknown function, file ..\..\..\..\ocv\opencv\src\cxcore\cxd
rawing.cpp, line 1666

There doesn't seem to be any related bug on the bug tracking database, so... what am I missing something, here?


It is a moderately easy-to-replicate bug. There are situations that arise (moving too fast, especially bumping the camera) that cause the logic of cv::RotatedRect( ... ) to explode and create a box that either collapses to a point or is bigger than the frame. This error is then caught.

When doing exactly this I just made a copy of the code and manually check the box-size in offending places. But at the end of the day I would say this function assumes that your camera is fixed and the scene lighting doesn't change much. It won't work mounted on a car driving around trying to track other cars, for example.

Crashes like this are a great example of why "unit testing" and "code practices" break down in computer vision; its not enough to check that it runs on the test server with no display, but it also needs to run for all ranges of parameters, data spectrum, and user behaviors...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜