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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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;
    }
}

相关评论(0)
您是不是忘了说点什么?

友情提示:垃圾评论一律封号...

还没有评论,快来抢沙发吧!