一,前言
这次的识别我们没有用到深度学习,是采用旋转图片对比相似度的方案。适用于图片较少的情况快速识别旋转验证码
旋转验证码识别的主要内容就是,先下载图片后旋转360°,然后每一度和打码后的图片进行对比相似度。 由于图片较少,或图片相似度高的情况下,使用该识别方式较为简单。
二,旋转图片
首先得到一张图片
旋转360°。
以下是旋转代码:
/// <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%的像素点就足以识别旋转验证码了。
大体效果如下: