Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

question about apps/traincascade/imagestorage.cpp

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:

  1. Is this function to return false eventually, or is false only for errors and in normal operation always true is returned? If the method is meant to eventually return false, how will this happen?

  2. Due to the if (src.empty()) continue statement near the start of the loop, I believe that the program can reach a state where last >= count. Is this a bug? Maybe last %= count should be added before continue? Or maybe the whole if statement can be removed?

  3. 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?)

    1. What is the role of the variable scale?

Any hints would be most welcome.

question about apps/traincascade/imagestorage.cpp

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:

  1. Is this function meant to return false eventually, or is false only for errors and in normal operation always true is returned? If the method is meant to eventually return false, how will this happen?

  2. Due to the if (src.empty()) continue statement near the start of the loop, I believe that the program can reach a state where last >= count. Is this a bug? Maybe last %= count should be added before continue? Or maybe the whole if statement can be removed?

  3. 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?)

    1. What is the role of the variable scale?

Any hints would be most welcome.