【问题标题】:OpenCV different results on Windows and LinuxOpenCV 在 Windows 和 Linux 上的不同结果
【发布时间】:2018-07-06 06:12:12
【问题描述】:

我正在为 rust 上的 OpenCV 编写跨平台包装器。我写了几个测试来检查我的包装器是否正常工作。有些测试通过了,有些测试失败了,值略有不同,但一个测试的结果完全不同。

正确的数字是我在 windows 上得到的,左 - 在 linux 上

---- compare_hist_chi_square stdout ----
    thread 'compare_hist_chi_square' panicked at '2697.981816285168 == 1360.7', tests/test_imgproc.rs:81:4
---- compare_hist_bhattacharyya stdout ----
    thread 'compare_hist_bhattacharyya' panicked at '0.6798259690477988 == 0.6679', tests/test_imgproc.rs:81:4
---- compare_hist_chi_square_alternative stdout ----
    thread 'compare_hist_chi_square_alternative' panicked at '41.65613074156445 == 41.027', tests/test_imgproc.rs:81:4
---- compare_hist_correlation stdout ----
    thread 'compare_hist_correlation' panicked at '0.20456957916644988 == 0.211', tests/test_imgproc.rs:81:4
---- compare_hist_intersection stdout ----
    thread 'compare_hist_intersection' panicked at '5.440850785933435 == 5.682', tests/test_imgproc.rs:81:4
---- compare_hist_kullback_leibler_divergence stdout ----
    thread 'compare_hist_kullback_leibler_divergence' panicked at '55.71912075710992 == 54.06287', tests/test_imgproc.rs:81:4

我试图从this article 复制代码。我拍了同样的照片,想要得到同样的结果。然而,这并没有发生。

这是一些测试的 Rust 代码(仅供参考):

extern crate cv;
extern crate float_cmp;
mod utils;

use cv::*;
use cv::imgproc::*;
use float_cmp::ApproxEqRatio;
use utils::*;

#[test]
#[should_panic]
fn compare_hist_different_dimensions_panic() {
    let first_image = load_unchanged("assets/Histogram_Comparison_Source_0.jpg");
    let second_image = load_unchanged("assets/Histogram_Comparison_Source_1.jpg");
    let _ = first_image.compare_hist(&second_image, HistogramComparisionMethod::Corellation).unwrap();
}

#[test]
fn compare_hist_correlation() {
    compare_hist(HistogramComparisionMethod::Corellation, 0.211);
}

#[test]
fn compare_hist_chi_square() {
    compare_hist(HistogramComparisionMethod::ChiSquare, 1360.7);
}

#[test]
fn compare_hist_intersection() {
    compare_hist(HistogramComparisionMethod::Intersection, 5.682);
}

#[test]
fn compare_hist_bhattacharyya() {
    compare_hist(HistogramComparisionMethod::Bhattacharyya, 0.6679);
}

#[test]
fn compare_hist_chi_square_alternative() {
    compare_hist(HistogramComparisionMethod::ChiSquareAlternative, 41.027);
}

#[test]
fn compare_hist_kullback_leibler_divergence() {
    compare_hist(
        HistogramComparisionMethod::KullbackLeiblerDivergence,
        54.06287,
    );
}

fn compare_hist(method: HistogramComparisionMethod, expected_result: f64) {
    let first_image = get_image_histogram("assets/Histogram_Comparison_Source_0.jpg");
    let second_image = get_image_histogram("assets/Histogram_Comparison_Source_1.jpg");
    let result = first_image.compare_hist(&second_image, method).unwrap();
    assert_eq(result, expected_result);
}

fn get_image_histogram(path: &'static str) -> Mat {
    let image = load_unchanged(path);
    let image = image.cvt_color(ColorConversionCodes::BGR2HSV);
    let hsize = [50, 60];
    let h_ranges = [0_f32, 180_f32];
    let s_ranges = [0_f32, 256_f32];
    let ranges = [
        h_ranges.as_ptr() as *const f32,
        s_ranges.as_ptr() as *const f32,
    ];
    let channels = [0, 1];
    let image = image.calc_hist(
        channels.as_ptr(),
        Mat::new(),
        2,
        hsize.as_ptr(),
        ranges.as_ptr(),
    );
    let image = image.normalize(0_f64, 1_f64, NormTypes::NormMinMax);
    image
}

fn assert_eq(a: f64, b: f64) {
    assert!(a.approx_eq_ratio(&b, 0.001), format!("{} == {}", a, b));
}

pub fn load_unchanged<P: AsRef<Path>>(img: P) -> Mat {
    let buf = load_image_as_buf(img);
    Mat::imdecode(&buf, ImreadModes::ImreadUnchanged)
}

fn load_image_as_buf<P: AsRef<Path>>(img: P) -> Vec<u8> {
    let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    d.push(img);
    let mut buf = Vec::new();
    File::open(d).unwrap().read_to_end(&mut buf).unwrap();
    buf
}

可能是因为jpg和jpg不同平台的读者不同,但不能解释2697.98和1360.7,相差近2倍!

这里可能有什么问题? Here is my entire pull request 我正在尝试添加此功能(小心,包含一些 Rust 代码)

【问题讨论】:

  • 把测试文件转成png怎么样?您使用参考代码得到相同的结果吗?
  • @VTT 我目前正在尝试将unchanged 读取模式更改为color,它更接近原始测试。然后我将按照您的建议将其转换为 png 或像 bmp 这样的无损格式。
  • png也是无损的,基本上是gzip压缩的位图。
  • @VTT 哇,我不知道。好的,我去看看。
  • @VTT 这很有趣,但使用png 我在 Windows 上得到非常接近的结果:Linux 上的 2902 与 2697。但是,仍然存在差异。也许是因为 linux 端的jpg。要去 CI 上检查一下,抱歉,构建需要大约 1 小时。 BRB

标签: c++ linux windows opencv cross-platform


【解决方案1】:

感谢@VTT,这个问题的发生是因为不同平台上的jpg 解释不同。切换到png即可解决问题

【讨论】:

    猜你喜欢
    • 2021-07-16
    • 2015-05-04
    • 1970-01-01
    • 2018-01-10
    • 2013-05-20
    • 2020-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多