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

Published on 12 May 2024 09:40 PM

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

This C# code compares two images ("Lenna50.jpg" and "Lenna100.jpg") by calculating the average pixel difference between them and displaying the result as a percentage. Here's a detailed explanation of the code:

  1. Loading Images:

    • It starts by loading two images, "Lenna50.jpg" and "Lenna100.jpg," as Bitmap objects using the Bitmap constructor.
  2. Checking Image Sizes:

    • The code checks if the two images are of different sizes. If they are not the same size, it prints an error message and exits the program.
  3. Calculating Pixel Differences:

    • The code initializes a variable, diff, to 0.
    • It then iterates through both images, pixel by pixel, using nested loops for rows and columns.
    • For each pixel, it gets the colors of the corresponding pixels from both images using the GetPixel method.
    • It calculates the absolute difference between the red, green, and blue channels of the two pixels and accumulates these differences in the diff variable.
  4. Calculating Average Percentage Difference:

    • After iterating through all pixels, the code calculates the average percentage difference by dividing the total difference (diff) by the total number of pixels in the image (img1.Width * img1.Height * 3, where 3 represents the three channels: red, green, and blue).
    • The result is multiplied by 100 to express it as a percentage.
  5. Displaying the Result:

    • Finally, the code prints the average percentage difference to the console.

In summary, this code compares two images by calculating the average pixel difference between them and displays the result as a percentage, which indicates how similar or different the two images are.

Source code in the csharp programming language

using System;
using System.Drawing;

class Program
{
    static void Main()
    {
        Bitmap img1 = new Bitmap("Lenna50.jpg");
        Bitmap img2 = new Bitmap("Lenna100.jpg");

        if (img1.Size != img2.Size)
        {
            Console.Error.WriteLine("Images are of different sizes");
            return;
        }

        float diff = 0;

        for (int y = 0; y < img1.Height; y++)
        {
            for (int x = 0; x < img1.Width; x++)
            {
                Color pixel1 = img1.GetPixel(x, y);
                Color pixel2 = img2.GetPixel(x, y);

                diff += Math.Abs(pixel1.R - pixel2.R);
                diff += Math.Abs(pixel1.G - pixel2.G);
                diff += Math.Abs(pixel1.B - pixel2.B);
            }
        }

        Console.WriteLine("diff: {0} %", 100 * (diff / 255) / (img1.Width * img1.Height * 3));
    }
}


  

You may also check:How to resolve the algorithm Vigenère cipher step by step in the Racket programming language
You may also check:How to resolve the algorithm File modification time step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Exceptions/Catch an exception thrown in a nested call step by step in the C programming language
You may also check:How to resolve the algorithm Non-decimal radices/Output step by step in the Tcl programming language
You may also check:How to resolve the algorithm Euler's sum of powers conjecture step by step in the Ring programming language