Ask Your Question

Revision history [back]

I modified the predict functions from LBPH part (the same code can be applied to eigenfaces and fisherfaces parts):

void LBPH::predict(InputArray _src, vector<int> &minClass, vector<double> &minDist, int maxN) const {
    if(_histograms.empty()) {
        // throw error if no data (or simply return -1?)
        String error_message = "This LBPH model is not computed yet. Did you call the train method?";
        CV_Error(Error::StsBadArg, error_message);
    }
    Mat src = _src.getMat();
    // get the spatial histogram from input image
    Mat lbp_image = elbp(src, _radius, _neighbors);
    Mat query = spatial_histogram(
            lbp_image, /* lbp_image */
            static_cast<int>(std::pow(2.0, static_cast<double>(_neighbors))), /* number of possible patterns */
            _grid_x, /* grid size x */
            _grid_y, /* grid size y */
            true /* normed histograms */);

    // find first maxN nearest neighbors
    // using priority for keeping maxN min distances and maxN min classes
    // remember to include queue header file
    priority_queue<double, vector<double>, greater<double>> minDist1;
    priority_queue<int, vector<int>, greater<int>> minClass1;

    for(size_t sampleIdx = 0; sampleIdx < _histograms.size(); sampleIdx++) {
        double dist = compareHist(_histograms[sampleIdx], query, HISTCMP_CHISQR_ALT);
        if(dist < _threshold)
        {
            if(minDist1.size() < maxN)
            {
                minDist1.push(dist);
                minClass1.push(_labels.at<int>((int) sampleIdx));
            }else if(dist < minDist1.top()) 
            {
                minDist1.pop();
                minClass1.pop();
                minDist1.push(dist);
                minClass1.push(_labels.at<int>((int) sampleIdx));
            }
        }
    }
    minDist = vector<double>(minDist1.size());
    minClass = vector<int>(minClass.size());
    // storing the results back to vector objects
    while(!minDist1.empty())
    {
        minDist.emplace_back(minDist1.top());
        minClass.emplace_back(minClass1.top());
        minDist1.pop();
        minClass1.pop();
    }
}

vector<int, double> LBPH::predict(InputArray _src, int maxN) const {
    vector<int> label;
    vector<double> weight;
    predict(_src, label, weight, maxN);
    vector<int, double> result(label.size());
    for(int i=0;i<label.size();i++)
        result[i] = make_pair(label[i], weight[i]);
    return result;
}

Hope this helps.

I modified the predict functions from LBPH part (the same code can be applied to eigenfaces and fisherfaces parts):

void LBPH::predict(InputArray _src, vector<int> &minClass, vector<double> &minDist, int maxN) const {
    if(_histograms.empty()) {
        // throw error if no data (or simply return -1?)
        String error_message = "This LBPH model is not computed yet. Did you call the train method?";
        CV_Error(Error::StsBadArg, error_message);
    }
    Mat src = _src.getMat();
    // get the spatial histogram from input image
    Mat lbp_image = elbp(src, _radius, _neighbors);
    Mat query = spatial_histogram(
            lbp_image, /* lbp_image */
            static_cast<int>(std::pow(2.0, static_cast<double>(_neighbors))), /* number of possible patterns */
            _grid_x, /* grid size x */
            _grid_y, /* grid size y */
            true /* normed histograms */);

    // find first maxN nearest neighbors
    // using priority for keeping maxN min distances and maxN min classes
    // remember to include queue header file
    priority_queue<double, vector<double>, greater<double>> minDist1;
    priority_queue<int, vector<int>, greater<int>> minClass1;

    for(size_t sampleIdx = 0; sampleIdx < _histograms.size(); sampleIdx++) {
        double dist = compareHist(_histograms[sampleIdx], query, HISTCMP_CHISQR_ALT);
        if(dist < _threshold)
        {
            if(minDist1.size() < maxN)
            {
                minDist1.push(dist);
                minClass1.push(_labels.at<int>((int) sampleIdx));
            }else if(dist < minDist1.top()) 
            {
                minDist1.pop();
                minClass1.pop();
                minDist1.push(dist);
                minClass1.push(_labels.at<int>((int) sampleIdx));
            }
        }
    }
    minDist = vector<double>(minDist1.size());
    minClass = vector<int>(minClass.size());
vector<int>(minClass1.size());
    // storing the results back to vector objects
    while(!minDist1.empty())
    {
        minDist.emplace_back(minDist1.top());
        minClass.emplace_back(minClass1.top());
        minDist1.pop();
        minClass1.pop();
    }
}

