本文整理汇总了C#中Mat.Create方法的典型用法代码示例。如果您正苦于以下问题:C# Mat.Create方法的具体用法?C# Mat.Create怎么用?C# Mat.Create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mat
的用法示例。
在下文中一共展示了Mat.Create方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Niblack
/// <summary>
/// Niblackの手法による二値化処理を行う。
/// </summary>
/// <param name="imgSrc">入力画像</param>
/// <param name="imgDst">出力画像</param>
/// <param name="kernelSize">局所領域のサイズ</param>
/// <param name="k">係数</param>
#else
/// <summary>
/// Binarizes by Niblack's method
/// </summary>
/// <param name="src">Input image</param>
/// <param name="dst">Output image</param>
/// <param name="kernelSize">Window size</param>
/// <param name="k">Adequate coefficient</param>
#endif
public static void Niblack(Mat src, Mat dst, int kernelSize, double k)
{
if (src == null)
throw new ArgumentNullException("src");
if (dst == null)
throw new ArgumentNullException("dst");
// グレースケールのみ
if (src.Type() != MatType.CV_8UC1)
throw new ArgumentException("src must be gray scale image");
if (dst.Type() != MatType.CV_8UC1)
throw new ArgumentException("dst must be gray scale image");
// サイズのチェック
if (kernelSize < 3)
throw new ArgumentOutOfRangeException("kernelSize", "size must be 3 and above");
if (kernelSize % 2 == 0)
throw new ArgumentOutOfRangeException("kernelSize", "size must be odd number");
int width = src.Width;
int height = src.Height;
dst.Create(src.Size(), src.Type());
using (var tSrcMat = new MatOfByte(src))
using (var tDstMat = new MatOfByte(dst))
{
var tSrc = tSrcMat.GetIndexer();
var tDst = tDstMat.GetIndexer();
//for (int y = 0; y < gray.Height; y++)
MyParallel.For(0, height, delegate(int y)
{
for (int x = 0; x < width; x++)
{
double m, s;
MeanStddev(src, x, y, kernelSize, out m, out s);
double threshold = m + k * s;
if (tSrc[y, x] < threshold)
tDst[y, x] = 0;
else
tDst[y, x] = 255;
}
}
);
}
}
示例2: NiblackFast
/// <summary>
/// Niblackの手法による二値化処理を行う(高速だが、メモリを多く消費するバージョン)。
/// </summary>
/// <param name="imgSrc">入力画像</param>
/// <param name="imgDst">出力画像</param>
/// <param name="kernelSize">局所領域のサイズ</param>
/// <param name="k">係数</param>
#else
/// <summary>
/// Binarizes by Niblack's method (This is faster but memory-hogging)
/// </summary>
/// <param name="src">Input image</param>
/// <param name="dst">Output image</param>
/// <param name="kernelSize">Window size</param>
/// <param name="k">Adequate coefficient</param>
#endif
public static void NiblackFast(Mat src, Mat dst, int kernelSize, double k)
{
if (src == null)
throw new ArgumentNullException("src");
if (dst == null)
throw new ArgumentNullException("dst");
// グレースケールのみ
if (src.Type() != MatType.CV_8UC1)
throw new ArgumentException("src must be gray scale image");
if (dst.Type() != MatType.CV_8UC1)
throw new ArgumentException("dst must be gray scale image");
// サイズのチェック
if (kernelSize < 3)
throw new ArgumentOutOfRangeException("kernelSize", "size must be 3 and above");
if (kernelSize % 2 == 0)
throw new ArgumentOutOfRangeException("kernelSize", "size must be odd number");
int borderSize = kernelSize / 2;
int width = src.Width;
int height = src.Height;
dst.Create(src.Size(), src.Type());
using (var tempMat = new Mat(height + (borderSize * 2), width + (borderSize * 2), src.Type()))
using (var sumMat = new Mat(tempMat.Height + 1, tempMat.Width + 1, MatType.CV_64FC1, 1))
using (var sqSumMat = new Mat(tempMat.Height + 1, tempMat.Width + 1, MatType.CV_64FC1, 1))
{
Cv2.CopyMakeBorder(src, tempMat, borderSize, borderSize, borderSize, borderSize, BorderTypes.Replicate, Scalar.All(0));
Cv2.Integral(tempMat, sumMat, sqSumMat);
using (var tSrcMat = new MatOfByte(src))
using (var tDstMat = new MatOfByte(dst))
using (var tSumMat = new MatOfDouble(sumMat))
using (var tSqSumMat = new MatOfDouble(sqSumMat))
{
var tSrc = tSrcMat.GetIndexer();
var tDst = tDstMat.GetIndexer();
var tSum = tSumMat.GetIndexer();
var tSqSum = tSqSumMat.GetIndexer();
int ylim = height + borderSize;
int xlim = width + borderSize;
int kernelPixels = kernelSize * kernelSize;
for (int y = borderSize; y < ylim; y++)
{
for (int x = borderSize; x < xlim; x++)
{
int x1 = x - borderSize;
int y1 = y - borderSize;
int x2 = x + borderSize + 1;
int y2 = y + borderSize + 1;
double sum = tSum[y2, x2] - tSum[y2, x1] - tSum[y1, x2] + tSum[y1, x1];
double sqsum = tSqSum[y2, x2] - tSqSum[y2, x1] - tSqSum[y1, x2] + tSqSum[y1, x1];
double mean = sum / kernelPixels;
double var = (sqsum / kernelPixels) - (mean * mean);
if (var < 0.0) var = 0.0;
double stddev = Math.Sqrt(var);
double threshold = mean + k * stddev;
if (tSrc[y - borderSize, x - borderSize] < threshold)
tDst[y - borderSize, x - borderSize] = 0;
else
tDst[y - borderSize, x - borderSize] = 255;
}
}
}
}
}
示例3: Bernsen
/// <summary>
/// Bernsenの手法による二値化処理を行う。
/// </summary>
/// <param name="imgSrc">入力画像</param>
/// <param name="imgDst">出力画像</param>
/// <param name="kernelSize">局所領域のサイズ</param>
/// <param name="constrastMin">この数値以下のコントラストの領域は背景領域とみなす</param>
/// <param name="bgThreshold">背景領域と見なされた領域に適用する閾値(背景領域以外では、適応的に閾値を求める)</param>
#else
/// <summary>
/// Binarizes by Bernsen's method
/// </summary>
/// <param name="src">Input image</param>
/// <param name="dst">Output image</param>
/// <param name="kernelSize">Window size</param>
/// <param name="constrastMin">Adequate coefficient</param>
/// <param name="bgThreshold">Adequate coefficient</param>
#endif
public static void Bernsen(Mat src, Mat dst, int kernelSize, byte constrastMin, byte bgThreshold)
{
if (src == null)
throw new ArgumentNullException("src");
if (dst == null)
throw new ArgumentNullException("dst");
// グレースケールのみ
if (src.Type() != MatType.CV_8UC1)
throw new ArgumentException("src must be gray scale image");
if (dst.Type() != MatType.CV_8UC1)
throw new ArgumentException("dst must be gray scale image");
// サイズのチェック
if (kernelSize < 3)
throw new ArgumentOutOfRangeException("kernelSize", "size must be 3 and above");
if (kernelSize % 2 == 0)
throw new ArgumentOutOfRangeException("kernelSize", "size must be odd number");
int width = src.Width;
int height = src.Height;
dst.Create(src.Size(), src.Type());
using (var tSrcMat = new MatOfByte(src))
using (var tDstMat = new MatOfByte(dst))
{
var tSrc = tSrcMat.GetIndexer();
var tDst = tDstMat.GetIndexer();
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
byte min, max;
MinMax(src, x, y, kernelSize, out min, out max);
int contrast = max - min;
byte threshold;
if (contrast < constrastMin)
threshold = bgThreshold;
else
threshold = (byte)((max + min) / 2);
if (tSrc[y, x] <= threshold)
tDst[y, x] = 0;
else
tDst[y, x] = 255;
}
}
}
}
示例4: TestMatCreate
public void TestMatCreate()
{
Mat m = new Mat();
m.Create(10, 12, CvEnum.DepthType.Cv8U, 1);
m.Create(18, 22, CvEnum.DepthType.Cv64F, 3);
}