Percentage difference between images: Difference between revisions
Content added Content deleted
Line 1,088: | Line 1,088: | ||
format "Diff: %\%\n" (totalDiff / ((img1.width * img1.height * 3) as float) * 100) |
format "Diff: %\%\n" (totalDiff / ((img1.width * img1.height * 3) as float) * 100) |
||
)</lang> |
)</lang> |
||
=={{header|Nim}}== |
|||
{{libheader|imageman}} |
|||
We use the procedure <code>loadImage</code> which sets default values for JPEG decoding parameters. With these parameters, the difference between the images is about 1.7747 %. Using the less convenient procedure <code>readImage</code> which works on an open file, it is possible to change the parameters in order to get the smaller difference of 1.6256 %. |
|||
<lang Nim>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} %"</lang> |
|||
{{out}} |
|||
<pre>Image difference: 1.7747 %</pre> |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |