How to resolve the algorithm Percentage difference between images step by step in the Tcl programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Percentage difference between images step by step in the Tcl 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 Tcl programming language
Source code in the tcl programming language
package require Tk
proc imageDifference {img1 img2} {
if {
[image width $img1] != [image width $img2] ||
[image height $img1] != [image height $img2]
} then {
return -code error "images are different size"
}
set diff 0
for {set x 0} {$x<[image width $img1]} {incr x} {
for {set y 0} {$y<[image height $img1]} {incr y} {
lassign [$img1 get $x $y] r1 g1 b1
lassign [$img2 get $x $y] r2 g2 b2
incr diff [expr {abs($r1-$r2)+abs($g1-$g2)+abs($b1-$b2)}]
}
}
expr {$diff/double($x*$y*3*255)}
}
# Package only used for JPEG loader
package require Img
image create photo lenna50 -file lenna50.jpg
image create photo lenna100 -file lenna100.jpg
puts "difference is [expr {[imageDifference lenna50 lenna100]*100.}]%"
exit ;# Need explicit exit here; don't want a GUI
You may also check:How to resolve the algorithm Levenshtein distance step by step in the Perl programming language
You may also check:How to resolve the algorithm Magic squares of odd order step by step in the Draco programming language
You may also check:How to resolve the algorithm Gray code step by step in the Groovy programming language
You may also check:How to resolve the algorithm Comments step by step in the Maxima programming language
You may also check:How to resolve the algorithm Formatted numeric output step by step in the AArch64 Assembly programming language