-1

i'm kinda new here and i have this project where i need to compare 2 images together (i am using php and mysql for this project) and show the difference between them on pixel based , meaning that if there is a slight difference on the image i need to know where is it

i know that we can use the normal imagecolorat option in php but this one only shows the difference in colors and there is no way for it to show the difference in coloring on the new compared image , like the below example:

`

$im = imagecreatefrompng("php.png");
$rgb = imagecolorat($im, 10, 15);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;

var_dump($r, $g, $b);

for a more complex solutions there is Imagick and it is better showing the percentage of change in pixels but still i have no clue how to make it show the difference in the pixels on the new compared image , like the below example

$image1 = new imagick("image1.png");
$image2 = new imagick("image2.png");

$result = $image1->compareImages($image2, Imagick::METRIC_MEANSQUAREERROR);
$result[0]->setImageFormat("png");

header("Content-Type: image/png");
echo $result[0];

anyone can help on this is much appreciated , and please dont be critical and judgmental

5
  • 1
    You could use Imagick::compositeImage() and subtract one image from the other with imagick::COMPOSITE_MINUS. You should be left with an empty image if the two images are exactly the same. Any difference should show up. I've never done this with Imagick, that's why this is a comment, not an answer, but I think it should work. Is this what you wanted? Commented Jan 15 at 22:25
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Jan 16 at 10:50
  • @KIKOSoftware its close let me try it and get back to you Commented Jan 16 at 11:20
  • @Community so here are the details , i have a database of over 100 images of a textile standard that i will need in my production line in my factory of textiles , i will install several cameras that monitores the outputs of the machines and these cameras will feed my web based system which will take the images from the cameras and compare them to the standard saved on my database and if there is a difference in color or a tear this needs to be highlighted , what do you think Commented Jan 16 at 16:19
  • 1
    First of all, please please don't add this information in a comment, put it in the question so people will see it and read it. You can edit your question. Secondly, this changes everything. You're talking about complex pattern recognition here. You definitely cannot just compare pixels. This can only be done by godlike-tier programmers and it will take time, or you need to come across a package that does this for you. The only real advantage you have is that this is about textiles, meaning a regular pattern, and you take the photos yourself. Commented Jan 16 at 16:55

1 Answer 1

0

To compare two images pixel-by-pixel and highlight the differences between them using PHP you can use Imagick (as you've mentioned) or GD library.

I think Imagick it's better because more sophisticated for this commitment.

So let's talk about Imagick (if you already use it, you have it installed).

Check out the following code that not only highlights the differences, but also returns the resulting image with the differences marked that works with images you create and want to compare from your MySql database of textile standard (the operation is explained in the comments)

<?php
$image1 = new Imagick("image1.png");
$image2 = new Imagick("image2.png");

// it uses the Mean Squared Error (MSE) metric
$result = $image1->compareImages($image2, Imagick::METRIC_MEANSQUAREERROR);

$diffImage = $result[0]; // This contains the image with differences

/*
You can also adjust the threshold to only show significant differences.
For example, you can adjust the color or opacity of differences here.
*/

// Set the color to red for differences
$diffImage->setImageBackgroundColor(new ImagickPixel('red'));

// Manipulating further the transparency
$diffImage->setImageOpacity(0.5);

header("Content-Type: image/png");
echo $diffImage;

As @KIKO Software wrote you can use also Imagick::compositeImage() with Imagick::COMPOSITE_MINUS like this

<?php
$image1 = new Imagick("image1.png");
$image2 = new Imagick("image2.png");

// Ensure both images are the same size (if not, you may need to resize them)
$image2->resizeImage($image1->getImageWidth(), $image1->getImageHeight(), Imagick::FILTER_LANCZOS, 1);

// Create a copy of the first image for modification
$diffImage = clone $image1;

// Subtract the second image from the first image using COMPOSITE_MINUS
$diffImage->compositeImage($image2, Imagick::COMPOSITE_MINUS, 0, 0);

// Applying a colorize effect (red) to highlight the difference
$diffImage->setImageChannelColorValue(Imagick::COLOR_RED, 1);

header("Content-Type: image/png");
echo $diffImage;

Choose the method you want to use.

The second one seems the smartest to me.

Now your production line in your factory of textiles with your several cameras that monitores the outputs of the machine it will let you know what the differences are between the images taken (absurd, I didn't think the textile industry had come this far 🫣).

In any case, Good work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.