Crate image_compare

source ·
Expand description

§Comparing gray images using structure

This crate allows to compare grayscale images using either structure or histogramming methods. The easiest use is loading two images, converting them to grayscale and running a comparison:

use image_compare::Algorithm;
let image_one = image::open("image1.png").expect("Could not find test-image").into_luma8();
let image_two = image::open("image2.png").expect("Could not find test-image").into_luma8();
let result = image_compare::gray_similarity_structure(&Algorithm::MSSIMSimple, &image_one, &image_two).expect("Images had different dimensions");

Check the Algorithm enum for implementation details

§Comparing gray images using histogram

Histogram comparisons are possible using the histogram comparison function

use image_compare::Metric;
let image_one = image::open("image1.png").expect("Could not find test-image").into_luma8();
let image_two = image::open("image2.png").expect("Could not find test-image").into_luma8();
let result = image_compare::gray_similarity_histogram(Metric::Hellinger, &image_one, &image_two).expect("Images had different dimensions");

Check the Metric enum for implementation details

§Comparing rgb images using hybrid mode

hybrid mode allows to decompose the image to structure and color channels (YUV) which are compared separately but then combined into a common result.

§Direct usage on two RGB8 images

let image_one = image::open("image1.png").expect("Could not find test-image").into_rgb8();
let image_two = image::open("image2.png").expect("Could not find test-image").into_rgb8();
let result = image_compare::rgb_hybrid_compare(&image_one, &image_two).expect("Images had different dimensions");

§Compare the similarity of two maybe-rgba images in front a given background color

If an image is RGBA it will be blended with a background of the given color. RGB images will not be modified.

use image::Rgb;
let image_one = image::open("image1.png").expect("Could not find test-image").into_rgba8();
let image_two = image::open("image2.png").expect("Could not find test-image").into_rgb8();
let white = Rgb([255,255,255]);
let result = image_compare::rgba_blended_hybrid_compare((&image_one).into(), (&image_two).into(), white).expect("Images had different dimensions");

§Comparing two RGBA8 images using hybrid mode

hybrid mode allows to decompose the image to structure, color and alpha channels (YUVA) which are compared separately but then combined into a common result.

let image_one = image::open("image1.png").expect("Could not find test-image").into_rgba8();
let image_two = image::open("image2.png").expect("Could not find test-image").into_rgba8();
let result = image_compare::rgba_hybrid_compare(&image_one, &image_two).expect("Images had different dimensions");

§Using structure results

All structural comparisons return a result struct that contains the similarity score. For the score 1.0 is perfectly similar, 0.0 is dissimilar and some algorithms even provide up to -1.0 for inverse. Furthermore, the algorithm may produce a similarity map (MSSIM, RMS and hybrid compare do) that can be evaluated per pixel or converted to a visualization:

let image_one = image::open("image1.png").expect("Could not find test-image").into_rgba8();
let image_two = image::open("image2.png").expect("Could not find test-image").into_rgba8();
let result = image_compare::rgba_hybrid_compare(&image_one, &image_two).expect("Images had different dimensions");
if result.score < 0.95 {
  let diff_img = result.image.to_color_map();
  diff_img.save("diff_image.png").expect("Could not save diff image");
}

Structs§

  • the resulting struct containing both an image of per pixel diffs as well as an average score

Enums§

  • The enum for selecting a grayscale comparison implementation
  • A wrapper class accepting both RgbaImage and RgbImage for the blended hybrid comparison
  • The errors that can occur during comparison of the images
  • The distance metric choices for histogram comparisons

Functions§

  • Comparing gray images using histogram
  • Comparing gray images using structure.
  • Comparing structure via MSSIM on Y channel, comparing color-diff-vectors on U and V summing the squares Please mind that the RGBSimilarity-Image does not contain plain RGB here
  • Comparing rgb images using structure. RGB structure similarity is performed by doing a channel split and taking the maximum deviation (minimum similarity) for the result. The image contains the complete deviations.
  • This processes the RGBA images be pre-blending the colors with the desired background color. It’s faster then the full RGBA similarity and more intuitive.
  • Hybrid comparison for RGBA images. Will do MSSIM on luma, then RMS on U and V and alpha channels. The calculation of the score is then pixel-wise the minimum of each pixels similarity. To account for perceived indifference in lower alpha regions, this down-weights the difference linearly with mean alpha channel.