public void CheckImageDisposed(bool invokeDispose) { bool disposeCheck = false; void onDispose(object obj, EventArgs e) { disposeCheck = true; } void createImageAndAttach() { new MatrixImage <byte>(50, 50).Disposed += onDispose; } if (invokeDispose) { MatrixImage <byte> image = new MatrixImage <byte>(50, 50); image.Disposed += onDispose; image.Dispose(); } else { createImageAndAttach(); GC.Collect(); GC.WaitForPendingFinalizers(); } Assert.True(disposeCheck); }
public override IMatrixImage Invoke(IMatrixImage input) { Map map = (Map)MainController.CurrentController.Storage["Map"]; map.Clear(); MatrixImage <byte> img = (MatrixImage <byte>)ImageLib.Controller.MainController.CurrentController.Storage["img"]; return(img.Clone()); }
public static Mat GetCVMat <TElement>(this MatrixImage <TElement> image, ColorConversionCodes conversion) where TElement : unmanaged, IComparable <TElement> { Mat <TElement>[] mats = image.Split(false).Select(a => GetCVMat(a)).ToArray(); Mat <TElement> mat = new Mat <TElement>(image.Height, image.Width); Cv2.Merge(mats, mat); return(mat.CvtColor(conversion)); }
public MainForm() { InitializeComponent(); MatrixImage <byte> m1 = new MatrixImage <byte>(256, 256, 1); m1.Split(false)[0].ForEachPixelsSet((x, y) => (byte)y); imageHistoryView1.AddHistoryElement(new ImageHistory(m1, "m1")); MatrixImage <byte> m2 = new MatrixImage <byte>(256, 256, 1); m2.Split(false)[0].ForEachPixelsSet((x, y) => (byte)x); imageHistoryView1.AddHistoryElement(new ImageHistory(m2, "m2")); MatrixImage <byte> vs = new MatrixImage <byte>(256, 256, 4); vs.Split(false)[0].ForEachPixelsSet((x, y) => (byte)x); vs.Split(false)[1].ForEachPixelsSet((x, y) => (byte)y); //vs.Split(false)[2].ForEachPixelsSet((x, y) => (byte)(x + y); vs.Split(false)[2].ForEachPixelsSet((x, y) => (byte)((x + y) > 255 ? 255 - (x + y % 256) : x + y)); vs.Split(false)[3].ForEachPixelsSet((x, y) => (byte)(255 - (y / 2))); MatrixLayer <byte> layer = new MatrixLayer <byte>(32, 32); vs.Split(false)[3].Insert(layer, 128 - 16, 128 - 16); //vs.Split(false)[3].ForEachPixelsSet((x, y) => y == 127 || y == 128 ? (byte)1 : y > 128 ? (byte)(y * 2) : (byte)((255 - y) * 2)); imageHistoryView1.AddHistoryElement(new ImageHistory(vs, "vs")); //vs.Split(false)[3].ForEachPixels((x, y) => (byte)255); //vs.Split(false)[2].ForEachPixels((x, y) => (byte)y); BitmapHandler bitmapHandler = vs.CreateBitmap(); this.imageView1.BitmapHandler = bitmapHandler; imageHistoryView1.HistorySelected += (_, img) => { imageView1.BitmapHandler = img.CreateImage().CreateBitmap(); }; //Random r = new Random(); //new Thread(() => //{ // try // { // Thread.Sleep(2000); // while (true) // { // this.Invoke(new Action(() => // vs.Split(false)[0].SetPoint((byte)r.Next(0, 255), r.Next(0, 255), r.Next(0, 255)))); // Thread.Sleep(5); // } // } // catch // { // } //}).Start(); }
public static void SetCVMat <TElement>(this MatrixImage <TElement> image, Mat mat) where TElement : unmanaged, IComparable <TElement> { var layers = image.Split(false); var mats = mat.Split(); int min = Math.Min(layers.Length, mats.Length); for (int i = 0; i < min; i++) { layers[i].SetCVMat <TElement>(mats[i]); } }
public void Select() { SelectImage select = new SelectImage(); if (select.image == null) { return; } image = select.image; matrix_box.image = image; if (matrix_box == null || Checker.IsTrue(matrix_box, rows, cols)) { Level(rows, cols); } else { matrix_image = new MatrixImage(image, rows, cols); matrix_image.Split(); matrix_box.matrix_image = matrix_image; matrix_box.Keep(); } SetGrid.SetImage(gridPicture, image); }
public abstract MatrixImage <TOutput> Invoke(MatrixImage <TInput> input);
public MatrixImage <byte>?Invoke(CancellationToken ct = default) { if (UseOpenCV) { MatrixImage <byte> img = (MatrixImage <byte>)ImageLib.Controller.MainController.CurrentController.Storage["img"]; img = img.Clone(); Mat mat; if (img.LayerCount > 1) { img.Split(false)[1].ForEachPixelsSet(a => (byte)(a / 1.5)); img.Split(false)[2].ForEachPixelsSet(a => (byte)(a / 1.25)); mat = img.GetCVMat(ColorConversionCodes.BGR2HSV) .ExtractChannel(1); } else { mat = img.GetCVMat(); } HierarchyIndex[] indices; //mat = mat.ExtractChannel(1).AdaptiveThreshold(MaxValue, AdaptiveThresholdTypes.MeanC, ThresholdType, 3, Saturation); mat = mat.Threshold(Saturation, MaxValue, ThresholdType) .Erode(new Mat()).Dilate(new Mat()); Mat view; if (ShowSaturation) { if (img.LayerCount > 1) { view = img.GetCVMat(ColorConversionCodes.BGR2HSV); } else { view = img.GetCVMat(); } } else { view = ((MatrixImage <byte>)ImageLib.Controller.MainController.CurrentController.Storage["img"]).GetCVMat(); } Map map = (Map)MainController.CurrentController.Storage["Map"]; try { map.CasheDotDistances = true; map.Clear(); OpenCvSharp.Point[][] points = mat.FindContoursAsArray(RetrievalModes.External, ContourApproximationModes.ApproxTC89KCOS); //mat.FindContours(out points, out indices, RetrievalModes.External, ContourApproximationModes.ApproxTC89KCOS); int length = Length; for (int i = 0; i < points.Length /*&& !CancellationToken.IsCancellationRequested*/; i++) { //if(i>5000)break; if (points[i].Length < length) { continue; } var rect = OpenCvSharp.Cv2.BoundingRect(points[i]); //OpenCvSharp.Cv2.Rectangle(hsv, rect, Scalar.Black); var rrect = Cv2.MinAreaRect(points[i]); Point2f[] ps = rrect.Points(); //Point2f edge1 = ps[0] - ps[1]; //Point2f edge2 = ps[2] - ps[1]; var p0 = new PointF(ps[0].X, ps[0].Y); var p1 = new PointF(ps[1].X, ps[1].Y); var p2 = new PointF(ps[2].X, ps[2].Y); var p3 = new PointF(ps[3].X, ps[3].Y); var edge1 = LineVector.FindVector(p0, p1); var edge2 = LineVector.FindVector(p2, p1); //if (edge1.Length * 2 < edge2.Length || edge2.Length * 2 < edge1.Length) //{ // PointF dp1, dp2; // if (edge1.Length > edge2.Length) // { // var tmp = new PointF(edge2.X / 2f, edge2.Y / 2f); // //dp1 = new PointF(edge1.X / 2f, edge1.Y / 2f); // dp1 = new PointF(p0.X - tmp.X, p0.Y - tmp.Y); // dp2 = new PointF(dp1.X + edge1.X, dp1.Y + edge1.Y); // dp1 = new PointF(dp1.X + edge1.X / 10, dp1.Y + edge1.Y / 10); // dp2 = new PointF(dp2.X - edge1.X / 10, dp2.Y - edge1.Y / 10); // //dp2 = new PointF(p0.X + tmp.X, p0.Y + tmp.Y); // } // else // { // var tmp = new PointF(edge1.X / 2f, edge1.Y / 2f); // dp1 = new PointF(p0.X + tmp.X, p0.Y + tmp.Y); // dp2 = new PointF(dp1.X - edge2.X, dp1.Y - edge2.Y); // dp1 = new PointF(dp1.X - edge2.X / 10, dp1.Y - edge2.Y / 10); // dp2 = new PointF(dp2.X + edge2.X / 10, dp2.Y + edge2.Y / 10); // } // var dot1 = map.CreateDot(dp1); // var dot2 = map.CreateDot(dp2); // dot1.startConnect = dot2; // dot2.startConnect = dot1; //} //else { map.CreateDot(new System.Drawing.PointF( rect.Location.X + rect.Width / 2, rect.Location.Y + rect.Height / 2)); } if (DrawRects) { if (ShowTreshold) { mat.Line((int)ps[0].X, (int)ps[0].Y, (int)ps[1].X, (int)ps[1].Y, Scalar.AntiqueWhite); mat.Line((int)ps[1].X, (int)ps[1].Y, (int)ps[2].X, (int)ps[2].Y, Scalar.AntiqueWhite); mat.Line((int)ps[2].X, (int)ps[2].Y, (int)ps[3].X, (int)ps[3].Y, Scalar.AntiqueWhite); mat.Line((int)ps[3].X, (int)ps[3].Y, (int)ps[0].X, (int)ps[0].Y, Scalar.AntiqueWhite); } else { view.Line((int)ps[0].X, (int)ps[0].Y, (int)ps[1].X, (int)ps[1].Y, Scalar.AntiqueWhite); view.Line((int)ps[1].X, (int)ps[1].Y, (int)ps[2].X, (int)ps[2].Y, Scalar.AntiqueWhite); view.Line((int)ps[2].X, (int)ps[2].Y, (int)ps[3].X, (int)ps[3].Y, Scalar.AntiqueWhite); view.Line((int)ps[3].X, (int)ps[3].Y, (int)ps[0].X, (int)ps[0].Y, Scalar.AntiqueWhite); } } } //Debug.WriteLine(indices.Length); //Debugger.Break(); //var ret = new MatrixImage<byte>(new[]{ CurrentImage.ToByteImage(false).Split(false)[0] }, true); MatrixImage <byte> ret; if (ShowTreshold) { ret = new MatrixImage <byte>(img.Width, img.Height, 1); ret.SetCVMat(mat); } else { if (ShowSaturation) { ret = new MatrixImage <byte>(img.Width, img.Height, 1); ret.SetCVMat(view.ExtractChannel(1)); } //else if (DrawRects) //{ ret = new MatrixImage <byte>(img.Width, img.Height, 3); ret.SetCVMat(view); //} //else //{ // //ret = new MatrixImage<byte>(img.Width, img.Height, img.LayerCount); // //ret.SetCVMat(empty); // ret = img; //} } //CurrentController.SetImage(ret); return(ret); } finally { map.CasheDotDistances = true; if (UseStep) { map.BuildLineSets(cancellationToken: ct, step: StepSync.Instance); } map.BuildLineSets(); mat?.Dispose(); view?.Dispose(); } } else { if (UseStep) { Map.Instance.BuildLineSets(cancellationToken: ct, step: StepSync.Instance); } else { Map.Instance.BuildLineSets(); } return(null); } }
public ContainerImage(Map map, MatrixImage <TElement> image) : base(map) { InputImage = image; }
public ImageContainer(MatrixImage <TElement> image) { InputImage = image; }