before candybox editor update
This commit is contained in:
119
Plugins/CandyboxPlugin/Forms/MaskControl.cs
Normal file
119
Plugins/CandyboxPlugin/Forms/MaskControl.cs
Normal file
@@ -0,0 +1,119 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using Inspectron.Hawkeye.Vision.Geometry;
|
||||
using OpenCvSharp;
|
||||
using OpenCvSharp.Extensions;
|
||||
using Point = System.Drawing.Point;
|
||||
|
||||
|
||||
namespace Lindt.Candybox.Demo
|
||||
{
|
||||
public partial class MaskControl : UserControl
|
||||
{
|
||||
private Bitmap _backBuffer;
|
||||
private GrahamConvexHull _hull;
|
||||
|
||||
public MaskControl()
|
||||
{
|
||||
InitializeComponent();
|
||||
_backBuffer = new Bitmap(1800, 1408);
|
||||
_hull = new GrahamConvexHull();
|
||||
}
|
||||
|
||||
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
|
||||
public Bitmap Mask { get; set; } = new Bitmap(1800, 1408);
|
||||
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
|
||||
public Bitmap Original { get; set; } = new Bitmap(1800, 1408);
|
||||
|
||||
protected override void OnPaintBackground(PaintEventArgs e)
|
||||
{
|
||||
//base.OnPaintBackground(e);
|
||||
}
|
||||
|
||||
private bool _isDown = false;
|
||||
private Point _lastMouse;
|
||||
|
||||
|
||||
protected override void OnMouseDown(MouseEventArgs e)
|
||||
{
|
||||
_isDown = true;
|
||||
_lastMouse = ConvertCoordinates(new Point(e.X, e.Y));
|
||||
}
|
||||
|
||||
protected override void OnMouseUp(MouseEventArgs e)
|
||||
{
|
||||
_isDown = false;
|
||||
}
|
||||
|
||||
private Pen _pen = new Pen(Color.White, 30);
|
||||
private Pen _penBlack = new Pen(Color.Black, 30);
|
||||
protected override void OnMouseMove(MouseEventArgs e)
|
||||
{
|
||||
|
||||
if (_isDown&&e.Button==MouseButtons.Left)
|
||||
{
|
||||
var curPoint = ConvertCoordinates(new Point(e.X, e.Y));
|
||||
using (Graphics g = Graphics.FromImage(Mask))
|
||||
{
|
||||
g.DrawLine(_pen, curPoint.X, curPoint.Y,_lastMouse.X,_lastMouse.Y);
|
||||
g.FillEllipse(Brushes.White, _lastMouse.X - 15, _lastMouse.Y - 15, 30,30);
|
||||
}
|
||||
}
|
||||
if (_isDown && e.Button == MouseButtons.Right)
|
||||
{
|
||||
var curPoint = ConvertCoordinates(new Point(e.X, e.Y));
|
||||
using (Graphics g = Graphics.FromImage(Mask))
|
||||
{
|
||||
g.DrawLine(_penBlack, curPoint.X, curPoint.Y, _lastMouse.X, _lastMouse.Y);
|
||||
g.FillEllipse(Brushes.Black, _lastMouse.X - 15, _lastMouse.Y - 15, 30, 30);
|
||||
}
|
||||
}
|
||||
_lastMouse = ConvertCoordinates(new Point(e.X, e.Y));
|
||||
Invalidate();
|
||||
}
|
||||
|
||||
private Pen _contourPen = new Pen(Color.GreenYellow, 3);
|
||||
protected override void OnPaint(PaintEventArgs e)
|
||||
{
|
||||
var mat = Mask.ToMat().CvtColor(ColorConversionCodes.BGR2GRAY);
|
||||
var contours = mat.FindContoursAsArray(RetrievalModes.External, ContourApproximationModes.ApproxNone);
|
||||
LastContours = contours;
|
||||
using (Graphics g = Graphics.FromImage(_backBuffer))
|
||||
{
|
||||
g.DrawImage(Original, 0, 0);
|
||||
|
||||
for (int i = 0; i < contours.Length; i++)
|
||||
{
|
||||
if (contours[i].Length > 1)
|
||||
{
|
||||
var points = contours[i].Select(x => new System.Drawing.Point(x.X, x.Y)).ToArray();
|
||||
g.DrawPolygon(_contourPen, points);
|
||||
}
|
||||
}
|
||||
g.DrawEllipse(Pens.Gray,_lastMouse.X-15, _lastMouse.Y - 15,30,30);
|
||||
}
|
||||
e.Graphics.DrawImage(_backBuffer,0, 0,Width,Height);
|
||||
}
|
||||
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
|
||||
public OpenCvSharp.Point[][] LastContours { get; set; }
|
||||
|
||||
Point ConvertCoordinates(Point coords)
|
||||
{
|
||||
return new Point(coords.X * 2, coords.Y * 2);
|
||||
}
|
||||
|
||||
protected override void OnResize(EventArgs e)
|
||||
{
|
||||
base.OnResize(e);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user