Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a collection of the pixels that differ between two images #112

Open
MJB222398 opened this issue Sep 3, 2024 · 1 comment
Open

Comments

@MJB222398
Copy link
Contributor

For my use case I compare screenshots taken on Full HD screens (1920 x 1080). The number of pixels different between these can sometimes be very very few, maybe a couple of hundred or even less. I have even had a case where only one pixel differed (it differed only by 4 in the R value, G and B matched. That's not even a needle in a haystack haha. Calculating the difference image to show the differences does not really help with this when there are so few pixels different. I couldn't tell what was a differing pixel and what was just dust on my screen.

When this happens I end up having to investigate manually and debug ImageShare.Compare to find the coordinates of the pixel that differed which of course takes time and is not ideal. I wonder therefore if it would be a good idea to include a collection of all the pixels that differed in the ICompareResult? This might have to be limited to a certain number as the collection could be huge if the images are large and vastly different.

Maybe you have a different and better idea?

@stesee
Copy link
Collaborator

stesee commented Sep 6, 2024

Have a look at

public static Image CalcDiffMaskImage(Stream actualImage, Stream expectedImage, ResizeOption resizeOption = ResizeOption.DontResize, int pixelColorShiftTolerance = 0)

The resulting image is a two-dimensional representation of the difference. Pixels with a color that is not zero indicate differences between the actualImage and expectedImage of the call of ImageSharpCompare.CalcDiffMaskImage.

You could write some code like

   [TestCase(pngBlack2x2px, pngBlack4x4px, ResizeOption.Resize)]
        public void ShouldCalcPixelsThatDiffer(string pathPic1, string pathPic2,ResizeOption resizeOption)
        {
            var absolutePathPic1 = Path.Combine(AppContext.BaseDirectory, pathPic1);
            var absolutePathPic2 = Path.Combine(AppContext.BaseDirectory, pathPic2);

            using var absolutePic1 = Image.Load(absolutePathPic1);
            using var absolutePic2 = Image.Load(absolutePathPic2);

            using (var maskImage = (Image<SixLabors.ImageSharp.PixelFormats.Rgb24>)ImageSharpCompare.CalcDiffMaskImage(absolutePic1, absolutePic2, resizeOption))
            {

                 TestContext.WriteLine($"Comparing {pathPic1} and {pathPic2} with resize option {resizeOption}");
   
                for (var x = 0; x < maskImage.Width; x++)
                {
                    for (var y = 0; y < maskImage.Height; y++)
                    {
                        var maskImagePixel = maskImage[x, y];
                        if (maskImagePixel.R != 0 || maskImagePixel.G != 0 || maskImagePixel.B != 0)
                        {
                            Console.WriteLine($"x: {x}, y: {y}, R: {maskImagePixel.R}, G: {maskImagePixel.G}, B: {maskImagePixel.B}");
                        }
                    }
                }
            }
        }

Let me know if that satisfies your case!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants