PHP锐化图片

2 篇文章 0 订阅

简单验证码的识别步骤有:黑白、锐化、切分、建模。

说说如何锐化,我从网上找到一个C#版本的:

 

//hsb: 0与1之间的值   
         public static Bitmap BitmapTo1Bpp(Bitmap img,Double hsb)
            {
                int w = img.Width;
                int h = img.Height;
                Bitmap bmp = new Bitmap(w, h, PixelFormat.Format1bppIndexed);
                BitmapData data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);
                for (int y = 0; y < h; y++)
                {
                    byte[] scan = new byte[(w + 7) / 8];
                    for (int x = 0; x < w; x++)
                    {
                        Color c = img.GetPixel(x, y);
                        //Console.WriteLine(c.GetBrightness().ToString());
                        if (c.GetBrightness() >= hsb ) scan[x / 8] |= (byte)(0x80 >> (x % 8));
                    }
                    Marshal.Copy(scan, 0, (IntPtr)((int)data.Scan0 + data.Stride * y), scan.Length);
                }
                bmp.UnlockBits(data);
                return bmp;
            }

 网上有不少图片处理类,所谓的“锐化”都起不到真正的效果。对于jpg等已经压缩过的图片,放大后,是可以看到空白区域有很多杂点的。用下面的函数,就可以轻松搞定:

 

function imagelightnessat($img, $x, $y) {
    if(!is_resource($img)) {
        trigger_error("imagelightnessat(): supplied argument is not a valid "
            . "Image resource", E_USER_WARNING);
        return 0.0;
    }
    $c = @imagecolorat($img, $x, $y);
    if($c === false) return false;
    if(imageistruecolor($img))
    {
        $red = ($c >> 16) & 0xFF;
        $green = ($c >> 8) & 0xFF;
        $blue = $c & 0xFF;
    }
    else
    {
        $i = imagecolorsforindex($img, $c);
        $red = $i['red'];
        $green = $i['green'];
        $blue = $i['blue'];
    }    
    $m = min($red, $green, $blue);
    $n = max($red, $green, $blue);
    /* Because RGB isn't normalized in GD, we divide by 510 here.
     *  Lightness = (Max(RGB) + Min(RGB)) / 2
     * But that's assuming red, green, and blue are 0 through 1 inclusive.
     * Red, green, and blue are actually 0-255 (255 + 255 = 510).
     */
    $lightness = (double)(($m + $n) / 510.0);
    return($lightness);
}
 

上面的函数来自于php.net线上手册注释。

有了清晰的、无干扰点的图片,剩下的事情,就很简单了。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值