Last active
August 29, 2015 14:17
-
-
Save 0V/68ad311b38e3a3ec5bfe to your computer and use it in GitHub Desktop.
OpenCvSharp でライン引きサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using OpenCvSharp; | |
using OpenCvSharp.CPlusPlus; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace OpenCvSharpSample.Samples | |
{ | |
public class MethodTest | |
{ | |
public static void MouseClickLineWindow(int pointMax = 1000) | |
{ | |
var pointList = new List<Point>() { new Point(0, 0) }; | |
using (var window = new Window("canvas")) | |
{ | |
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 && pointList.Count < pointMax) | |
{ | |
mat = new Mat(new Size(640, 480), MatType.CV_8UC3); | |
mat.SetTo(new Scalar(255, 255, 255)); | |
pointList.Add(new Point(x, y)); | |
var beforePoint = pointList.First(); | |
foreach (var point in pointList.Skip(1)) | |
{ | |
mat.Line(beforePoint, point, color, 2, LineType.AntiAlias); | |
beforePoint = point; | |
} | |
window.ShowImage(mat); | |
} | |
}; | |
bool notEscDown = true; | |
while (notEscDown) | |
{ | |
switch (Cv2.WaitKey(1)) | |
{ | |
case 3014656: //Delete #Clear | |
pointList.Clear(); | |
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