本文整理汇总了C#中OpenCvSharp.Mat类的典型用法代码示例。如果您正苦于以下问题:C# Mat类的具体用法?C# Mat怎么用?C# Mat使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Mat类属于OpenCvSharp命名空间,在下文中一共展示了Mat类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Run
public void Run()
{
var capture = new VideoCapture();
capture.Set(CaptureProperty.FrameWidth, 640);
capture.Set(CaptureProperty.FrameHeight, 480);
capture.Open(-1);
if (!capture.IsOpened())
throw new Exception("capture initialization failed");
var fs = FrameSource.CreateCameraSource(-1);
var sr = SuperResolution.CreateBTVL1();
sr.SetInput(fs);
using (var normalWindow = new Window("normal"))
using (var srWindow = new Window("super resolution"))
{
var normalFrame = new Mat();
var srFrame = new Mat();
while (true)
{
capture.Read(normalFrame);
sr.NextFrame(srFrame);
if (normalFrame.Empty() || srFrame.Empty())
break;
normalWindow.ShowImage(normalFrame);
srWindow.ShowImage(srFrame);
Cv2.WaitKey(100);
}
}
}
示例2: Run
public void Run()
{
Mat src = new Mat("Data/Image/shapes.png", ImreadModes.Color);
Mat gray = src.CvtColor(ColorConversionCodes.BGR2GRAY);
Mat binary = gray.Threshold(0, 255, ThresholdTypes.Otsu | ThresholdTypes.Binary);
Mat labelView = src.EmptyClone();
Mat rectView = binary.CvtColor(ColorConversionCodes.GRAY2BGR);
ConnectedComponents cc = Cv2.ConnectedComponentsEx(binary);
if (cc.LabelCount <= 1)
return;
// draw labels
cc.RenderBlobs(labelView);
// draw bonding boxes except background
foreach (var blob in cc.Blobs.Skip(1))
{
rectView.Rectangle(blob.Rect, Scalar.Red);
}
// filter maximum blob
var maxBlob = cc.GetLargestBlob();
var filtered = new Mat();
cc.FilterByBlob(src, filtered, maxBlob);
using (new Window("src", src))
using (new Window("binary", binary))
using (new Window("labels", labelView))
using (new Window("bonding boxes", rectView))
using (new Window("maximum blob", filtered))
{
Cv2.WaitKey();
}
}
示例3: SubMat
/// <summary>
/// Submatrix operations
/// </summary>
private void SubMat()
{
Mat src = Cv2.ImRead(FilePath.Image.Lenna);
// Assign small image to mat
Mat small = new Mat();
Cv2.Resize(src, small, new Size(100, 100));
src[10, 110, 10, 110] = small;
src[370, 470, 400, 500] = small.T();
// ↑ This is same as the following:
//small.T().CopyTo(src[370, 470, 400, 500]);
// Get partial mat (similar to cvSetImageROI)
Mat part = src[200, 400, 200, 360];
// Invert partial pixel values
Cv2.BitwiseNot(part, part);
// Fill the region (50..100, 100..150) with color (128, 0, 0)
part = src.SubMat(50, 100, 400, 450);
part.SetTo(128);
using (new Window("SubMat", src))
{
Cv2.WaitKey();
}
}
示例4: SelectStitchingImages
private static Mat[] SelectStitchingImages(int width, int height, int count)
{
Mat source = new Mat(@"Data\Image\lenna.png", ImreadModes.Color);
Mat result = source.Clone();
var rand = new Random();
var mats = new List<Mat>();
for (int i = 0; i < count; i++)
{
int x1 = rand.Next(source.Cols - width);
int y1 = rand.Next(source.Rows - height);
int x2 = x1 + width;
int y2 = y1 + height;
result.Line(new Point(x1, y1), new Point(x1, y2), new Scalar(0, 0, 255));
result.Line(new Point(x1, y2), new Point(x2, y2), new Scalar(0, 0, 255));
result.Line(new Point(x2, y2), new Point(x2, y1), new Scalar(0, 0, 255));
result.Line(new Point(x2, y1), new Point(x1, y1), new Scalar(0, 0, 255));
Mat m = source[new Rect(x1, y1, width, height)];
mats.Add(m.Clone());
}
using (new Window(result))
{
Cv2.WaitKey();
}
return mats.ToArray();
}
示例5: Run
public void Run()
{
var gray = new Mat(FilePath.Image.Lenna, ImreadModes.GrayScale);
var dst = new Mat(FilePath.Image.Lenna, ImreadModes.Color);
BRISK brisk = BRISK.Create();
KeyPoint[] keypoints = brisk.Detect(gray);
if (keypoints != null)
{
var color = new Scalar(0, 255, 0);
foreach (KeyPoint kpt in keypoints)
{
float r = kpt.Size / 2;
Cv2.Circle(dst, kpt.Pt, (int)r, color);
Cv2.Line(dst,
new Point2f(kpt.Pt.X + r, kpt.Pt.Y + r),
new Point2f(kpt.Pt.X - r, kpt.Pt.Y - r),
color);
Cv2.Line(dst,
new Point2f(kpt.Pt.X - r, kpt.Pt.Y + r),
new Point2f(kpt.Pt.X + r, kpt.Pt.Y - r),
color);
}
}
using (new Window("BRISK features", dst))
{
Cv2.WaitKey();
}
}
示例6: brightness
public void brightness(ref Mat img, double 目標)
{
//中心近くの9ピクセルから輝度調整
int width = img.Width;
int height = img.Height;
int center_x = width / 5;
int center_y = height / 5;
double[] vals = new double[9];
double average = 0;
double diff = 0;
var indexer = new MatOfByte3(img).GetIndexer();
vals[0] = indexer[center_y - 10, center_x - 10].Item0; vals[3] = indexer[center_y - 10, center_x].Item0; vals[6] = indexer[center_y - 10, center_x + 10].Item0;
vals[1] = indexer[center_y, center_x - 10].Item0; vals[4] = indexer[center_y, center_x].Item0; vals[7] = indexer[center_y, center_x + 10].Item0;
vals[2] = indexer[center_y + 10, center_x - 10].Item0; vals[5] = indexer[center_y + 10, center_x].Item0; vals[8] = indexer[center_y + 10, center_x + 10].Item0;
for (int num = 0; num < 9; num++) average += vals[num];
average = average / 9.0;
diff = 目標 - average;
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
{
Vec3b color = indexer[y, x];
double val = color.Item0 + diff;
if (val > 255) color.Item0 = 255;
else if (val < 0) color.Item0 = 0;
else color.Item0 = (byte)val;
indexer[y, x] = color;
}
indexer = null;
}
示例7: Add
/// <summary>
/// Adds descriptors to a training set.
/// </summary>
/// <param name="descriptors">descriptors Descriptors to add to a training set. Each row of the descriptors matrix is a descriptor.
/// The training set is clustered using clustermethod to construct the vocabulary.</param>
public void Add(Mat descriptors)
{
if (descriptors == null)
throw new ArgumentNullException(nameof(descriptors));
NativeMethods.features2d_BOWTrainer_add(ptr, descriptors.CvPtr);
GC.KeepAlive(descriptors);
}
示例8: FilterByLabels
/// <summary>
/// Filter a image with the specified label values.
/// </summary>
/// <param name="src">Source image.</param>
/// <param name="dst">Destination image.</param>
/// <param name="labelValues">Label values.</param>
/// <returns>Filtered image.</returns>
public Mat FilterByLabels(Mat src, Mat dst, IEnumerable<int> labelValues)
{
if (src == null)
throw new ArgumentNullException("src");
if (dst == null)
throw new ArgumentNullException("dst");
if (labelValues == null)
throw new ArgumentNullException("labelValues");
int[] labelArray = EnumerableEx.ToArray(labelValues);
if (labelArray.Length == 0)
throw new ArgumentException("empty labelValues");
foreach (int labelValue in labelArray)
{
if (labelValue < 0 || labelValue >= LabelCount)
throw new ArgumentException("0 <= x < LabelCount");
}
// マスク用Matを用意し、Andで切り抜く
using (Mat mask = GetLabelMask(labelArray[0]))
{
for (int i = 1; i < labelArray.Length; i++)
{
using (var maskI = GetLabelMask(labelArray[i]))
{
Cv2.BitwiseOr(mask, maskI, mask);
}
}
src.CopyTo(dst, mask);
return dst;
}
}
示例9: DrawFeatures
private void DrawFeatures(string filename, IEnumerable<KeyPoint> keypoints)
{
using (var dst = new Mat(filename, ImreadModes.GrayScale))
{
int thickness = 2;
foreach (KeyPoint kp in keypoints)
{
float r = kp.Size/2;
Cv2.Circle(dst, kp.Pt, (int) r, Scalar.Red, thickness);
Cv2.Line(dst,
new Point2f(kp.Pt.X + r, kp.Pt.Y + r),
new Point2f(kp.Pt.X - r, kp.Pt.Y - r),
Scalar.Red, thickness);
Cv2.Line(dst,
new Point2f(kp.Pt.X - r, kp.Pt.Y - r),
new Point2f(kp.Pt.X + r, kp.Pt.Y + r),
Scalar.Red, thickness);
}
using (new Window("src w. keypoints", WindowMode.FreeRatio, dst))
{
Cv2.WaitKey();
}
}
}
示例10: DetectFace
/// <summary>
///
/// </summary>
/// <param name="cascade"></param>
/// <returns></returns>
private Mat DetectFace(CascadeClassifier cascade)
{
Mat result;
using (var src = new Mat(FilePath.Image.Yalta, ImreadModes.Color))
using (var gray = new Mat())
{
result = src.Clone();
Cv2.CvtColor(src, gray, ColorConversionCodes.BGR2GRAY);
// Detect faces
Rect[] faces = cascade.DetectMultiScale(
gray, 1.08, 2, HaarDetectionType.ScaleImage, new Size(30, 30));
// Render all detected faces
foreach (Rect face in faces)
{
var center = new Point
{
X = (int)(face.X + face.Width * 0.5),
Y = (int)(face.Y + face.Height * 0.5)
};
var axes = new Size
{
Width = (int)(face.Width * 0.5),
Height = (int)(face.Height * 0.5)
};
Cv2.Ellipse(result, center, axes, 0, 0, 360, new Scalar(255, 0, 255), 4);
}
}
return result;
}
示例11: Run
public void Run()
{
Mat src = new Mat(FilePath.Image.Girl, ImreadModes.Color);
Mat dst = new Mat(FilePath.Image.Lenna, ImreadModes.Color);
Mat src0 = src.Resize(dst.Size(), 0, 0, InterpolationFlags.Lanczos4);
Mat mask = Mat.Zeros(src0.Size(), MatType.CV_8UC3);
mask.Circle(200, 200, 100, Scalar.White, -1);
Mat blend1 = new Mat();
Mat blend2 = new Mat();
Mat blend3 = new Mat();
Cv2.SeamlessClone(
src0, dst, mask, new Point(260, 270), blend1,
SeamlessCloneMethods.NormalClone);
Cv2.SeamlessClone(
src0, dst, mask, new Point(260, 270), blend2,
SeamlessCloneMethods.MonochromeTransfer);
Cv2.SeamlessClone(
src0, dst, mask, new Point(260, 270), blend3,
SeamlessCloneMethods.MixedClone);
using (new Window("src", src0))
using (new Window("dst", dst))
using (new Window("mask", mask))
using (new Window("blend NormalClone", blend1))
using (new Window("blend MonochromeTransfer", blend2))
using (new Window("blend MixedClone", blend3))
{
Cv2.WaitKey();
}
}
示例12: ToBitmapGrayScale
public void ToBitmapGrayScale()
{
Mat img = new Mat(FilePath.Image.Lenna511, ImreadModes.GrayScale); // width % 4 != 0
Bitmap bitmap = BitmapConverter.ToBitmap(img);
// Bitmap bitmap = img.ToBitmap();
using (var form = new Form())
using (var pb = new PictureBox())
{
pb.Image = bitmap;
var size = new System.Drawing.Size(bitmap.Width, bitmap.Height);
pb.ClientSize = size;
form.ClientSize = size;
form.Controls.Add(pb);
form.KeyPreview = true;
form.KeyDown += (sender, args) =>
{
if (args.KeyCode.HasFlag(Keys.Enter))
((Form)sender).Close();
};
form.Text = "Grayscale Mat to Bitmap Test";
form.ShowDialog();
}
}
示例13: Run
public void Run()
{
var gray = new Mat(FilePath.Image.Lenna, ImreadModes.GrayScale);
var kaze = KAZE.Create();
var akaze = AKAZE.Create();
var kazeDescriptors = new Mat();
var akazeDescriptors = new Mat();
KeyPoint[] kazeKeyPoints = null, akazeKeyPoints = null;
var kazeTime = MeasureTime(() =>
kaze.DetectAndCompute(gray, null, out kazeKeyPoints, kazeDescriptors));
var akazeTime = MeasureTime(() =>
akaze.DetectAndCompute(gray, null, out akazeKeyPoints, akazeDescriptors));
var dstKaze = new Mat();
var dstAkaze = new Mat();
Cv2.DrawKeypoints(gray, kazeKeyPoints, dstKaze);
Cv2.DrawKeypoints(gray, akazeKeyPoints, dstAkaze);
using (new Window(String.Format("KAZE [{0:F2}ms]", kazeTime.TotalMilliseconds), dstKaze))
using (new Window(String.Format("AKAZE [{0:F2}ms]", akazeTime.TotalMilliseconds), dstAkaze))
{
Cv2.WaitKey();
}
}
示例14: Run
public void Run()
{
Mat src = new Mat(FilePath.Image.Fruits, ImreadModes.Color);
Mat normconv = new Mat(), recursFildered = new Mat();
Cv2.EdgePreservingFilter(src, normconv, EdgePreservingMethods.NormconvFilter);
Cv2.EdgePreservingFilter(src, recursFildered, EdgePreservingMethods.RecursFilter);
Mat detailEnhance = new Mat();
Cv2.DetailEnhance(src, detailEnhance);
Mat pencil1 = new Mat(), pencil2 = new Mat();
Cv2.PencilSketch(src, pencil1, pencil2);
Mat stylized = new Mat();
Cv2.Stylization(src, stylized);
using (new Window("src", src))
using (new Window("edgePreservingFilter - NormconvFilter", normconv))
using (new Window("edgePreservingFilter - RecursFilter", recursFildered))
using (new Window("detailEnhance", detailEnhance))
using (new Window("pencilSketch grayscale", pencil1))
using (new Window("pencilSketch color", pencil2))
using (new Window("stylized", stylized))
{
Cv2.WaitKey();
}
}
示例15: MatchBySurf
private void MatchBySurf(Mat src1, Mat src2)
{
var gray1 = new Mat();
var gray2 = new Mat();
Cv2.CvtColor(src1, gray1, ColorConversionCodes.BGR2GRAY);
Cv2.CvtColor(src2, gray2, ColorConversionCodes.BGR2GRAY);
var surf = SURF.Create(500, 4, 2, true);
// Detect the keypoints and generate their descriptors using SURF
KeyPoint[] keypoints1, keypoints2;
var descriptors1 = new MatOfFloat();
var descriptors2 = new MatOfFloat();
surf.Compute(gray1, null, out keypoints1, descriptors1);
surf.Compute(gray2, null, out keypoints2, descriptors2);
// Match descriptor vectors
var bfMatcher = new BFMatcher(NormTypes.L2, false);
var flannMatcher = new FlannBasedMatcher();
DMatch[] bfMatches = bfMatcher.Match(descriptors1, descriptors2);
DMatch[] flannMatches = flannMatcher.Match(descriptors1, descriptors2);
// Draw matches
var bfView = new Mat();
Cv2.DrawMatches(gray1, keypoints1, gray2, keypoints2, bfMatches, bfView);
var flannView = new Mat();
Cv2.DrawMatches(gray1, keypoints1, gray2, keypoints2, flannMatches, flannView);
using (new Window("SURF matching (by BFMather)", WindowMode.AutoSize, bfView))
using (new Window("SURF matching (by FlannBasedMatcher)", WindowMode.AutoSize, flannView))
{
Cv2.WaitKey();
}
}