Ask Your Question

question about apps/traincascade/imagestorage.cpp

asked 2014-09-19 10:40:44 -0500

Jochen gravatar image

updated 2014-09-19 12:16:50 -0500

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()) {
        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) {

    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.

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2015-04-03 07:17:18 -0500

Gino Strato gravatar image

updated 2015-04-03 07:18:26 -0500

  1. Normally it returns true. For example it is needed by the line with code “if ( !nextImg() )”
  2. The program only reaches the state where last >= count if no valid image is loaded. Otherwise, it gets out of the loop and when it gets in the next time, everything is all right.

  3. Count is the number of negative images. The algorithm iterates through the whole sets of images, over different rounds (hence not only one time), until it collects all the samples it needs. _offset.x >= 0 && _offset.y >= 0 discards images which are smaller than the window itself.
    The role of scale is to shrink the image to the minimum size to allow it to be fitted by a sliding window.

edit flag offensive delete link more

Question Tools


Asked: 2014-09-19 10:40:44 -0500

Seen: 252 times

Last updated: Apr 03 '15