|
| 1 | +using OpenCvSharp; |
| 2 | +using OpenCvSharp.Extensions; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Drawing; |
| 6 | +using System.Drawing.Imaging; |
| 7 | +using System.Linq; |
| 8 | +using System.Runtime.InteropServices; |
| 9 | +using System.Text; |
| 10 | +using System.Threading.Tasks; |
| 11 | + |
| 12 | +namespace GenshinAutoFish.Core |
| 13 | +{ |
| 14 | + class ImageRecognition |
| 15 | + { |
| 16 | + public static Bitmap GetRect(Bitmap img, out List<Rect> rects, bool enableImShow) |
| 17 | + { |
| 18 | + using (Mat mask = new Mat()) |
| 19 | + using (Mat rgbMat = new Mat()) |
| 20 | + using (Mat src = img.ToMat()) |
| 21 | + { |
| 22 | + Cv2.CvtColor(src, rgbMat, ColorConversionCodes.BGR2RGB); |
| 23 | + var lowPurple = new Scalar(255, 255, 192); |
| 24 | + var highPurple = new Scalar(255, 255, 192); |
| 25 | + Cv2.InRange(rgbMat, lowPurple, highPurple, mask); |
| 26 | + Cv2.Threshold(mask, mask, 0, 255, ThresholdTypes.Binary); //二值化 |
| 27 | + |
| 28 | + OpenCvSharp.Point[][] contours; |
| 29 | + HierarchyIndex[] hierarchy; |
| 30 | + Cv2.FindContours(mask, out contours, out hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxSimple, null); |
| 31 | + if (contours.Length > 0) |
| 32 | + { |
| 33 | + var imgTar = src.Clone(); |
| 34 | + var boxes = contours.Select(Cv2.BoundingRect).Where(w => w.Height >= 10); |
| 35 | + rects = boxes.ToList(); |
| 36 | + foreach (Rect rect in rects) |
| 37 | + { |
| 38 | + Cv2.Rectangle(imgTar, new OpenCvSharp.Point(rect.X, rect.Y), new OpenCvSharp.Point(rect.X + rect.Width, rect.Y + rect.Height), Scalar.Red, 2); |
| 39 | + } |
| 40 | + if (enableImShow) |
| 41 | + { |
| 42 | + Cv2.ImShow("钓鱼条识别窗口", imgTar); |
| 43 | + } |
| 44 | + return imgTar.ToBitmap(); |
| 45 | + } |
| 46 | + else |
| 47 | + { |
| 48 | + rects = null; |
| 49 | + return src.ToBitmap(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + public static Rect MatchWords(Bitmap img, ImageCapture capture, bool enableImShow) |
| 56 | + { |
| 57 | + using (Mat src = img.ToMat()) |
| 58 | + using (Mat result = new Mat()) |
| 59 | + { |
| 60 | + Cv2.CvtColor(src, src, ColorConversionCodes.BGR2RGB); |
| 61 | + var lowPurple = new Scalar(253, 253, 253); |
| 62 | + var highPurple = new Scalar(255, 255, 255); |
| 63 | + Cv2.InRange(src, lowPurple, highPurple, src); |
| 64 | + Cv2.Threshold(src, src, 0, 255, ThresholdTypes.Binary); |
| 65 | + var kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(20, 20), new OpenCvSharp.Point(-1, -1)); |
| 66 | + Cv2.Dilate(src, src, kernel); //膨胀 |
| 67 | + |
| 68 | + Scalar color = new Scalar(0, 0, 255); |
| 69 | + OpenCvSharp.Point[][] contours; |
| 70 | + HierarchyIndex[] hierarchy; |
| 71 | + Cv2.FindContours(src, out contours, out hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxSimple, null); |
| 72 | + if (contours.Length > 0) |
| 73 | + { |
| 74 | + var imgTar = img.ToMat(); |
| 75 | + var boxes = contours.Select(Cv2.BoundingRect); |
| 76 | + List<Rect> rects = boxes.ToList(); |
| 77 | + if (rects.Count > 1) |
| 78 | + { |
| 79 | + rects.Sort((a, b) => b.Height.CompareTo(a.Height)); |
| 80 | + } |
| 81 | + if (rects[0].Height < src.Height |
| 82 | + && rects[0].Width * 1.0 / rects[0].Height >= 3 // 长宽比判断 |
| 83 | + && capture.W > rects[0].Width * 3 // 文字范围3倍小于钓鱼条范围的 |
| 84 | + && capture.W * 1.0 / 2 > rects[0].X // 中轴线判断左 |
| 85 | + && capture.W * 1.0 / 2 < rects[0].X + rects[0].Width) // 中轴线判断右 |
| 86 | + { |
| 87 | + foreach (Rect rect in rects) |
| 88 | + { |
| 89 | + Cv2.Rectangle(imgTar, new OpenCvSharp.Point(rect.X, rect.Y), new OpenCvSharp.Point(rect.X + rect.Width, rect.Y + rect.Height), Scalar.Red, 2); |
| 90 | + } |
| 91 | + if (enableImShow) |
| 92 | + { |
| 93 | + Cv2.ImShow("自动提杆识别窗口", imgTar); |
| 94 | + } |
| 95 | + return rects[0]; |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + return Rect.Empty; |
| 100 | + } |
| 101 | + |
| 102 | + } |
| 103 | +} |
0 commit comments