Last active
October 17, 2015 16:28
-
-
Save 0V/63bc4dee6a205f449693 to your computer and use it in GitHub Desktop.
OpenCvSharp で対称定規。 依存関係は OpenCvSharp のみ
This file contains 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 System; | |
using System.Collections.Generic; | |
using OpenCvSharp.CPlusPlus; | |
using OpenCvSharp; | |
namespace OpenCvSharpSomethings | |
{ | |
class Program | |
{ | |
static Mat[] rotMat; | |
static IEnumerable<IEnumerable<Point>> GetSymmetryLinePoints(Point p1, Point p2, Size imageSize) | |
{ | |
var ox = (imageSize.Width / 2); | |
var oy = (imageSize.Height / 2); | |
var src1 = new Mat(3, 1, MatType.CV_64FC1, new double[] { p1.X, p1.Y, 1 }); | |
var src2 = new Mat(3, 1, MatType.CV_64FC1, new double[] { p2.X, p2.Y, 1 }); | |
var src3 = new Mat(3, 1, MatType.CV_64FC1, new double[] { imageSize.Width - p1.X, p1.Y, 1 }); | |
var src4 = new Mat(3, 1, MatType.CV_64FC1, new double[] { imageSize.Width - p2.X, p2.Y, 1 }); | |
var dstPoints = new List<List<Point>>(); | |
for (int i = 0; i <6; i++) | |
{ | |
Mat m1 = rotMat[i] * src1; | |
Mat m2 = rotMat[i] * src2; | |
Mat m3 = rotMat[i] * src3; | |
Mat m4 = rotMat[i] * src4; | |
var points = new List<Point> | |
{ | |
new Point(m1.At<double>(0), m1.At<double>(1)), | |
new Point(m2.At<double>(0), m2.At<double>(1)) | |
}; | |
var points2 = new List<Point> | |
{ | |
new Point(m3.At<double>(0), m3.At<double>(1)), | |
new Point(m4.At<double>(0), m4.At<double>(1)) | |
}; | |
dstPoints.Add(points); | |
dstPoints.Add(points2); | |
} | |
return dstPoints; | |
} | |
static void Main(string[] args) | |
{ | |
using (var window = new Window("対称定規")) | |
{ | |
const int WindowHeightWidth = 600; | |
var ImageSize = new Size(WindowHeightWidth, WindowHeightWidth); | |
var BackgroundColor = CvColor.Black; | |
var ForegroundColor = CvColor.Aquamarine; | |
var previewPoint = new Point(-1, -1); | |
var mat = new Mat(ImageSize, MatType.CV_8UC3); | |
int Thickness = 1; | |
mat.SetTo(BackgroundColor); | |
// 回転行列作成 | |
rotMat = new Mat[6]; | |
for (int i = 0; i < 6; i++) | |
{ | |
rotMat[i] = Cv2.GetRotationMatrix2D(new Point2f(ImageSize.Width / 2, ImageSize.Height / 2), i * -60, 1); | |
} | |
window.ShowImage(mat); | |
window.OnMouseCallback += (e, x, y, flags) => | |
{ | |
if (e == MouseEvent.MouseMove && flags == MouseEvent.FlagLButton) | |
{ | |
var nowPoint = new Point(x, y); | |
if (previewPoint.X != -1) | |
{ | |
var points = GetSymmetryLinePoints(previewPoint, nowPoint, ImageSize); | |
mat.Polylines(points, false, ForegroundColor, Thickness, LineType.AntiAlias); | |
previewPoint = nowPoint; | |
} | |
else | |
{ | |
mat.Circle(nowPoint, 1, ForegroundColor, Thickness - 1, LineType.AntiAlias); | |
previewPoint = nowPoint; | |
} | |
window.ShowImage(mat); | |
} | |
else if (e == MouseEvent.LButtonUp) | |
{ | |
previewPoint = new Point(-1, -1); | |
} | |
}; | |
bool notEscDown = true; | |
while (notEscDown) | |
{ | |
switch (Cv2.WaitKey(1)) | |
{ | |
case 3014656: //Delete #Clear | |
mat.SetTo(BackgroundColor); | |
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