phpgd

PHP .png Lineart Comparison


For a couple days now, I've been stuck on a task involving image comparison using PHP. I'm beginning to think that PHP isn't the language to do this sort of task.

The Objective:

Use a base, compare a new line-art to it, and return the percentage difference between them. I've tried a couple ways of going about this: Compare the differences in pixel count (which doesn't really solve the goal of line-art difference), make all pixels that overlap, white, and find pixels without transparency/the color white and divide by non-transparent pixels in the base (which doesn't seem to get the correct number)

Here's an example:

Baseenter image description here

-- -- -- -- -- -- -- -- Base -- -- -- -- -- -- -- -- -- -- -- Line-Art -- -- -- -- -- -- --

The percentage difference should be around 54%. However, none of the ways I've tried get close (~5%) to this.

My question is: Can/How can this be done in PHP? Thanks!

Images © Aywas.com


Solution

  • You might try using the ImageMagick function compareimagechannels(), using only the black channel?

    Edit: Here's a basic attempt, and its output. You might be able to manipulate the output numbers to figure out your thresholds. Images 1 and 2 are the line art and image 3 is the Google header logo resized to 200x200.

    $img1 = new Imagick('image1.png');
    $img2 = new Imagick('image2.png');
    $img3 = new Imagick('image3.png');
    
    $diff12 = $img1->compareImageChannels($img2,
                 Imagick::CHANNEL_ALL, Imagick::METRIC_MEANABSOLUTEERROR);
    $diff13 = $img1->compareImageChannels($img3,
                 Imagick::CHANNEL_ALL, Imagick::METRIC_MEANABSOLUTEERROR);
    
    print_r($diff12);
    print_r($diff13);
    

    Output:

    Array
    (
        [0] => Imagick Object
            (
            )
    
        [1] => 1512.25385625
    )
    Array
    (
        [0] => Imagick Object
            (
            )
    
        [1] => 24353.6380375
    )