public void CreateByChannels() { Assert.Equal(MatType.CV_8UC1, MatType.CV_8UC(1)); Assert.Equal(MatType.CV_8UC2, MatType.CV_8UC(2)); Assert.Equal(MatType.CV_8UC3, MatType.CV_8UC(3)); Assert.Equal(MatType.CV_8UC4, MatType.CV_8UC(4)); Assert.Equal(MatType.CV_8SC1, MatType.CV_8SC(1)); Assert.Equal(MatType.CV_8SC2, MatType.CV_8SC(2)); Assert.Equal(MatType.CV_8SC3, MatType.CV_8SC(3)); Assert.Equal(MatType.CV_8SC4, MatType.CV_8SC(4)); Assert.Equal(MatType.CV_16UC1, MatType.CV_16UC(1)); Assert.Equal(MatType.CV_16UC2, MatType.CV_16UC(2)); Assert.Equal(MatType.CV_16UC3, MatType.CV_16UC(3)); Assert.Equal(MatType.CV_16UC4, MatType.CV_16UC(4)); Assert.Equal(MatType.CV_16SC1, MatType.CV_16SC(1)); Assert.Equal(MatType.CV_16SC2, MatType.CV_16SC(2)); Assert.Equal(MatType.CV_16SC3, MatType.CV_16SC(3)); Assert.Equal(MatType.CV_16SC4, MatType.CV_16SC(4)); Assert.Equal(MatType.CV_32SC1, MatType.CV_32SC(1)); Assert.Equal(MatType.CV_32SC2, MatType.CV_32SC(2)); Assert.Equal(MatType.CV_32SC3, MatType.CV_32SC(3)); Assert.Equal(MatType.CV_32SC4, MatType.CV_32SC(4)); Assert.Equal(MatType.CV_32FC1, MatType.CV_32FC(1)); Assert.Equal(MatType.CV_32FC2, MatType.CV_32FC(2)); Assert.Equal(MatType.CV_32FC3, MatType.CV_32FC(3)); Assert.Equal(MatType.CV_32FC4, MatType.CV_32FC(4)); Assert.Equal(MatType.CV_64FC1, MatType.CV_64FC(1)); Assert.Equal(MatType.CV_64FC2, MatType.CV_64FC(2)); Assert.Equal(MatType.CV_64FC3, MatType.CV_64FC(3)); Assert.Equal(MatType.CV_64FC4, MatType.CV_64FC(4)); }
private static Mat AudioFrameToMat(AudioFrame frame) { int planar = ffmpeg.av_sample_fmt_is_planar((AVSampleFormat)frame.AVFrame.format); int planes = planar != 0 ? frame.AVFrame.channels : 1; int block_align = ffmpeg.av_get_bytes_per_sample((AVSampleFormat)frame.AVFrame.format) * (planar != 0 ? 1 : frame.AVFrame.channels); int stride = frame.AVFrame.nb_samples * block_align; int channels = planar != 0 ? 1 : frame.AVFrame.channels; MatType dstType; switch ((AVSampleFormat)frame.AVFrame.format) { case AVSampleFormat.AV_SAMPLE_FMT_U8: case AVSampleFormat.AV_SAMPLE_FMT_U8P: dstType = MatType.CV_8UC(channels); break; case AVSampleFormat.AV_SAMPLE_FMT_S16: case AVSampleFormat.AV_SAMPLE_FMT_S16P: dstType = MatType.CV_16SC(channels); break; case AVSampleFormat.AV_SAMPLE_FMT_S32: case AVSampleFormat.AV_SAMPLE_FMT_S32P: dstType = MatType.CV_32SC(channels); break; case AVSampleFormat.AV_SAMPLE_FMT_FLT: case AVSampleFormat.AV_SAMPLE_FMT_FLTP: dstType = MatType.CV_32FC(channels); break; case AVSampleFormat.AV_SAMPLE_FMT_DBL: case AVSampleFormat.AV_SAMPLE_FMT_DBLP: // opencv not have 64S, use 64F case AVSampleFormat.AV_SAMPLE_FMT_S64: case AVSampleFormat.AV_SAMPLE_FMT_S64P: dstType = MatType.CV_64FC(channels); break; default: throw new FFmpegException(FFmpegException.NotSupportFormat); } Mat mat = new Mat(planes, frame.AVFrame.nb_samples, dstType); for (int i = 0; i < planes; i++) { FFmpegHelper.CopyMemory(mat.Data + i * stride, frame.Data[i], (uint)stride); } return(mat); }
/// <summary> /// 元画像から指定された範囲とテンプレート画像とをテンプレートマッチングします。 /// </summary> /// <param name="src">元画像</param> /// <param name="template">テンプレート画像</param> /// <param name="partialArea">テンプレートマッチングしたい範囲</param> /// <returns>類似度</returns> private static double MatchTemplate(Mat src, Mat template, Rect partialArea) { using (var part = src.Clone(new OpenCvSharp.Rect(partialArea.X - 5, partialArea.Y - 5, partialArea.W + 5, partialArea.H + 5))) { var dstSize = new Size(part.Width - template.Width + 1, part.Height - template.Height + 1); using (var dst = new Mat(dstSize, MatType.CV_32FC(1))) { Cv2.MatchTemplate(part, template, dst, TemplateMatchModes.CCoeffNormed); double minVal, maxVal; Cv2.MinMaxLoc(dst, out minVal, out maxVal); return(maxVal); } } }
public void MakeType() { Assert.Equal(MatType.CV_8UC(1), MatType.MakeType(MatType.CV_8U, 1)); Assert.Equal(MatType.CV_8UC(2), MatType.MakeType(MatType.CV_8U, 2)); Assert.Equal(MatType.CV_8UC(3), MatType.MakeType(MatType.CV_8U, 3)); Assert.Equal(MatType.CV_8UC(4), MatType.MakeType(MatType.CV_8U, 4)); Assert.Equal(MatType.CV_8UC(5), MatType.MakeType(MatType.CV_8U, 5)); Assert.Equal(MatType.CV_8UC(6), MatType.MakeType(MatType.CV_8U, 6)); Assert.Equal(MatType.CV_8SC(1), MatType.MakeType(MatType.CV_8S, 1)); Assert.Equal(MatType.CV_8SC(2), MatType.MakeType(MatType.CV_8S, 2)); Assert.Equal(MatType.CV_8SC(3), MatType.MakeType(MatType.CV_8S, 3)); Assert.Equal(MatType.CV_8SC(4), MatType.MakeType(MatType.CV_8S, 4)); Assert.Equal(MatType.CV_8SC(5), MatType.MakeType(MatType.CV_8S, 5)); Assert.Equal(MatType.CV_8SC(6), MatType.MakeType(MatType.CV_8S, 6)); Assert.Equal(MatType.CV_16UC(1), MatType.MakeType(MatType.CV_16U, 1)); Assert.Equal(MatType.CV_16UC(2), MatType.MakeType(MatType.CV_16U, 2)); Assert.Equal(MatType.CV_16UC(3), MatType.MakeType(MatType.CV_16U, 3)); Assert.Equal(MatType.CV_16UC(4), MatType.MakeType(MatType.CV_16U, 4)); Assert.Equal(MatType.CV_16UC(5), MatType.MakeType(MatType.CV_16U, 5)); Assert.Equal(MatType.CV_16UC(6), MatType.MakeType(MatType.CV_16U, 6)); Assert.Equal(MatType.CV_16SC(1), MatType.MakeType(MatType.CV_16S, 1)); Assert.Equal(MatType.CV_16SC(2), MatType.MakeType(MatType.CV_16S, 2)); Assert.Equal(MatType.CV_16SC(3), MatType.MakeType(MatType.CV_16S, 3)); Assert.Equal(MatType.CV_16SC(4), MatType.MakeType(MatType.CV_16S, 4)); Assert.Equal(MatType.CV_16SC(5), MatType.MakeType(MatType.CV_16S, 5)); Assert.Equal(MatType.CV_16SC(6), MatType.MakeType(MatType.CV_16S, 6)); Assert.Equal(MatType.CV_32SC(1), MatType.MakeType(MatType.CV_32S, 1)); Assert.Equal(MatType.CV_32SC(2), MatType.MakeType(MatType.CV_32S, 2)); Assert.Equal(MatType.CV_32SC(3), MatType.MakeType(MatType.CV_32S, 3)); Assert.Equal(MatType.CV_32SC(4), MatType.MakeType(MatType.CV_32S, 4)); Assert.Equal(MatType.CV_32SC(5), MatType.MakeType(MatType.CV_32S, 5)); Assert.Equal(MatType.CV_32SC(6), MatType.MakeType(MatType.CV_32S, 6)); Assert.Equal(MatType.CV_32FC(1), MatType.MakeType(MatType.CV_32F, 1)); Assert.Equal(MatType.CV_32FC(2), MatType.MakeType(MatType.CV_32F, 2)); Assert.Equal(MatType.CV_32FC(3), MatType.MakeType(MatType.CV_32F, 3)); Assert.Equal(MatType.CV_32FC(4), MatType.MakeType(MatType.CV_32F, 4)); Assert.Equal(MatType.CV_32FC(5), MatType.MakeType(MatType.CV_32F, 5)); Assert.Equal(MatType.CV_32FC(6), MatType.MakeType(MatType.CV_32F, 6)); Assert.Equal(MatType.CV_64FC(1), MatType.MakeType(MatType.CV_64F, 1)); Assert.Equal(MatType.CV_64FC(2), MatType.MakeType(MatType.CV_64F, 2)); Assert.Equal(MatType.CV_64FC(3), MatType.MakeType(MatType.CV_64F, 3)); Assert.Equal(MatType.CV_64FC(4), MatType.MakeType(MatType.CV_64F, 4)); Assert.Equal(MatType.CV_64FC(5), MatType.MakeType(MatType.CV_64F, 5)); Assert.Equal(MatType.CV_64FC(6), MatType.MakeType(MatType.CV_64F, 6)); }
/// <summary> /// Convert NdArray to Mat /// </summary> /// <param name="array">NumSharp NDArray object</param> /// <returns>mmat object</returns> public static Mat ToMat(this NDArray array) { Shape shape = array.Shape; int dim = shape.NDim; int row = 0; int col = 0; int channel = 0; switch (dim) { case 2: row = shape[0]; col = shape[1]; channel = 1; break; case 3: row = shape[0]; col = shape[1]; channel = shape[2]; break; default: throw new Exception("Not Support Dim!");; } if (channel > 4) { throw new Exception("Not Support Channel Count!");; } MatType matType; Mat mat; switch (array.typecode) { case NPTypeCode.Byte: matType = MatType.CV_8UC(channel); byte[] data = array.ToByteArray(); mat = new Mat(row, col, matType, data); break; case NPTypeCode.Int16: matType = MatType.CV_16SC(channel); short[] shortData = array.ToArray <short>(); mat = new Mat(row, col, matType, shortData); break; case NPTypeCode.UInt16: matType = MatType.CV_16UC(channel); ushort[] ushortData = array.ToArray <ushort>(); mat = new Mat(row, col, matType, ushortData); break; case NPTypeCode.Int32: matType = MatType.CV_32SC(channel); int[] intData = array.ToArray <int>(); mat = new Mat(row, col, matType, intData); break; case NPTypeCode.Single: matType = MatType.CV_32FC(channel); float[] floatData = array.ToArray <float>(); mat = new Mat(row, col, matType, floatData); break; case NPTypeCode.Double: matType = MatType.CV_64FC(channel); double[] doubleData = array.ToArray <double>(); mat = new Mat(row, col, matType, doubleData); break; case NPTypeCode.Decimal: case NPTypeCode.String: case NPTypeCode.Complex: case NPTypeCode.Empty: case NPTypeCode.Boolean: case NPTypeCode.Char: case NPTypeCode.UInt32: case NPTypeCode.Int64: case NPTypeCode.UInt64: default: throw new ArgumentOutOfRangeException(); } return(mat); }