在处理一个项目的时候遇到这种滑块验证,类似的应该有不少,比如其它形状的,这种类型相比较恶心的视觉旋转验证其实还是很简单。可以用OpenCv这个库去解析,此库的强大毋庸置疑,本屌依然用c#来撸了一遍
项目引入OpenCvSharp库(我用的是OpenCvSharp4.4.5.2.20210404版本,同时需要引用OpenCvSharp4.runtime.win.4.5.2.20210404)
[C#] 纯文本查看 复制代码 private static System.Drawing.Point FindPicFromImage(Bitmap imgSrc, Bitmap imgSub, double threshold = 0.9)
{
OpenCvSharp.Mat srcMat = null;
OpenCvSharp.Mat dstMat = null;
OpenCvSharp.OutputArray outArray = null;
try
{
srcMat = imgSrc.ToMat();
dstMat = imgSub.ToMat();
outArray = OpenCvSharp.OutputArray.Create(srcMat);
OpenCvSharp.Cv2.MatchTemplate(srcMat, dstMat, outArray, TemplateMatchModes.CCoeffNormed);
double minValue, maxValue;
OpenCvSharp.Point location, point;
OpenCvSharp.Cv2.MinMaxLoc(OpenCvSharp.InputArray.Create(outArray.GetMat()), out minValue, out maxValue, out location, out point);
if (maxValue >= threshold)
return new System.Drawing.Point(point.X, point.Y);
return System.Drawing.Point.Empty;
}
catch (Exception ex)
{
return System.Drawing.Point.Empty;
}
finally
{
if (srcMat != null)
srcMat.Dispose();
if (dstMat != null)
dstMat.Dispose();
if (outArray != null)
outArray.Dispose();
}
}
imgSrc是大图,imgSub是小图,获取位置距离即可。
|