How to resolve the algorithm Percentage difference between images step by step in the Nim programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Percentage difference between images step by step in the Nim 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 Nim programming language

Source code in the nim programming language

import strformat
import imageman

var img50, img100: Image[ColorRGBU]
try:
  img50 = loadImage[ColorRGBU]("Lenna50.jpg")
  img100 = loadImage[ColorRGBU]("Lenna100.jpg")
except IOError:
  echo getCurrentExceptionMsg()
  quit QuitFailure

let width = img50.width
let height = img50.height
if img100.width != width or img100.height != height:
  quit "Images have different sizes.", QuitFailure

var sum = 0
for x in 0..<height:
  for y in 0..<width:
    let color1 = img50[x, y]
    let color2 = img100[x, y]
    for i in 0..2:
      sum += abs(color1[i].int - color2[i].int)

echo &"Image difference: {sum * 100 / (width * height * 3 * 255):.4f} %"


  

You may also check:How to resolve the algorithm OpenGL step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Roots of unity step by step in the GAP programming language
You may also check:How to resolve the algorithm Knapsack problem/Unbounded step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the Oz programming language
You may also check:How to resolve the algorithm 100 doors step by step in the Ol programming language