views:

28

answers:

0

Hello, everyone.

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};
cv::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?