Detect which image is sharper

后端 未结 5 1469
情话喂你
情话喂你 2020-12-07 18:56

I\'m looking for a way to detect which of two (similar) images is sharper.

I\'m thinking this could be using some measure of overall sharpness and generating a score

5条回答
  •  情书的邮戳
    2020-12-07 19:47

    Simple practical approach would be to use edge detection (more edges == sharper image).

    Quick and dirty hands-on using PHP GD

    function getBlurAmount($image) {
        $size = getimagesize($image);
        $image = imagecreatefromjpeg($image);
        imagefilter($image, IMG_FILTER_EDGEDETECT);    
        $blur = 0;
        for ($x = 0; $x < $size[0]; $x++) {
            for ($y = 0; $y < $size[1]; $y++) {
                $blur += imagecolorat($image, $x, $y) & 0xFF;
            }
        }
        return $blur;
    }
    
    $e1 = getBlurAmount('http://upload.wikimedia.org/wikipedia/commons/thumb/5/51/Jonquil_flowers_at_f32.jpg/800px-Jonquil_flowers_at_f32.jpg');
    $e2 = getBlurAmount('http://upload.wikimedia.org/wikipedia/commons/thumb/0/01/Jonquil_flowers_at_f5.jpg/800px-Jonquil_flowers_at_f5.jpg');
    
    echo "Relative blur amount: first image " . $e1 / min($e1, $e2) . ", second image " . $e2 / min($e1, $e2);
    

    (image with less blur is sharper) More efficient approach would be to detect edges in your code, using Sobel operator. PHP example (rewriting in C++ should give huge performance boost I guess).

提交回复
热议问题