/// <summary> /// 高速なマルチスケール Hesian 検出器を用いて keypoint を検出します. /// </summary> /// <param name="img"></param> /// <param name="mask"></param> /// <returns></returns> #else /// <summary> /// detects keypoints using fast multi-scale Hessian detector /// </summary> /// <param name="img"></param> /// <param name="mask"></param> /// <returns></returns> #endif public KeyPoint[] Extract(Mat img, Mat mask) { if (img == null) throw new ArgumentNullException("img"); CvMat imgMat = img.ToCvMat(); CvMat maskMat = (mask == null) ? null : mask.ToCvMat(); CvSURFPoint[] keypoints; float[][] descriptors; Cv.ExtractSURF(imgMat, maskMat, out keypoints, out descriptors, this); KeyPoint[] result = new KeyPoint[keypoints.Length]; for (int i = 0; i < result.Length; i++) { CvSURFPoint kpt = keypoints[i]; result[i] = new KeyPoint(kpt.Pt, (float) kpt.Size, kpt.Dir, kpt.Hessian, GetPointOctave(kpt, this)); } return result; }
/// <summary> /// keypoint を検出し,その SURF ディスクリプタを計算します.[useProvidedKeypoints = true] /// </summary> /// <param name="img"></param> /// <param name="mask"></param> /// <param name="keypoints"></param> /// <param name="descriptors"></param> #else /// <summary> /// detects keypoints and computes the SURF descriptors for them. [useProvidedKeypoints = true] /// </summary> /// <param name="img"></param> /// <param name="mask"></param> /// <param name="keypoints"></param> /// <param name="descriptors"></param> #endif public void Extract(Mat img, Mat mask, KeyPoint[] keypoints, out float[][] descriptors) { if (img == null) throw new ArgumentNullException("img"); CvMat imgMat = img.ToCvMat(); CvMat maskMat = (mask == null) ? null : mask.ToCvMat(); CvSURFPoint[] kpt = new CvSURFPoint[keypoints.Length]; for (int i = 0; i < keypoints.Length; i++) { KeyPoint k = keypoints[i]; kpt[i] = new CvSURFPoint(k.Pt, 1, (int) Math.Round(k.Size), k.Angle, k.Response); } Cv.ExtractSURF(imgMat, maskMat, ref kpt, out descriptors, this, true); for (int i = 0; i < kpt.Length; i++) { CvSURFPoint p = kpt[i]; keypoints[i] = new KeyPoint(p.Pt, p.Size, p.Dir, p.Hessian, GetPointOctave(p, this)); } }
private void Transform(double[] srcPoints) { const int POINT_COUNT = 8; System.Diagnostics.Debug.Assert(srcPoints.Length == POINT_COUNT); double leftOffset = (srcGrid.Width - imgRaw.Source.Width) / 2; double topOffset = (srcGrid.Height - imgRaw.Source.Height) / 2; CvMat srcPointsMat = new CvMat(4, 2, MatrixType.F64C1, srcPoints); CvMat dstPointsMat = new CvMat(4, 2, MatrixType.F64C1, new double[POINT_COUNT] { dstGrid.Width * 1 / 4, dstGrid.Height * 1 / 4, dstGrid.Width * 3 / 4, dstGrid.Height * 1 / 4, dstGrid.Width * 3 / 4, dstGrid.Height * 3 / 4, dstGrid.Width * 1 / 4, dstGrid.Height * 3 / 4 }); CvMat viewerHomographyMatrix = new CvMat(3, 3, MatrixType.F64C1, new double[9]); Cv.FindHomography(srcPointsMat, dstPointsMat, viewerHomographyMatrix); CV.Mat src = WriteableBitmapConverter.ToMat((WriteableBitmap)imgRaw.Source); CV.Mat dst = new CV.Mat((int)srcGrid.Height, (int)srcGrid.Width, src.Type()); Cv.WarpPerspective(src.ToCvMat(), dst.ToCvMat(), viewerHomographyMatrix); imgTransformed.Source = WriteableBitmapConverter.ToWriteableBitmap(dst); srcPointsMat.Dispose(); dstPointsMat.Dispose(); src.Dispose(); dst.Dispose(); }
/// <summary> /// keypoint を検出し,その SURF ディスクリプタを計算します.[useProvidedKeypoints = false] /// </summary> /// <param name="img"></param> /// <param name="mask"></param> /// <param name="keypoints"></param> /// <param name="descriptors"></param> #else /// <summary> /// detects keypoints and computes the SURF descriptors for them. [useProvidedKeypoints = false] /// </summary> /// <param name="img"></param> /// <param name="mask"></param> /// <param name="keypoints"></param> /// <param name="descriptors"></param> #endif public void Extract(Mat img, Mat mask, out KeyPoint[] keypoints, out float[][] descriptors) { if (img == null) throw new ArgumentNullException("img"); CvMat imgMat = img.ToCvMat(); CvMat maskMat = (mask == null) ? null : mask.ToCvMat(); CvSURFPoint[] kpt; Cv.ExtractSURF(imgMat, maskMat, out kpt, out descriptors, this); keypoints = new KeyPoint[kpt.Length]; for (int i = 0; i < keypoints.Length; i++) { CvSURFPoint p = kpt[i]; keypoints[i] = new KeyPoint(p.Pt, (float) p.Size, p.Dir, p.Hessian, GetPointOctave(p, this)); } }
/// <summary> /// MSERのすべての輪郭情報を抽出する /// </summary> /// <param name="image"></param> /// <param name="mask"></param> /// <returns></returns> #else /// <summary> /// Extracts the contours of Maximally Stable Extremal Regions /// </summary> /// <param name="image"></param> /// <param name="mask"></param> /// <returns></returns> #endif public CvPoint[][] Extract(Mat image, Mat mask) { if(image == null) throw new ArgumentNullException("image"); CvMat _image = image.ToCvMat(); IntPtr pmask = (mask == null) ? IntPtr.Zero : mask.ToCvMat().CvPtr; IntPtr pcontours = IntPtr.Zero; using(CvMemStorage storage = new CvMemStorage(0)) { CvInvoke.cvExtractMSER(_image.CvPtr, pmask, ref pcontours, storage.CvPtr, Struct); if (pcontours == IntPtr.Zero) { return new CvPoint[0][]; } CvSeq<IntPtr> seq = new CvSeq<IntPtr>(pcontours); CvContour[] contours = Array.ConvertAll<IntPtr, CvContour>(seq.ToArray(), delegate(IntPtr p) { return new CvContour(p); }); CvPoint[][] result = new CvPoint[contours.Length][]; for (int i = 0; i < contours.Length; i++) { result[i] = contours[i].ToArray(); } return result; } }