C#旋转验证码识别

一,前言

这次的识别我们没有用到深度学习,是采用旋转图片对比相似度的方案。适用于图片较少的情况快速识别旋转验证码

旋转验证码识别的主要内容就是,先下载图片后旋转360°,然后每一度和打码后的图片进行对比相似度。 由于图片较少,或图片相似度高的情况下,使用该识别方式较为简单。

二,旋转图片

首先得到一张图片

旋转360°。

57301ff39bee2d6980f140d3351342dd.png

以下是旋转代码:

/// <summary>  
        /// 以逆时针为方向对图像进行旋转  
        /// </summary>  
        /// <param name="b">位图流</param>  
        /// <param name="angle">旋转角度[0,360](前台给的)</param>  
        /// <returns></returns>  
        public Image RotateImg(Image b, int angle)
        {
            angle = angle % 360;
            //弧度转换  
            double radian = angle * Math.PI / 180.0;
            double cos = Math.Cos(radian);
            double sin = Math.Sin(radian);
            //原图的宽和高  
            int w = b.Width;
            int h = b.Height;
            int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));
            int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));
            //目标位图  
            Bitmap dsImage = new Bitmap(W, H);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dsImage);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //计算偏移量  
            Point Offset = new Point((W - w) / 2, (H - h) / 2);
            //构造图像显示区域:让图像的中心与窗口的中心点一致  
            Rectangle rect = new Rectangle(Offset.X, Offset.Y, w, h);
            Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
            g.TranslateTransform(center.X, center.Y);
            g.RotateTransform(angle);
            //恢复图像在水平和垂直方向的平移  
            g.TranslateTransform(-center.X, -center.Y);
            g.DrawImage(b, rect);
            //重至绘图的所有变换  
            g.ResetTransform();
            g.Save();
            //保存旋转后的图片  
            dsImage.Save(System.IO.Directory.GetCurrentDirectory() + angle + ".jpg", System.Drawing.Imaging.ImageFormat.Png);
            g.Dispose();
            return dsImage;
        }

然后我们以原图,开始对比旋转后的图片:

三,缩小图片

为了保证破解率用原图对比是很不明智的,所以我们选择缩小下图片像素

  
private Image ReduceSize(int width = 48, int height = 48)
        {
            Image image = SourceImg.GetThumbnailImage(width, height, () => { return false; }, IntPtr.Zero);

            return image;
        }

四,对比相似度

然后开始对比像素点:

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Drawing;

namespace YZMpojie
{
    public  class Class2
    {
        Image SourceImg;

        //public SimilarPhoto(string filePath)
        //{
        //    SourceImg = Image.FromFile(filePath);
        //}

        //public SimilarPhoto(Stream stream)
        //{
        //    SourceImg = Image.FromStream(stream);
        //}

        public String GetHash(Image img)
        {
            SourceImg = img;
            Image image = ReduceSize();
            Byte[] grayValues = ReduceColor(image);
            Byte average = CalcAverage(grayValues);
            String reslut = ComputeBits(grayValues, average);
            return reslut;
        }

        public Image GetReduceSize(Image img, int width = 48, int height = 48)
        {
            Image image = img.GetThumbnailImage(width, height, () => { return false; }, IntPtr.Zero);

            return image;
        }

        // Step 1 : Reduce size to 8*8
        private Image ReduceSize(int width = 48, int height = 48)
        {
            Image image = SourceImg.GetThumbnailImage(width, height, () => { return false; }, IntPtr.Zero);

            return image;
        }

        // Step 2 : Reduce Color
        private Byte[] ReduceColor(Image image)
        {
            Bitmap bitMap = new Bitmap(image);
            Byte[] grayValues = new Byte[image.Width * image.Height];

            for (int x = 0; x < image.Width; x++)
                for (int y = 0; y < image.Height; y++)
                {
                    Color color = bitMap.GetPixel(x, y);
                    byte grayValue = (byte)((color.R * 30 + color.G * 59 + color.B * 11) / 100);
                    grayValues[x * image.Width + y] = grayValue;
                }
            return grayValues;
        }

        // Step 3 : Average the colors
        private Byte CalcAverage(byte[] values)
        {
            int sum = 0;
            for (int i = 0; i < values.Length; i++)
                sum += (int)values[i];
            return Convert.ToByte(sum / values.Length);
        }

        // Step 4 : Compute the bits
        private String ComputeBits(byte[] values, byte averageValue)
        {
            char[] result = new char[values.Length];
            for (int i = 0; i < values.Length; i++)
            {
                if (values[i] < averageValue)
                    result[i] = '0';
                else
                    result[i] = '1';
            }
            return new String(result);
        }

        // Compare hash
        public static Int32 CalcSimilarDegree(string a, string b)
        {
            if (a.Length != b.Length)
                throw new ArgumentException();
            int count = 0;
            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] != b[i])
                    count++;
            }
            return count;
        }


    }
}

然后计算相似度:

  
public List<bool> GetboolList(string str) 
        {
            List<bool> lstb = new List<bool>();

            for (int i = 0; i < str.Length; i++)
            {
                if (str.Substring(i, 1) == "0")
                {
                    lstb.Add(true);
                }
                else
                {
                    lstb.Add(false);
                }
            }
            return lstb;
        }

        
         List<bool> bla = GetboolList(图片a的字符串);
         List<bool> blb = GetboolList(图片b的字符串);
         int equalElements = bla.Zip(blb, (i, j) => i == j).Count(eq => eq);

我们缩小后的图片有 2304像素点,一般对比70-90%的像素点就足以识别旋转验证码了。

大体效果如下:

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值