How to resolve the algorithm Percentage difference between images step by step in the Rust programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Percentage difference between images step by step in the Rust programming language
Table of Contents
Problem Statement
Useful for comparing two JPEG images saved with a different compression ratios. You can use these pictures for testing (use the full-size version of each): The expected difference for these two images is 1.62125%
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Percentage difference between images step by step in the Rust programming language
Source code in the rust programming language
extern crate image;
use image::{GenericImageView, Rgba};
fn diff_rgba3(rgba1 : Rgba<u8>, rgba2 : Rgba<u8>) -> i32 {
(rgba1[0] as i32 - rgba2[0] as i32).abs()
+ (rgba1[1] as i32 - rgba2[1] as i32).abs()
+ (rgba1[2] as i32 - rgba2[2] as i32).abs()
}
fn main() {
let img1 = image::open("Lenna100.jpg").unwrap();
let img2 = image::open("Lenna50.jpg").unwrap();
let mut accum = 0;
let zipper = img1.pixels().zip(img2.pixels());
for (pixel1, pixel2) in zipper {
accum += diff_rgba3(pixel1.2, pixel2.2);
}
println!("Percent difference {}", accum as f64 * 100.0/ (255.0 * 3.0 * (img1.width() * img1.height()) as f64));
}
You may also check:How to resolve the algorithm Check input device is a terminal step by step in the Python programming language
You may also check:How to resolve the algorithm Two's complement step by step in the Nim programming language
You may also check:How to resolve the algorithm Hailstone sequence step by step in the RPL programming language
You may also check:How to resolve the algorithm Jump anywhere step by step in the SPL programming language
You may also check:How to resolve the algorithm ABC problem step by step in the ALGOL 68 programming language