I am having trouble training the cascade classifier, opencv_traincascade
works for some round but then seems to hang in an endless loop. While trying to debug this problem, I came across the CvCascadeImageReader::NegReader::nextImg
method in the file apps/traincascade/imagestorage.cpp
(current source is on github), which is involved in the endless loop opencv_traincascade
seems to be stuck in. Slightly reindented, the code for this method is as follows:
bool CvCascadeImageReader::NegReader::nextImg()
{
Point _offset = Point(0,0);
size_t count = imgFilenames.size();
for (size_t i = 0; i < count; i++) {
src = imread(imgFilenames[last++], 0);
if (src.empty()) {
continue;
}
round += last / count;
round = round % (winSize.width * winSize.height);
last %= count;
_offset.x = std::min((int)round % winSize.width, src.cols - winSize.width);
_offset.y = std::min((int)round / winSize.width, src.rows - winSize.height);
if (!src.empty() && src.type() == CV_8UC1
&& _offset.x >= 0 && _offset.y >= 0) {
break;
}
}
if (src.empty()) {
return false; // no appropriate image
}
point = offset = _offset;
scale = max(((float)winSize.width + point.x) / ((float)src.cols),
((float)winSize.height + point.y) / ((float)src.rows));
Size sz((int)(scale*src.cols + 0.5F), (int)(scale*src.rows + 0.5F));
resize(src, img, sz);
return true;
}
My questions:
Is this function to return
false
eventually, or isfalse
only for errors and in normal operation alwaystrue
is returned? If the method is meant to eventually returnfalse
, how will this happen?Due to the
if (src.empty()) continue
statement near the start of the loop, I believe that the program can reach a state wherelast >= count
. Is this a bug? Maybelast %= count
should be added beforecontinue
? Or maybe the wholeif
statement can be removed?Why does the loop have an upper bound of
count
iterations? What is the role of the_offset.x >= 0 && _offset.y >= 0
condition near the end of the loop? (This seems to allow negative _offset,x for the last iteration in the loop?)- What is the role of the variable
scale
?
- What is the role of the variable
Any hints would be most welcome.