本文整理汇总了C#中Mat.Get方法的典型用法代码示例。如果您正苦于以下问题:C# Mat.Get方法的具体用法?C# Mat.Get怎么用?C# Mat.Get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mat
的用法示例。
在下文中一共展示了Mat.Get方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: usingCppInterface1
private static void usingCppInterface1()
{
// Cv2.ImRead
using (var src = new Mat(@"..\..\Images\Penguin.Png", LoadMode.AnyDepth | LoadMode.AnyColor))
using (var dst = new Mat())
{
src.CopyTo(dst);
for (var y = 0; y < src.Height; y++)
{
for (var x = 0; x < src.Width; x++)
{
var pixel = src.Get<Vec3b>(y, x);
var newPixel = new Vec3b
{
Item0 = (byte)(255 - pixel.Item0), // B
Item1 = (byte)(255 - pixel.Item1), // G
Item2 = (byte)(255 - pixel.Item2) // R
};
dst.Set(y, x, newPixel);
}
}
// [Cpp] Accessing Pixel
// https://github.com/shimat/opencvsharp/wiki/%5BCpp%5D-Accessing-Pixel
//Cv2.NamedWindow();
//Cv2.ImShow();
using (new Window("C++ Interface: Src", image: src))
using (new Window("C++ Interface: Dst", image: dst))
{
Cv2.WaitKey(0);
}
}
}
示例2: Run
public void Run()
{
Console.WriteLine("===== FlannTest =====");
// creates data set
using (Mat features = new Mat(10000, 2, MatType.CV_32FC1))
{
Random rand = new Random();
for (int i = 0; i < features.Rows; i++)
{
features.Set<float>(i, 0, rand.Next(10000));
features.Set<float>(i, 1, rand.Next(10000));
}
// query
Point2f queryPoint = new Point2f(7777, 7777);
Mat queries = new Mat(1, 2, MatType.CV_32FC1);
queries.Set<float>(0, 0, queryPoint.X);
queries.Set<float>(0, 1, queryPoint.Y);
Console.WriteLine("query:({0}, {1})", queryPoint.X, queryPoint.Y);
Console.WriteLine("-----");
// knnSearch
using (Index nnIndex = new Index(features, new KDTreeIndexParams(4)))
{
const int Knn = 1;
int[] indices;
float[] dists;
nnIndex.KnnSearch(queries, out indices, out dists, Knn, new SearchParams(32));
for (int i = 0; i < Knn; i++)
{
int index = indices[i];
float dist = dists[i];
Point2f pt = new Point2f(features.Get<float>(index, 0), features.Get<float>(index, 1));
Console.Write("No.{0}\t", i);
Console.Write("index:{0}", index);
Console.Write(" distance:{0}", dist);
Console.Write(" data:({0}, {1})", pt.X, pt.Y);
Console.WriteLine();
}
Knn.ToString();
}
}
Console.Read();
}
示例3: ToByteArray
static byte[] ToByteArray(Mat m)
{
var result = new byte[m.Width * m.Height];
for (var y = 0; y < m.Height; ++y)
for(var x = 0; x < m.Width; ++x)
{
result[y * m.Width + x] = m.Get<byte>(x, y);
}
return result;
//var buffer = new byte[m.Width, m.Height];
//m.GetArray(m.Width, m.Height, buffer);
//var result = new byte[m.Width * m.Height];
//Buffer.BlockCopy(buffer, 0, result, 0, result.Length);
//return result;
}
示例4: Run
public void Run()
{
Mat src = Cv2.ImRead(FilePath.Lenna, LoadMode.GrayScale);
// Histogram view
const int Width = 260, Height = 200;
Mat render = new Mat(new Size(Width, Height), MatType.CV_8UC3, Scalar.All(255));
// Calculate histogram
Mat hist = new Mat();
int[] hdims = {256}; // Histogram size for each dimension
Rangef[] ranges = { new Rangef(0,256), }; // min/max
Cv2.CalcHist(
new Mat[]{src},
new int[]{0},
null,
hist,
1,
hdims,
ranges);
// Get the max value of histogram
double minVal, maxVal;
Cv2.MinMaxLoc(hist, out minVal, out maxVal);
Scalar color = Scalar.All(100);
// Scales and draws histogram
hist = hist * (maxVal != 0 ? Height / maxVal : 0.0);
for (int j = 0; j < hdims[0]; ++j)
{
int binW = (int)((double)Width / hdims[0]);
render.Rectangle(
new Point(j * binW, render.Rows),
new Point((j + 1) * binW, render.Rows - (int)(hist.Get<float>(j))),
color,
-1);
}
using (new Window("Image", WindowMode.AutoSize | WindowMode.FreeRatio, src))
using (new Window("Histogram", WindowMode.AutoSize | WindowMode.FreeRatio, render))
{
Cv.WaitKey();
}
}
示例5: calculateHistogram1
private static void calculateHistogram1(Window histogramWindow, Mat src, Mat modifiedSrc)
{
const int histogramSize = 64;//from 0 to 63
int[] dimensions = { histogramSize }; // Histogram size for each dimension
Rangef[] ranges = { new Rangef(0, histogramSize) }; // min/max
using (var histogram = new Mat())
{
Cv2.CalcHist(
images: new[] { modifiedSrc },
channels: new[] { 0 }, //The channel (dim) to be measured. In this case it is just the intensity (each array is single-channel) so we just write 0.
mask: null,
hist: histogram,
dims: 1, //The histogram dimensionality.
histSize: dimensions,
ranges: ranges);
using (var histogramImage = (Mat)(Mat.Ones(rows: src.Rows, cols: src.Cols, type: MatType.CV_8U) * 255))
{
// Scales and draws histogram
Cv2.Normalize(histogram, histogram, 0, histogramImage.Rows, NormType.MinMax);
var binW = Cv.Round((double)histogramImage.Cols / histogramSize);
var color = Scalar.All(100);
for (var i = 0; i < histogramSize; i++)
{
Cv2.Rectangle(histogramImage,
new Point(i * binW, histogramImage.Rows),
new Point((i + 1) * binW, histogramImage.Rows - Cv.Round(histogram.Get<float>(i))),
color,
-1);
}
histogramWindow.Image = histogramImage;
}
}
}
示例6: CreateHistogram
public static Mat CreateHistogram(Mat grayScaleMat, int width = 260, int height = 200)
{
// Histogram view
Mat render = new Mat(new OpenCvSharp.CPlusPlus.Size(width, height), MatType.CV_8UC3, Scalar.All(255));
// Calculate histogram
Mat hist = new Mat();
int[] hdims = { 256 }; // Histogram size for each dimension
Rangef[] ranges = { new Rangef(0, 256), }; // min/max
Cv2.CalcHist(
new Mat[] { grayScaleMat },
new int[] { 0 },
null,
hist,
1,
hdims,
ranges);
// Get the max value of histogram
double minVal, maxVal;
Cv2.MinMaxLoc(hist, out minVal, out maxVal);
Scalar color = Scalar.All(100);
// Scales and draws histogram
hist = hist * (maxVal != 0 ? height / maxVal : 0.0);
for (int j = 0; j < hdims[0]; ++j)
{
int binW = (int)((double)width / hdims[0]);
render.Rectangle(
new OpenCvSharp.CPlusPlus.Point(j * binW, render.Rows),
new OpenCvSharp.CPlusPlus.Point((j + 1) * binW, render.Rows - (int)(hist.Get<float>(j))),
color,
-1);
}
return render;
}
示例7: Run
/// <summary>
/// Classical Multidimensional Scaling
/// </summary>
public void Run()
{
// creates distance matrix
int size = CityDistance.GetLength(0);
Mat t = new Mat(size, size, MatType.CV_64FC1, CityDistance);
// adds Torgerson's additive constant to t
double torgarson = Torgerson(t);
t += torgarson;
// squares all elements of t
t = t.Mul(t);
// centering matrix G
Mat g = CenteringMatrix(size);
// calculates inner product matrix B
Mat b = g * t * g.T() * -0.5;
// calculates eigenvalues and eigenvectors of B
Mat values = new Mat();
Mat vectors = new Mat();
Cv2.Eigen(b, values, vectors);
for (int r = 0; r < values.Rows; r++)
{
if (values.Get<double>(r) < 0)
values.Set<double>(r, 0);
}
//Console.WriteLine(values.Dump());
// multiplies sqrt(eigenvalue) by eigenvector
Mat result = vectors.RowRange(0, 2);
{
var at = result.GetGenericIndexer<double>();
for (int r = 0; r < result.Rows; r++)
{
for (int c = 0; c < result.Cols; c++)
{
at[r, c] *= Math.Sqrt(values.Get<double>(r));
}
}
}
// scaling
Cv2.Normalize(result, result, 0, 800, NormType.MinMax);
// opens a window
using (Mat img = Mat.Zeros(600, 800, MatType.CV_8UC3))
using (Window window = new Window("City Location Estimation"))
{
var at = result.GetGenericIndexer<double>();
for (int c = 0; c < size; c++)
{
double x = at[0, c];
double y = at[1, c];
x = x * 0.7 + img.Width * 0.1;
y = y * 0.7 + img.Height * 0.1;
img.Circle((int)x, (int)y, 5, CvColor.Red, -1);
Point textPos = new Point(x + 5, y + 10);
img.PutText(CityNames[c], textPos, FontFace.HersheySimplex, 0.5, CvColor.White);
}
window.Image = img;
Cv2.WaitKey();
}
}
示例8: Update
// Update is called once per frame
void Update()
{
if (runCalibration)
{
if (Input.GetMouseButton(0) || Input.GetMouseButton(1) || Input.GetMouseButton(2))
{
if (Input.GetMouseButton(0))
{
//Debug.Log(Input.mousePosition);
GameObject bc = GameObject.FindGameObjectWithTag("BlueCross");
bc.transform.localPosition = new Vector3(Map(Input.mousePosition.x, Screen.width / 2.0f - 320.0f, Screen.width / 2.0f + 320.0f, 0.0f, 640.0f) - 320.0f, -Map(Input.mousePosition.y, Screen.height / 2.0f + 240.0f, Screen.height / 2.0f - 240.0f, 0.0f, 480.0f) + 240.0f, 0.0f);
}
else if (Input.GetMouseButton(1))
{
GameObject yc = GameObject.FindGameObjectWithTag("YellowCross");
yc.transform.localPosition = new Vector3(Map(Input.mousePosition.x, Screen.width / 2.0f - 320.0f, Screen.width / 2.0f + 320.0f, 0.0f, 640.0f) - 320.0f, -Map(Input.mousePosition.y, Screen.height / 2.0f + 240.0f, Screen.height / 2.0f - 240.0f, 0.0f, 480.0f) + 240.0f, 0.0f);
nextBt = true;
}
else if (Input.GetMouseButton(2) && nextBt == true)
{
if (addKinectPoint())
{
addProjectorPoint();
Debug.Log("Point Added! -> (" + kinectCoordinates.Count + ") ");
nextBt = false;
}
else
{
Debug.Log("Kinect Point out of bounds!");
}
}
}
if (Input.GetKeyDown(KeyCode.A))
{
//PointerEventData pointer = new PointerEventData(EventSystem.current);
//pointer.position = Input.mousePosition;
//List<RaycastResult> raycastResults = new List<RaycastResult>();
//EventSystem.current.RaycastAll(pointer, raycastResults);
if (addKinectPoint())
{
addProjectorPoint();
Debug.Log("Point Added! -> " + kinectCoordinates.Count);
}
else
{
Debug.Log("Kinect Point out of bounds!");
}
}
if (Input.GetKeyDown(KeyCode.S))
{
if (kinectCoordinates.Count >= 8)
{
Debug.Log("Starting Calibration...");
findTransformation(kinectCoordinates, projectorCoordinates);
foundResult = true;
}
else
{
Debug.Log("Not Enough Points!");
}
}
if (Input.GetKeyDown(KeyCode.D) && foundResult == true)
{
showResult = !showResult;
if (!showResult)
{
screenTx.SetPixels32(resetPixels);
screenTx.Apply(false);
}
Debug.Log("Show result toggle: " + showResult);
}
if (Input.GetKeyDown(KeyCode.F) && foundResult == true)
{
using (CvFileStorage fs = new CvFileStorage("KinectCalibration.xml", null, FileStorageMode.Write))
{
string nodeName = "calibResult";
fs.Write(nodeName, result.ToCvMat());
nodeName = "kinectPoints";
Mat kinectPts = new Mat(1, kinectCoordinates.Count, MatType.CV_64FC3);
for (int i = 0; i < kinectCoordinates.Count; i++)
{
kinectPts.Set<CvPoint3D64f>(0, i, (CvPoint3D64f)kinectCoordinates[i]);
}
fs.Write(nodeName, kinectPts.ToCvMat());
nodeName = "projectorPoints";
Mat projPts = new Mat(1, projectorCoordinates.Count, MatType.CV_64FC2);
for (int i = 0; i < projectorCoordinates.Count; i++)
{
projPts.Set<CvPoint2D64f>(0, i, (CvPoint2D64f)projectorCoordinates[i]);
}
fs.Write(nodeName, projPts.ToCvMat());
fs.Dispose();
}
Debug.Log("Calib Data saved!");
}
if (Input.GetKeyDown(KeyCode.Q))
{
delLastPoints();
//.........这里部分代码省略.........
示例9: Start
// Use this for initialization
void Start()
{
if (runCalibration)
{
Application.RegisterLogCallback(HandleLog);
scTex = new Texture2D(KinectWrapper.GetDepthWidth(), KinectWrapper.GetDepthHeight(), TextureFormat.RGBA32, false);
screenTx = new Texture2D(Screen.width, Screen.height, TextureFormat.RGBA32, false);
resetPixels = new Color32[Screen.width * Screen.height];
for (int i = 0; i < resetPixels.Length; i++)
{
Color32 cCol = new Color32(0, 0, 0, 0);
resetPixels[i] = cCol;
}
screenTx.SetPixels32(resetPixels);
screenTx.Apply(false);
GameObject.FindGameObjectWithTag("Restex").GetComponent<RawImage>().texture = screenTx;
}
foundCoordinatesMatrix = new Mat();
rightSideMatrix = new Mat();
//result = new Mat(11,1,OpenCvSharp.CPlusPlus.MatType.CV_64FC1);
using (CvFileStorage fs = new CvFileStorage("KinectCalibration.xml", null, FileStorageMode.Read))
{
string nodeName = "projectorPoints";
CvFileNode param = fs.GetFileNodeByName(null, nodeName);
Mat pPts = new Mat(fs.Read<CvMat>(param), true);
for (int i = 0; i < pPts.Cols; i++)
{
projectorCoordinates.Add(pPts.Get<CvPoint2D64f>(0, i));
}
nodeName = "kinectPoints";
param = fs.GetFileNodeByName(null, nodeName);
Mat kPts = new Mat(fs.Read<CvMat>(param), true);
for (int i = 0; i < pPts.Cols; i++)
{
kinectCoordinates.Add(kPts.Get<CvPoint3D64f>(0, i));
}
nodeName = "calibResult";
param = fs.GetFileNodeByName(null, nodeName);
result = new Mat(fs.Read<CvMat>(param), true);
fs.Dispose();
}
if (result.Rows > 1)
{
foundResult = true;
Debug.Log("Calib Loaded: " + result.Get<double>(0, 0).ToString() + " " + result.Get<double>(0, 1) + " " + result.Get<double>(0, 2) + " " + result.Get<double>(0, 3) + " " + result.Get<double>(0, 4) + " " + result.Get<double>(0, 5) + " " + result.Get<double>(0, 6) + " " + result.Get<double>(0, 7) + " " + result.Get<double>(0, 8) + " " + result.Get<double>(0, 9) + " " + result.Get<double>(0, 10) + "--" + kinectCoordinates.Count + " " + projectorCoordinates.Count);
}
Debug.Log(Screen.width + " " + Screen.height);
}
示例10: DispersionColor
static int DispersionColor(Mat mat, Point p, int radius)
{
int? min = null;
int? max = null;
for (var x = p.X - radius; x < p.X + radius; x++)
for (var y = p.Y - radius; y < p.Y + radius; y++)
{
var v = mat.Get<byte>(y, x);
if (min == null || v < min.Value)
min = v;
if (max == null || max.Value < v)
max = v;
}
return max.Value - min.Value;
}
示例11: AverageColor
static int AverageColor(Mat mat, Point p, int radius)
{
var sum = 0;
var count = 0;
for (var x = p.X - radius; x < p.X + radius; x++)
for (var y = p.Y - radius; y < p.Y + radius; y++)
{
sum += mat.Get<byte>(y, x);
count++;
}
return sum / count;
}