Author Testimonials: Math is not good, encounter algorithmic problems minute dog belt, no transfer of space-_-| | |
Recent whim, looked at similar images of the search, the most most elementary method is to extract the Hamming codes, and then match the distance. Of course, in the hundreds of millions of Chinese plaintext, to sift out the required pictures, the calculation is too large to meet the production requirements. As a small math white, at this time completely did not fold. At this time accidentally see "dimensionality" A said, can be reduced to the dimension of the hash code into the database, in addition to the classification of the constraints, the basic to meet the small white taste of the e-commerce similar image search requirements:)
The following direct code:
classdfingerprint{Private $hImg=NULL; Private $hTargetImg=NULL; Private $arrResult=Array(); ConstPrint_width=8; ConstPrint_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::P rint_height,self::print_height); } } Public functionCalprint () {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); This sentence turns out problem, mantissa error, cause unknown $hexData=$this-Bin2Hex($binData); return Array( ' Bin ' =$binData, ' hex ' =$hexData ); } Public functionDecenddimension ()//two value reduced dimension { if(!$this->arrresult)return false; $binData= ' '; for($x= 0;$x<self::P rint_width;$x++) { $rtl=0; for($y= 0;$y<self::P rint_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 functionSizecompress ()//size Compression { 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 functionTogray ()//Grayscale { for($x= 0;$x<self::P rint_width;$x++) { for($y= 0;$y<self::P rint_height;$y++) { $color=imagecolorat ($this->htargetimg,$x,$y); $_red= ($color>>) & 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 functionBinaryzation ()//two value of { if(!$this->arrresult)return false; if(sizeof($this->arrresult)!=self::P rint_width)return false; $totalVal=0; $avgVal=0; for($x= 0;$x<self::P rint_width;$x++) { for($y= 0;$y<self::P rint_height;$y++) { $totalVal+=$this->arrresult[$x][$y]; } } $avgVal=$totalVal/(Self::P rint_height*self::print_height); //start of binary value $binData= ' '; for($x= 0;$x<self::P rint_width;$x++) { for($y= 0;$y<self::P rint_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 functionGetext ($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; }}
Call:
$oFingerPrint=new dfingerprint (' 1.jpg '); $arrHammingCode=$oFingerPrint-Calprint (); $arrDecendCode $ofingerprint->decenddimension ();
View information:
http://blog.csdn.net/lu597203933/article/details/45101859
http://blog.csdn.net/cshilin/article/details/52119682
The extraction and dimensionality reduction of the Hamming codes in PHP implementation