Ask Your Question

Revision history [back]

The same two images have different histogram correlation when compared in different machines

I asked this problem is stackoverflow today, but no one answer it. I copied my question description here.

I met with a very weird problem about OpenCV. I use the same codes to calculate the correlation between two histograms, but get different results in two machines.

My local machine is macOS, and the version of OpenCV is 2.4.13.2. The OpenCV version is 2.4.13 on my server, and the OS is CentOS, compiler is gcc4.8.2.

I write and test my codes locally, then push the codes to git repo, and pull this repo down to the server. After compilation, I input the exact same two images as on my mac (I have tested them by using diff), but get different results. I list the similarities below (they are of very slight difference ):

MacOS: 0.991395 0.998581 0.956077 0.959144 0.972919 0.856964 0.969325 0.867045 0.926774 0.975421 0.992405 0.994396

Server: 0.991386 0.998579 0.955386 0.958642 0.973364 0.856524 0.972811 0.867602 0.926943 0.977988 0.993043 0.994663

The detailed procedure of my code:

First, I cut the images into 12 grids (4 rows, 3 columns), and then calculate the histograms of every sub image. The I use CV_COMP_CORREL to measure the similarity of two corresponding sub histograms.

The calculation of sub histograms

    void void Hist::calculate() {
    assert(!_image.empty());  // not empty
    _hist.clear();  // vector<MatND> _hist;

    int roi_w = _image.size().width / _grid.col;  // _grid.col == 3
    int roi_h = _image.size().height / _grid.row;  // _gird.row == 4
    int channels[] = {0, 1, 2};
    Rect roi = Rect(0, 0, roi_w, roi_h);
    Mat sub_hsv_img = Mat();
    MatND temp_hist = MatND();

    for (int r = 0; r < _grid.row; r++) {  // row
        roi.y = r * roi.height;
        for (int c = 0; c < _grid.col; c++) {  // col
            roi.x = c * roi.width;
            cvtColor(_image(roi), sub_hsv_img, CV_BGR2HSV);
            temp_hist.release();
            calcHist(&sub_hsv_img, 1, channels, Mat(), temp_hist, \
                    3, _hist_size, _hist_range, true, false);
            _hist.push_back(temp_hist);
        }
    }

    sub_hsv_img.release();
}

The comparison of two images:

float compare(Hist h0, Hist h1, int mode) {
    int lng = ROW * COL;  // ROW == 4, COL == 3
    vector<float> diff;
    float correl = 0.0;
    for (int i = 0; i < lng; i++) {
        correl = cv::compareHist(h0[i], h1[i], mode);
        diff.push_back(correl);
    }

    for (int i = 0; i < 12; i++) {
        cout << diff[i] << " ";
    }
    cout << std::endl;

    sort(diff.begin(), diff.end());

    float sum = 0.0;
    for (int i = BEGIN; i < END; i++) {
        sum += diff[i];
    }

    return sum / (END - BEGIN);
}

could someone help to point out the possible cautions?