vector<int, double> LBPH::predict(InputArray _src, int maxN) const {
    vector<int> label;
    vector<double> weight;
    predict(_src, label, weight, maxN);
    vector<int, double> result(label.size());
    for(int i=0;i<label.size();i++)
        result[i] = make_pair(label[i], weight[i]);
    return result;
}

Hope this helps.

I modified the predict functions from LBPH part (the same code can be applied to eigenfaces and fisherfaces parts):

 void LBPH::predict(InputArray _src, vector<int> std::vector<int> &minClass, vector<double> std::vector<double> &minDist, int maxN) const {
    if(_histograms.empty()) if (_histograms.empty()) {
        // throw error if no data (or simply return -1?)
        String error_message = "This LBPH model is not computed yet. Did you call the train method?";
        CV_Error(Error::StsBadArg, error_message);
    }
    Mat src = _src.getMat();
    // get the spatial histogram from input image
    Mat lbp_image = elbp(src, _radius, _neighbors);
    Mat query = spatial_histogram(
         lbp_image, /* lbp_image */
         static_cast<int>(std::pow(2.0, static_cast<double>(_neighbors))), /* number of possible patterns */
         _grid_x, /* grid size x */
         _grid_y, /* grid size y */
         true /* normed histograms */);

    // find first maxN nearest neighbors
    // using priority for keeping maxN min distances and maxN min classes
    // remember to include queue header file
    priority_queue<double, vector<double>, greater<double>> file and algorithm for greater<double>
    std::priority_queue<double, std::vector<double>, std::greater<double>> minDist1;
    priority_queue<int, vector<int>, greater<int>> std::priority_queue<int, std::vector<int>, std::greater<int>> minClass1;

    for(size_t for (size_t sampleIdx = 0; sampleIdx < _histograms.size(); sampleIdx++) {
        double dist = compareHist(_histograms[sampleIdx], query, HISTCMP_CHISQR_ALT);
        if(dist if (dist < _threshold)
        {
            if(minDist1.size() if (minDist1.size() < maxN)
            {
                minDist1.push(dist);
                minClass1.push(_labels.at<int>((int) sampleIdx));
            }else if(dist minClass1.push(_labels.at<int>((int)sampleIdx));
            }
            else if (dist < minDist1.top()) 
minDist1.top())
            {
                minDist1.pop();
                minClass1.pop();
                minDist1.push(dist);
                minClass1.push(_labels.at<int>((int) sampleIdx));
minClass1.push(_labels.at<int>((int)sampleIdx));
            }
        }
    }
    minDist = vector<double>(minDist1.size());
std::vector<double>(minDist1.size());
    minClass = vector<int>(minClass1.size());
std::vector<int>(minClass1.size());
    // storing the results back to vector objects
    while(!minDist1.empty())
while (!minDist1.empty())
    {
        minDist.emplace_back(minDist1.top());
        minClass.emplace_back(minClass1.top());
        minDist1.pop();
        minClass1.pop();
    }
}

vector<int, double> std::vector<std::pair<int, double>> LBPH::predict(InputArray _src, int maxN) const {
    vector<int> std::vector<int> label;
    vector<double> std::vector<double> weight;
    predict(_src, label, weight, maxN);
    vector<int, double> std::vector<std::pair<int, double>> result(label.size());
    for(int i=0;i<label.size();i++)
for (int i = 0; i < label.size(); i++)
        result[i] = make_pair(label[i], std::make_pair(label[i], weight[i]);
    return result;
}

Hope this helps.