原创

php识别相似图片简易版

温馨提示:
本文最后更新于 2017年03月24日,已超过 2,589 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

这个识别图片的原理是分析像素点,计算平均颜色,大于平均颜色则为1,小于则为0,然后进行比对

精确度很低,只能匹配形状和比例一样的图片

class img
{
//比较图片相似度
    public function cpimg($img1, $img2, $rate = '2')
    {
        $data1 = $this->dataimg($img1);
        $data2 = $this->dataimg($img2);
        $than=$this->thanimg($data1,$data2);
        $rate=$than/(64*$rate*$rate);
        return $rate;
    }
//计算图片数据
    public function dataimg($image,$if_url=1,$rate = '2')
    {
        if($if_url) {
            $image = $this->creatimg($image);
        }
        $result = $this->imgdeflate($image);
        return $result;
    }

    /**
     * 打开一张图片
     */
    public function creatimg($image)
    {
        $img_info = getimagesize($image);
        switch ($img_info[2]) {
            case 1:
                $img = imagecreatefromgif($image);
                break;
            case 2:
                $img = imagecreatefromjpeg($image);
                break;
            case 3:
                $img = imagecreatefrompng($image);
                break;
        }
        return $img;
    }

    /**
     * $rate为图片长宽最大值
     */
    public function imgdeflate($image, $rate = '2')
    {
        $width = imagesx($image);
        $height = imagesy($image);
        $n_w = 8 * $rate;//新图片宽度
        $n_h = 8 * $rate;//新图片高度
        $new = imagecreatetruecolor($n_w, $n_h);//新建一张设定真彩色宽高的图
//取出一个png图形
//copy部分图像并调整
        imagecopyresized($new, $image, 0, 0, 0, 0, $n_w, $n_h, $width, $height);
//图像输出新图片、另存为
        imagefilter($new, IMG_FILTER_GRAYSCALE);//将图片转为64级灰度
//获取每个像素的灰度值
        $total = 0;
        $array = array();
        for ($y = 0; $y < $n_h; $y++) {
            for ($x = 0; $x < $n_w; $x++) {
                $gray = (imagecolorat($new, $x, $y) >> 8) & 0xFF;
                $array[$y] = array();
                $array[$y][$x] = $gray;
                $total += $gray;
                //echo $total.'<br>';
            }
        }//平均值计算
        //echo $total.'<br>';
        $average = intval($total / (64 * $rate * $rate));
        //echo $average."<br>";
        $total = 0;
        $result = '';
        $array = array();
        for ($y = 0; $y < $n_h; $y++) {
            for ($x = 0; $x < $n_w; $x++) {
                $gra = (imagecolorat($new, $x, $y) >> 8) & 0xFF;
                $array[$y][$x] = $gra;
                if ($gra >= $average) {
                    $result .= '1';
                } else {
                    $result .= '0';
                }
            }
        }
        return $result;
    }


//进行编辑距离数值比较
    public function thanimg($data1, $data2)
    {
        $dist = 0;
        $len1 = strlen($data1);
        $len2 = strlen($data2);

        if ($len1 == 0) {
            return $len2;
        }

        if ($len2 == 0) {
            return $len1;
        }

        for ($i = 0; $i <= $len1; $i++) {
            $matrix[$i][0] = 0;
        }

        for ($j = 0; $j <= $len2; $j++) {
            $matrix[0][$j] = 0;
        }

        for ($i = 1; $i <= $len1; $i++) {
            $ch1 = $data1[$i - 1];
            for ($j = 1; $j <= $len2; $j++) {
                $ch2 = $data2[$j - 1];
                $temp = $ch1 == $ch2 ? 0 : 1;
                $arr = array(
                    $matrix[$i - 1][$j] + 1,
                    $matrix[$i][$j - 1] + 1,
                    $matrix[$i - 1][$j - 1] + $temp
                );
                $matrix[$i][$j] = min($arr);
                $val=$matrix[$i][$j];
            }
        }
        return $val;
    }
    /*
     *
     *
     * 汉明距离
     */
    public function hamimg($data1, $data2){
        $len1 = strlen($data1);
        $len2 = strlen($data2);
        if($len1 != $len2)
        {
            return false;
        }

        $dist = 0;
        for($i = 0; $i < $len1; $i++)
        {
            if($data1[$i] != $data2[$i])
            {
                $dist++;
            }
        }
        return $dist;
    }
}
正文到此结束
本文目录