public static void CropMatrixToStructure(Matrix m, StructureMeta contour, double marginMM) { var allPoints = contour.SliceContours.SelectMany(sc => sc.ContourPoints).ToList(); var minPointX = allPoints.Min(pt => pt.X) - marginMM; var maxPointX = allPoints.Max(pt => pt.X) + marginMM; var minPointY = allPoints.Min(pt => pt.Y) - marginMM; var maxPointY = allPoints.Max(pt => pt.Y) + marginMM; var minPointZ = contour.SliceContours.Min(sc => sc.Z) - marginMM; var maxPointZ = contour.SliceContours.Max(sc => sc.Z) + marginMM; var minCorner = new Point3d(minPointX, minPointY, minPointZ); var maxCorner = new Point3d(maxPointX, maxPointY, maxPointZ); var imageCoordMinCorner = m.PatientTransformMatrix.Inv().ToMat().TransformPoint3d(minCorner); var imageCoordMaxCorner = m.PatientTransformMatrix.Inv().ToMat().TransformPoint3d(maxCorner); var minX = (int)Math.Floor(Math.Min(imageCoordMinCorner.X, imageCoordMaxCorner.X)); minX = minX < 0 ? 0 : minX; var minY = (int)Math.Floor(Math.Min(imageCoordMinCorner.Y, imageCoordMaxCorner.Y)); minY = minY < 0 ? 0 : minY; var minZ = (int)Math.Floor(Math.Min(imageCoordMinCorner.Z, imageCoordMaxCorner.Z)); minZ = minZ < 0 ? 0 : minZ; var maxX = (int)Math.Ceiling(Math.Max(imageCoordMinCorner.X, imageCoordMaxCorner.X)); maxX = maxX > (m.DimensionX - 1) ? m.DimensionX : maxX; var maxY = (int)Math.Ceiling(Math.Max(imageCoordMinCorner.Y, imageCoordMaxCorner.Y)); maxY = maxY > (m.DimensionY - 1) ? m.DimensionY : maxY; var maxZ = (int)Math.Ceiling(Math.Max(imageCoordMinCorner.Z, imageCoordMaxCorner.Z)); maxZ = maxZ > (m.DimensionZ - 1) ? m.DimensionZ : maxZ; var rangeX = new OpenCvSharp.Range(minX, maxX); var rangeY = new OpenCvSharp.Range(minY, maxY); var rangeZ = new OpenCvSharp.Range(minZ, maxZ); using (var cropped = new Mat(m._mat, rangeZ, rangeY, rangeX)) { m.DimensionX = maxX - minX; m.DimensionY = maxY - minY; m.DimensionZ = maxZ - minZ; var xOffset = minX * m.XRes; var yOffset = minY * m.YRes; var zOffset = minZ * m.ZRes; var txOffset = Transform.TransformOffset(new Vector3(xOffset, yOffset, zOffset), m.ImageOrientation); m.Origin = m.Origin + txOffset; var mat = cropped.Clone(); m._mat.Dispose(); m._mat = mat; m.CalculatePatientTransformMatrix(); m.CalculateBounds(); } }
public void SetZPlaneBySlice(Mat insert, int z) { var rangeX = OpenCvSharp.Range.All; var rangeY = OpenCvSharp.Range.All; var rangeZ = new OpenCvSharp.Range(z, z + 1); using (var slice = new Mat(_mat, rangeZ, rangeY, rangeX)) { using (var slice2D = slice.Reshape(_mat.Channels(), new int[] { DimensionY, DimensionX })) { insert.CopyTo(slice2D); } } }
public Mat GetYPlaneBySlice(int y) { var rangeX = OpenCvSharp.Range.All; var rangeY = new OpenCvSharp.Range(y, y + 1); var rangeZ = OpenCvSharp.Range.All; using (var slice = new Mat(_mat, rangeZ, rangeY, rangeX).Clone()) { using (var slice2D = slice.Reshape(_mat.Channels(), new int[] { DimensionZ, DimensionX })) { return(slice2D.Clone()); } } }
public Mat GetZPlaneBySlice(int z) { var rangeX = OpenCvSharp.Range.All; var rangeY = OpenCvSharp.Range.All; var rangeZ = new OpenCvSharp.Range(z, z + 1); var clone = new Mat(); using (var slice = new Mat(_mat, rangeZ, rangeY, rangeX).Clone()) { using (var slice2D = slice.Reshape(_mat.Channels(), new int[] { DimensionY, DimensionX })) { clone = slice2D.Clone(); } } return(clone); }
/// <summary> /// Sets a matrix header for the specified matrix row/column span. /// </summary> /// <param name="range"></param> /// <param name="value"></param> /// <returns></returns> public void Set(Range range, MatExpr value) { this[range] = value; }
public static int FindMaxValueIndexInRange <T>(this Mat inputMatrix, int dim0Index, Range dim1Range) where T : unmanaged, IComparable <T> { if (inputMatrix is null) { throw new ArgumentNullException(nameof(inputMatrix)); } int dim1MaxIndex = -1; T dim1MaxValue = default; for (int dim1Index = dim1Range.Start; dim1Index <= dim1Range.End; dim1Index++) { var curValue = inputMatrix.At <T>(dim0Index, dim1Index); if (curValue.CompareTo(dim1MaxValue) > 0) { dim1MaxIndex = dim1Index; dim1MaxValue = curValue; } } return(dim1MaxIndex); }
public static extern IntPtr core_Mat_new5(IntPtr mat, Range rowRange);
/// <summary> /// Creates a matrix header for the specified row/column span. /// </summary> /// <param name="rowRange"></param> /// <param name="colRange"></param> /// <returns></returns> public MatExpr Get(Range rowRange, Range colRange) { return this[rowRange, colRange]; }
/// <summary> /// Sets a matrix header for the specified matrix row/column span. /// </summary> /// <param name="rowRange"></param> /// <param name="colRange"></param> /// <param name="value"></param> /// <returns></returns> public void Set(Range rowRange, Range colRange, MatExpr value) { this[rowRange, colRange] = value; }
/// <summary> /// /// </summary> /// <param name="rowRange"></param> /// <param name="colRange"></param> /// <returns></returns> public MatExpr this[Range rowRange, Range colRange] { get { return SubMat(rowRange, colRange); } set { if (value == null) throw new ArgumentNullException("value"); MatExpr subMatExpr = SubMat(rowRange, colRange); NativeMethods.core_Mat_assignment_FromMatExpr(subMatExpr.CvPtr, value.CvPtr); } }
/// <summary> /// Creates/Sets a matrix header for the specified row/column span. /// </summary> /// <param name="range"></param> /// <param name="value"></param> public virtual void Set(Range range, Mat value) { this[range.Start, range.End] = value; }
public static extern IntPtr cuda_GpuMat_opRange2(IntPtr src, Range rowRange, Range colRange);
public static extern IntPtr cuda_GpuMat_new9(IntPtr m, Range rowRange, Range colRange);
private IList <DnnDetectedObject[]> ExtractYoloBatchedResults(IEnumerable <Mat> output, IEnumerable <Mat> image, float threshold, float nmsThreshold, bool nms = true) { var inputImages = image.ToList(); DnnDetectedObject[][] results = new DnnDetectedObject[inputImages.Count][]; for (int inputIndex = 0; inputIndex < inputImages.Count; inputIndex++) { //for nms _classIds.Clear(); _confidences.Clear(); _probabilities.Clear(); _boxes.Clear(); var w = inputImages[inputIndex].Width; var h = inputImages[inputIndex].Height; /* * YOLO3 COCO trainval output * 0 1 : center 2 3 : w/h * 4 : confidence 5 ~ 84 : class probability */ const int prefix = 5; //skip 0~4 foreach (var prob in output) { //dimensions will be 2 for single image analysis and 3 for batch analysis //2 input Prob Dims:3 images: 0 - 2, 1 - 300, 2 - 85 var probabilitiesRange = new Range(prefix, prob.Size(2) - 1); for (var i = 0; i < prob.Size(1); i++) { var confidence = prob.At <float>(inputIndex, i, 4); //Filter out bogus results of > 100% confidence if (confidence > threshold && confidence <= 1.0) { var maxProbIndex = prob.FindMaxValueIndexInRange <float>(inputIndex, i, probabilitiesRange); if (maxProbIndex == -1) { continue; } var probability = prob.At <float>(inputIndex, i, maxProbIndex); if (probability > threshold) //more accuracy, you can cancel it { //get center and width/height var centerX = prob.At <float>(inputIndex, i, 0) * w; var centerY = prob.At <float>(inputIndex, i, 1) * h; var width = prob.At <float>(inputIndex, i, 2) * w; var height = prob.At <float>(inputIndex, i, 3) * h; float X = Math.Max(0, centerX - (width / 2.0f)); float Y = Math.Max(0, centerY - (height / 2.0f)); //put data to list for NMSBoxes _classIds.Add(maxProbIndex - prefix); _confidences.Add(confidence); _probabilities.Add(probability); _boxes.Add(new Rect2d(X, Y, width, height)); } } } } results[inputIndex] = OptimizeDetections(threshold, nmsThreshold, nms).ToArray(); } return(results); }
/// <summary> /// Creates a matrix header for the specified row/column span. /// </summary> /// <param name="range"></param> /// <returns></returns> public virtual MatExpr this[Range range] { get { return this[range.Start, range.End]; } set { this[range.Start, range.End] = value; } }
/// <summary> /// Creates/Sets a matrix header for the specified row/column span. /// </summary> /// <param name="range"></param> /// <returns></returns> public virtual Mat this[Range range] { get { return this[range.Start, range.End]; } }
/// <summary> /// Creates a matrix header for the specified row/column span. /// </summary> /// <param name="range"></param> /// <returns></returns> public MatExpr Get(Range range) { return this[range]; }
/// <summary> /// Creates a matrix header for the specified row/column span. /// </summary> /// <param name="range"></param> /// <returns></returns> public virtual Mat Get(Range range) { return this[range]; }
/// <summary> /// /// </summary> /// <param name="rowRange"></param> /// <param name="colRange"></param> /// <returns></returns> public MatExpr SubMat(Range rowRange, Range colRange) { return SubMat(rowRange.Start, rowRange.End, colRange.Start, colRange.End); }
public static extern IntPtr core_Mat_subMa2t(IntPtr self, int nRanges, Range[] ranges);
/// <summary> /// Creates a matrix header for the specified row/column span. /// </summary> /// <param name="rowRange"></param> /// <param name="colRange"></param> /// <returns></returns> public abstract MatExpr this[Range rowRange, Range colRange] { get; set; }
public static extern IntPtr core_Mat_new4(IntPtr mat, Range rowRange, Range colRange);
private DnnDetectedObject[][] ExtractYolo3SingleResults(IEnumerable <Mat> output, Mat image, float threshold, float nmsThreshold, bool nms = true) { DnnDetectedObject[][] results = new DnnDetectedObject[1][]; var classIds = new List <int>(); var confidences = new List <float>(); var probabilities = new List <float>(); var boxes = new List <Rect2d>(); //for nms classIds.Clear(); confidences.Clear(); probabilities.Clear(); boxes.Clear(); var w = image.Width; var h = image.Height; /* * YOLO3 COCO trainval output * 0 1 : center 2 3 : w/h * 4 : confidence 5 ~ 84 : class probability */ const int prefix = 5; //skip 0~4 foreach (var prob in output) { //dimensions will be 2 for single image analysis and 3 for batch analysis //1 input Prob Dims:2 images: 0 - 300, 1 - 85 for (var i = 0; i < prob.Size(0); i++) { var confidence = prob.At <float>(i, 4); //Filter out bogus results of > 100% confidence if (confidence > threshold && confidence <= 1.0) { var colRange = new Range(prefix, prob.Size(1) - 1); var maxProbIndex = prob.FindMaxValueIndexInRange <float>(i, colRange); if (maxProbIndex == -1) { continue; } var probability = prob.At <float>(i, maxProbIndex); if (probability > threshold) //more accuracy, you can cancel it { //get center and width/height var centerX = prob.At <float>(i, 0) * w; var centerY = prob.At <float>(i, 1) * h; var width = prob.At <float>(i, 2) * w; var height = prob.At <float>(i, 3) * h; float X = Math.Max(0, centerX - (width / 2.0f)); float Y = Math.Max(0, centerY - (height / 2.0f)); //put data to list for NMSBoxes classIds.Add(maxProbIndex - prefix); confidences.Add(confidence); probabilities.Add(probability); boxes.Add(new Rect2d(X, Y, width, height)); } } } } List <DnnDetectedObject> result = OptimizeDetections(threshold, nmsThreshold, nms, classIds, confidences, probabilities, boxes); results[0] = result.ToArray(); return(results); }
/// <summary> /// 他の行列の部分行列として初期化 /// </summary> /// <param name="m">作成された行列に(全体的,部分的に)割り当てられる配列. /// これらのコンストラクタによってデータがコピーされる事はありません. /// 代わりに,データ m ,またはその部分配列を指し示すヘッダが作成され, /// 関連した参照カウンタがあれば,それがインクリメントされます. /// つまり,新しく作成された配列の内容を変更することで, m の対応する要素も /// 変更することになります.もし部分配列の独立したコピーが必要ならば, /// Mat.Clone() を利用してください.</param> /// <param name="rowRange">扱われる 行列の行の範囲.すべての行を扱う場合は,Range.All を利用してください.</param> /// <param name="colRange">扱われる 行列の列の範囲.すべての列を扱う場合は,Range.All を利用してください.</param> #else /// <summary> /// creates a matrix header for a part of the bigger matrix /// </summary> /// <param name="m">Array that (as a whole or partly) is assigned to the constructed matrix. /// No data is copied by these constructors. Instead, the header pointing to m data or its sub-array /// is constructed and associated with it. The reference counter, if any, is incremented. /// So, when you modify the matrix formed using such a constructor, you also modify the corresponding elements of m . /// If you want to have an independent copy of the sub-array, use Mat::clone() .</param> /// <param name="rowRange">Range of the m rows to take. As usual, the range start is inclusive and the range end is exclusive. /// Use Range.All to take all the rows.</param> /// <param name="colRange">Range of the m columns to take. Use Range.All to take all the columns.</param> #endif public MatOfDouble(MatOfDouble m, Range rowRange, Range?colRange = null) : base(m, rowRange, colRange) { }