PHP實現圖片的漢明碼提取與降維

來源:互聯網
上載者:User

標籤:gray   jpg   csdn   tail   als   net   for   base   exp   

作者感言:數學不好,遇到演算法問題分分鐘狗帶,毫無轉寰的餘地-_-|||

最近心血來潮,看了相似圖片的搜尋,最最最初級的方法即提取漢明碼,之後匹配漢明距離。當然,在數以億計的漢明碼中,要篩出需要的圖片,計算量太大了,滿足不了生產要求。作為數學小白,此時完全沒折了。這時不小心看到“降維”一說,可以把降維後的雜湊碼存入資料庫,加之分類的約束,基本滿足小白初嘗電商相似圖片搜尋的要求了:)

以下直接貼代碼吧:

class DFingerPrint{    private $hImg=null;    private $hTargetImg=null;    private $arrResult=array();        const PRINT_WIDTH=8;    const PRINT_HEIGHT=8;        public function __construct($imgPath)    {        if(file_exists($imgPath))        {            $ext=$this->GetExt($imgPath);            $create_fun=‘‘;            switch($ext)            {                case ‘jpg‘:                case ‘jpeg‘:                    $create_fun=‘imagecreatefromjpeg‘;                    break;                case ‘gif‘:                    $create_fun=‘imagecreatefromgif‘;                    break;                case ‘png‘:                    $create_fun=‘imagecreatefrompng‘;                    break;                case ‘bmp‘:                    $create_fun=‘imagecreatefrombmp‘;                    break;                default:                    $create_fun=‘imagecreatefromgd2‘;                    break;            }                        $this->hImg=$create_fun($imgPath);            if($this->hImg===false) $this->hImg=null;        }                if($this->hImg)        {            $this->hTargetImg=imagecreatetruecolor(self::PRINT_HEIGHT,self::PRINT_HEIGHT);        }    }        public function CalPrint()    {        if(!$this->hImg) return false;        if(!$this->SizeCompress()) return false;        if(!$this->ToGray()) return false;        if(!($binData=$this->Binaryzation())) return false;                //$hexData=base_convert($binData,2,16);    //這一句轉出來問題,尾數錯誤,原因未知        $hexData=$this->Bin2Hex($binData);                return array(            ‘bin‘=>$binData,            ‘hex‘=>$hexData        );    }        public function DecendDimension()    //二值降維    {        if(!$this->arrResult) return false;                $binData=‘‘;        for($x=0;$x<self::PRINT_WIDTH;$x++)        {            $rtl=0;            for($y=0;$y<self::PRINT_HEIGHT;$y++)            {                $rtl+=$this->arrResult[$x][$y]==1?1:-1;            }            $binData.=$rtl>0?‘1‘:‘0‘;        }                $hexData=$this->Bin2Hex($binData);                return array(            ‘bin‘=>$binData,            ‘hex‘=>$hexData        );    }        private function SizeCompress()    //尺寸壓縮    {        if(!$this->hImg) return false;                imagecopyresized($this->hTargetImg,$this->hImg,0,0,0,0,8,8,imagesx($this->hImg),imagesy($this->hImg));        return true;    }        private function ToGray()    //灰階化    {        for($x=0;$x<self::PRINT_WIDTH;$x++)        {            for($y=0;$y<self::PRINT_HEIGHT;$y++)            {                $color=imagecolorat($this->hTargetImg,$x,$y);                $_red = ($color >> 16) & 0xff;                  $_green = ($color >> 8) & 0xff;                  $_blue = ($color) & 0xff;                                $newColor= intval(0.3 * $_red + 0.59 * $_green + 0.11 * $_blue);                                 $this->arrResult[$x][$y]=$newColor;            }        }        return true;    }        private function Binaryzation()    //二值化    {        if(!$this->arrResult) return false;        if(sizeof($this->arrResult)!=self::PRINT_WIDTH) return false;                $totalVal=0;        $avgVal=0;        for($x=0;$x<self::PRINT_WIDTH;$x++)        {            for($y=0;$y<self::PRINT_HEIGHT;$y++)            {                $totalVal+=$this->arrResult[$x][$y];            }        }        $avgVal=$totalVal/(self::PRINT_HEIGHT*self::PRINT_HEIGHT);                //開始二值化        $binData=‘‘;        for($x=0;$x<self::PRINT_WIDTH;$x++)        {            for($y=0;$y<self::PRINT_HEIGHT;$y++)            {                if($this->arrResult[$x][$y]>=$avgVal) $this->arrResult[$x][$y]=1;                else $this->arrResult[$x][$y]=0;                                //imagesetpixel($this->hTargetImg,$x,$y,$this->arrResult[$x][$y]==1?0xffffff:0x00);                $binData.=strval($this->arrResult[$x][$y]);            }        }                return $binData;    }        public function __destruct()    {        if($this->hImg)        {            imagedestroy($this->hImg);            $this->hImg=null;        }        if($this->hTargetImg)        {            imagedestroy($this->hTargetImg);            $this->hTargetImg=null;        }            }        private function GetExt($path)    {        $arr=explode(‘.‘,$path);        return strtolower($arr[sizeof($arr)-1]);    }        private function Bin2Hex($bin)    {        $hex=‘‘;        $i=1;        while($bin)        {            $tmp_bin=substr($bin,-4,4);            $hex=base_convert($tmp_bin,2,16).$hex;                        $bin=substr($bin,0,strlen($bin)-4);        }        return $hex;    }}

調用:

$oFingerPrint=new DFingerPrint(‘1.jpg‘);$arrHammingCode=$oFingerPrint->CalPrint();$arrDecendCode$oFingerPrint->DecendDimension();

 

查看資料:

http://blog.csdn.net/lu597203933/article/details/45101859

http://blog.csdn.net/cshilin/article/details/52119682

PHP實現圖片的漢明碼提取與降維

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.