本文整理汇总了C#中Mat.At方法的典型用法代码示例。如果您正苦于以下问题:C# Mat.At方法的具体用法?C# Mat.At怎么用?C# Mat.At使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mat
的用法示例。
在下文中一共展示了Mat.At方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HDR
private static void HDR()
{
var hdr = CalibrateDebevec.Create();
Mat[] images = new Mat[3];
images[0] = Cv2.ImRead(@"data\lenna.png", ImreadModes.AnyColor);
images[1] = Cv2.ImRead(@"data\lenna.png", ImreadModes.AnyColor);
images[2] = Cv2.ImRead(@"data\lenna.png", ImreadModes.AnyColor);
float[] speeds = new float[3];
speeds[0] = 1;
speeds[1] = 1;
speeds[2] = 1;
Mat dst = new Mat();
hdr.Process(images, dst, speeds);
dst.ToString();
for (int i = 0; i < Math.Max(dst.Rows, dst.Cols); i++)
{
Console.WriteLine(dst.At<float>(i));
}
}
示例2: ByMat
/// <summary>
/// Solve equation AX = Y
/// </summary>
private void ByMat()
{
// x + y = 10
// 2x + 3y = 26
// (x=4, y=6)
double[,] av = {{1, 1},
{2, 3}};
double[] yv = {10, 26};
Mat a = new Mat(2, 2, MatType.CV_64FC1, av);
Mat y = new Mat(2, 1, MatType.CV_64FC1, yv);
Mat x = new Mat();
Cv2.Solve(a, y, x, MatrixDecomposition.LU);
Console.WriteLine("ByMat:");
Console.WriteLine("X1 = {0}, X2 = {1}", x.At<double>(0), x.At<double>(1));
}
示例3: ByMat
public void ByMat()
{
// x + y = 10
// 2x + 3y = 26
// (x=4, y=6)
double[,] av = {{1, 1},
{2, 3}};
double[] yv = { 10, 26 };
Mat a = new Mat(2, 2, MatType.CV_64FC1, av);
Mat y = new Mat(2, 1, MatType.CV_64FC1, yv);
Mat x = new Mat();
Cv2.Solve(a, y, x, DecompTypes.LU);
Console.WriteLine("X1 = {0}, X2 = {1}", x.At<double>(0), x.At<double>(1));
Assert.That(x.At<double>(0), Is.EqualTo(4).Within(1e-6));
Assert.That(x.At<double>(1), Is.EqualTo(6).Within(1e-6));
}
示例4: Histgram
public ActionResult Histgram(HttpPostedFileBase imageData)
{
try
{
if (imageData == null) { throw new ArgumentException("File is not exist."); }
using (var img = Mat.FromStream(imageData.InputStream, LoadMode.Color))
{
int chWidth = 260;
int chHeight = 200;
using (var mask = new Mat())
using (var hist = new Mat())
using (var histImg = new Mat(new Size(chWidth, chHeight), MatType.CV_8UC3, Scalar.All(255)))
{
Mat[] images = new Mat[] { img };
int[] channels = new int[] { 0 };
int[] hdims = new int[] { 256 };
float[] hranges = new float[] { 0, 256 };
float[][] ranges = new float[][] { hranges };
Cv2.CalcHist(images, channels, mask, hist, 1, hdims, ranges);
double minVal, maxVal;
Cv2.MinMaxLoc(hist, out minVal, out maxVal);
for (int j = 0; j < hdims[0]; ++j)
{
int binW = (int)((double)chWidth / hdims[0]);
Cv2.Rectangle(histImg, new Point(j * binW, histImg.Rows), new Point((j + 1) * binW, histImg.Rows - (int)(hist.At<float>(j) * (maxVal != 0 ? chHeight/ maxVal : 0.0))), Scalar.All(100));
}
byte[] imgBytes = img.ToBytes(".png");
string base64Img = Convert.ToBase64String(imgBytes);
ViewBag.Base64Img = base64Img;
byte[] result = histImg.ToBytes(".png");
string base64Result = Convert.ToBase64String(result);
ViewBag.Base64Result = base64Result;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return View();
}
示例5: example02
private static void example02()
{
var src = new Mat(@"..\..\Images\fruits.jpg", LoadMode.AnyDepth | LoadMode.AnyColor);
Cv2.ImShow("Source", src);
Cv2.WaitKey(1); // do events
Cv2.Blur(src, src, new Size(15, 15));
Cv2.ImShow("Blurred Image", src);
Cv2.WaitKey(1); // do events
// Converts the MxNx3 image into a Kx3 matrix where K=MxN and
// each row is now a vector in the 3-D space of RGB.
// change to a Mx3 column vector (M is number of pixels in image)
var columnVector = src.Reshape(cn: 3, rows: src.Rows * src.Cols);
// convert to floating point, it is a requirement of the k-means method of OpenCV.
var samples = new Mat();
columnVector.ConvertTo(samples, MatType.CV_32FC3);
for (var clustersCount = 2; clustersCount <= 8; clustersCount += 2)
{
var bestLabels = new Mat();
var centers = new Mat();
Cv2.Kmeans(
data: samples,
k: clustersCount,
bestLabels: bestLabels,
criteria:
new TermCriteria(type: CriteriaType.Epsilon | CriteriaType.Iteration, maxCount: 10, epsilon: 1.0),
attempts: 3,
flags: KMeansFlag.PpCenters,
centers: centers);
var clusteredImage = new Mat(src.Rows, src.Cols, src.Type());
for (var size = 0; size < src.Cols * src.Rows; size++)
{
var clusterIndex = bestLabels.At<int>(0, size);
var newPixel = new Vec3b
{
Item0 = (byte)(centers.At<float>(clusterIndex, 0)), // B
Item1 = (byte)(centers.At<float>(clusterIndex, 1)), // G
Item2 = (byte)(centers.At<float>(clusterIndex, 2)) // R
};
clusteredImage.Set(size / src.Cols, size % src.Cols, newPixel);
}
Cv2.ImShow(string.Format("Clustered Image [k:{0}]", clustersCount), clusteredImage);
Cv2.WaitKey(1); // do events
}
Cv2.WaitKey();
Cv2.DestroyAllWindows();
}
示例6: example01
/// <summary>
/// https://github.com/Itseez/opencv_extra/blob/master/learning_opencv_v2/ch13_ex13_1.cpp
/// </summary>
private static void example01()
{
using (var window = new Window("Clusters", flags: WindowMode.AutoSize | WindowMode.FreeRatio))
{
const int maxClusters = 5;
var rng = new RNG(state: (ulong)DateTime.Now.Ticks);
for (; ; )
{
var clustersCount = rng.Uniform(a: 2, b: maxClusters + 1);
var samplesCount = rng.Uniform(a: 1, b: 1001);
var points = new Mat(rows: samplesCount, cols: 1, type: MatType.CV_32FC2);
clustersCount = Math.Min(clustersCount, samplesCount);
var img = new Mat(rows: 500, cols: 500, type: MatType.CV_8UC3, s: Scalar.All(0));
// generate random sample from multi-gaussian distribution
for (var k = 0; k < clustersCount; k++)
{
var pointChunk = points.RowRange(
startRow: k * samplesCount / clustersCount,
endRow: (k == clustersCount - 1)
? samplesCount
: (k + 1) * samplesCount / clustersCount);
var center = new Point
{
X = rng.Uniform(a: 0, b: img.Cols),
Y = rng.Uniform(a: 0, b: img.Rows)
};
rng.Fill(
mat: pointChunk,
distType: DistributionType.Normal,
a: new Scalar(center.X, center.Y),
b: new Scalar(img.Cols * 0.05f, img.Rows * 0.05f));
}
Cv2.RandShuffle(dst: points, iterFactor: 1, rng: rng);
var labels = new Mat();
var centers = new Mat(rows: clustersCount, cols: 1, type: points.Type());
Cv2.Kmeans(
data: points,
k: clustersCount,
bestLabels: labels,
criteria: new TermCriteria(CriteriaType.Epsilon | CriteriaType.Iteration, 10, 1.0),
attempts: 3,
flags: KMeansFlag.PpCenters,
centers: centers);
Scalar[] colors =
{
new Scalar(0, 0, 255),
new Scalar(0, 255, 0),
new Scalar(255, 100, 100),
new Scalar(255, 0, 255),
new Scalar(0, 255, 255)
};
for (var i = 0; i < samplesCount; i++)
{
var clusterIdx = labels.At<int>(i);
Point ipt = points.At<Point2f>(i);
Cv2.Circle(
img: img,
center: ipt,
radius: 2,
color: colors[clusterIdx],
lineType: LineType.AntiAlias,
thickness: Cv.FILLED);
}
window.Image = img;
var key = (char)Cv2.WaitKey();
if (key == 27 || key == 'q' || key == 'Q') // 'ESC'
{
break;
}
}
}
}
示例7: watershedExample
//.........这里部分代码省略.........
var key = Cv2.WaitKey(0);
if ((char)key == 27) // ESC
{
break;
}
if ((char)key == 'r') // Reset
{
markerMask = new Mat(markerMask.Size(), markerMask.Type(), s: Scalar.All(0));
src.CopyTo(srcCopy);
sourceWindow.Image = srcCopy;
}
if ((char)key == 'w' || (char)key == ' ') // Apply watershed
{
Point[][] contours; //vector<vector<Point>> contours;
HiearchyIndex[] hierarchyIndexes; //vector<Vec4i> hierarchy;
Cv2.FindContours(
markerMask,
out contours,
out hierarchyIndexes,
mode: ContourRetrieval.CComp,
method: ContourChain.ApproxSimple);
if (contours.Length == 0)
{
continue;
}
var markers = new Mat(markerMask.Size(), MatType.CV_32S, s: Scalar.All(0));
var componentCount = 0;
var contourIndex = 0;
while ((contourIndex >= 0))
{
Cv2.DrawContours(
markers,
contours,
contourIndex,
color: Scalar.All(componentCount+1),
thickness: -1,
lineType: LineType.Link8,
hierarchy: hierarchyIndexes,
maxLevel: int.MaxValue);
componentCount++;
contourIndex = hierarchyIndexes[contourIndex].Next;
}
if (componentCount == 0)
{
continue;
}
var colorTable = new List<Vec3b>();
for (var i = 0; i < componentCount; i++)
{
var b = rnd.Next(0, 255); //Cv2.TheRNG().Uniform(0, 255);
var g = rnd.Next(0, 255); //Cv2.TheRNG().Uniform(0, 255);
var r = rnd.Next(0, 255); //Cv2.TheRNG().Uniform(0, 255);
colorTable.Add(new Vec3b((byte)b, (byte)g, (byte)r));
}
Cv2.Watershed(src, markers);
var watershedImage = new Mat(markers.Size(), MatType.CV_8UC3);
// paint the watershed image
for (var i = 0; i < markers.Rows; i++)
{
for (var j = 0; j < markers.Cols; j++)
{
var idx = markers.At<int>(i, j);
if (idx == -1)
{
watershedImage.Set(i, j, new Vec3b(255, 255, 255));
}
else if (idx <= 0 || idx > componentCount)
{
watershedImage.Set(i, j, new Vec3b(0, 0, 0));
}
else
{
watershedImage.Set(i, j, colorTable[idx - 1]);
}
}
}
watershedImage = watershedImage * 0.5 + imgGray * 0.5;
Cv2.ImShow("Watershed Transform", watershedImage);
Cv2.WaitKey(1); //do events
}
}
sourceWindow.Dispose();
Cv2.DestroyAllWindows();
src.Dispose();
}