本文整理汇总了C#中Mat.GetGenericIndexer方法的典型用法代码示例。如果您正苦于以下问题:C# Mat.GetGenericIndexer方法的具体用法?C# Mat.GetGenericIndexer怎么用?C# Mat.GetGenericIndexer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mat
的用法示例。
在下文中一共展示了Mat.GetGenericIndexer方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConvertImageSample
private static void ConvertImageSample()
{
var m = new Mat(100, 100, MatType.CV_32FC1);
var rand = new Random();
var indexer = m.GetGenericIndexer<float>();
for (int r = 0; r < m.Rows; r++)
{
for (int c = 0; c < m.Cols; c++)
{
indexer[r, c] = (float)rand.NextDouble();
}
}
//Window.ShowImages(m);
var conv = new Mat();
Cv2.ConvertImage(m, conv);
conv.SaveImage(@"C:\temp\float.png");
}
示例2: MatForEach
private static void MatForEach()
{
var img = new Mat("data/lenna.png", ImreadModes.Color);
var watch = Stopwatch.StartNew();
unsafe
{
img.ForEachAsVec3b((value, position) =>
{
value->Item0 = (byte)(255 - value->Item0);
value->Item1 = (byte)(255 - value->Item1);
value->Item2 = (byte)(255 - value->Item2);
});
}
watch.Stop();
Console.WriteLine("{0}ms", watch.ElapsedMilliseconds);
Window.ShowImages(img);
watch.Restart();
var indexer = img.GetGenericIndexer<Vec3b>();
int w = img.Width, h = img.Height;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
Vec3b v = indexer[y, x];
v.Item0 = (byte)(255 - v.Item0);
v.Item1 = (byte)(255 - v.Item1);
v.Item2 = (byte)(255 - v.Item2);
indexer[y, x] = v;
}
}
watch.Stop();
Console.WriteLine("{0}ms", watch.ElapsedMilliseconds);
Window.ShowImages(img);
}
示例3: PerformOne
/// <summary>
///
/// </summary>
/// <param name="labels"></param>
/// <param name="blob"></param>
/// <param name="imgSrc"></param>
/// <param name="imgDst"></param>
/// <param name="mode"></param>
/// <param name="color"></param>
/// <param name="alpha"></param>
public static unsafe void PerformOne(LabelData labels, CvBlob blob, Mat imgSrc, Mat imgDst,
RenderBlobsMode mode, Scalar color, double alpha)
{
if (labels == null)
throw new ArgumentNullException("labels");
if (blob == null)
throw new ArgumentNullException("blob");
if (imgSrc == null)
throw new ArgumentNullException("imgSrc");
if (imgDst == null)
throw new ArgumentNullException("imgDst");
if (imgDst.Type() != MatType.CV_8UC3)
throw new ArgumentException("'img' must be a 3-channel U8 image.");
if ((mode & RenderBlobsMode.Color) == RenderBlobsMode.Color)
{
var pSrc = imgSrc.GetGenericIndexer<Vec3b>();
var pDst = imgDst.GetGenericIndexer<Vec3b>();
for (int r = blob.MinY; r < blob.MaxY; r++)
{
for (int c = blob.MinX; c < blob.MaxX; c++)
{
if (labels[r, c] == blob.Label)
{
byte v0 = (byte) ((1.0 - alpha)*pSrc[r, c].Item0 + alpha*color.Val0);
byte v1 = (byte) ((1.0 - alpha)*pSrc[r, c].Item1 + alpha*color.Val1);
byte v2 = (byte) ((1.0 - alpha)*pSrc[r, c].Item2 + alpha*color.Val2);
pDst[r, c] = new Vec3b(v0, v1, v2);
}
}
}
}
if (mode != RenderBlobsMode.None)
{
if ((mode & RenderBlobsMode.BoundingBox) == RenderBlobsMode.BoundingBox)
{
Cv2.Rectangle(
imgDst,
new Point(blob.MinX, blob.MinY),
new Point(blob.MaxX - 1, blob.MaxY - 1),
new Scalar(255, 0, 0));
}
if ((mode & RenderBlobsMode.Angle) == RenderBlobsMode.Angle)
{
double angle = blob.Angle();
double lengthLine = Math.Max(blob.MaxX - blob.MinX, blob.MaxY - blob.MinY) / 2.0;
double x1 = blob.Centroid.X - lengthLine * Math.Cos(angle);
double y1 = blob.Centroid.Y - lengthLine * Math.Sin(angle);
double x2 = blob.Centroid.X + lengthLine * Math.Cos(angle);
double y2 = blob.Centroid.Y + lengthLine * Math.Sin(angle);
Cv2.Line(imgDst, new Point((int)x1, (int)y1), new Point((int)x2, (int)y2),
new Scalar(0, 255, 0));
}
if ((mode & RenderBlobsMode.Centroid) == RenderBlobsMode.Centroid)
{
Cv2.Line(imgDst,
new Point((int)blob.Centroid.X - 3, (int)blob.Centroid.Y),
new Point((int)blob.Centroid.X + 3, (int)blob.Centroid.Y),
new Scalar(255, 0, 0));
Cv2.Line(imgDst,
new Point((int)blob.Centroid.X, (int)blob.Centroid.Y - 3),
new Point((int)blob.Centroid.X, (int)blob.Centroid.Y + 3),
new Scalar(255, 0, 0));
}
}
}
示例4: Render
/// <summary>
/// Draw a contour.
/// </summary>
/// <param name="img">Image to draw on.</param>
/// <param name="color">Color to draw (default, white).</param>
public void Render(Mat img, Scalar color)
{
if (img == null)
throw new ArgumentNullException(nameof(img));
if (img.Type() != MatType.CV_8UC3)
throw new ArgumentException("Invalid img format (U8 3-channels)");
var indexer = img.GetGenericIndexer<Vec3b>();
int x = StartingPoint.X;
int y = StartingPoint.Y;
foreach (CvChainCode cc in ChainCode)
{
indexer[y, x] = new Vec3b((byte) color.Val0, (byte) color.Val1, (byte) color.Val2);
x += CvBlobConst.ChainCodeMoves[(int) cc][0];
y += CvBlobConst.ChainCodeMoves[(int) cc][1];
}
}
示例5: Torgerson
/// <summary>
/// Returns Torgerson's additive constant
/// </summary>
/// <param name="mat"></param>
/// <returns></returns>
private double Torgerson(Mat mat)
{
if (mat == null)
throw new ArgumentNullException();
if (mat.Rows != mat.Cols)
throw new ArgumentException();
int n = mat.Rows;
// Additive constant in case of negative value
double min, max;
Cv2.MinMaxLoc(-mat, out min, out max);
double c2 = max;
// Additive constant from triangular inequality
double c1 = 0;
var at = mat.GetGenericIndexer<double>();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
double v = at[i, k] - at[i, j] - at[j, k];
if (v > c1)
{
c1 = v;
}
}
}
}
return Math.Max(Math.Max(c1, c2), 0);
}