Skip to content

Instantly share code, notes, and snippets.

@0V
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0V/b567a40a829fa0f6e598 to your computer and use it in GitHub Desktop.
Save 0V/b567a40a829fa0f6e598 to your computer and use it in GitHub Desktop.
OpenCvSharp でお絵かき。なめらかな曲線を描ける。
using OpenCvSharp;
using OpenCvSharp.CPlusPlus;
using System;
using System.Collections.Generic;
using System.Linq;
namespace OpenCvSharpSample.Samples
{
public class MethodTest
{
public static void MouseClickPolylinesCanvasWindow()
{
using (var window = new Window("canvas"))
{
var pointList = new List<List<Point>>();
bool moving = false;
var color = new Scalar(100, 100, 0);
var mat = new Mat(new Size(640, 480), MatType.CV_8UC3);
mat.SetTo(new Scalar(255, 255, 255));
window.ShowImage(mat);
window.OnMouseCallback += (e, x, y, flags) =>
{
if (e == MouseEvent.LButtonDown)
{
if (moving)
{
pointList.Last().Add(new Point(x, y));
}
else
{
pointList.Add(new List<Point>() { new Point(x, y) });
}
moving = true;
mat.Circle(x, y, 1, color, 3, LineType.AntiAlias);
window.ShowImage(mat);
}
else if (e == MouseEvent.MouseMove && flags == MouseEvent.FlagLButton)
{
if (moving)
{
pointList.Last().Add(new Point(x, y));
// 点を削除
pointList.RemoveAll(list => list.Count <= 1);
mat.Polylines(pointList, false, color, 3, LineType.AntiAlias);
}
else
{
pointList.Add(new List<Point>() { new Point(x, y) });
}
moving = true;
window.ShowImage(mat);
}
else if (e == MouseEvent.LButtonUp)
{
moving = false;
}
};
bool notEscDown = true;
while (notEscDown)
{
switch (Cv2.WaitKey(1))
{
case 3014656: //Delete #Clear
pointList.Clear();
pointList.Add(new List<Point>());
mat.SetTo(new Scalar(255, 255, 255));
window.ShowImage(mat);
break;
case 27: //Esc #Exit
notEscDown = false;
break;
case 32: //SpaceBar #Save
mat.SaveImage(DateTime.Now.ToString("yyyyMMdd-HHmmss") + ".jpg");
break;
default:
break;
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment