本文整理汇总了C#中Mat.CopyTo方法的典型用法代码示例。如果您正苦于以下问题:C# Mat.CopyTo方法的具体用法?C# Mat.CopyTo怎么用?C# Mat.CopyTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mat
的用法示例。
在下文中一共展示了Mat.CopyTo方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: FourierTransform
public ActionResult FourierTransform(HttpPostedFileBase imageData)
{
try
{
if (imageData == null) { throw new ArgumentException("File is not exist."); }
using (var img = Mat.FromStream(imageData.InputStream, LoadMode.Color))
using(var padded = new Mat())
{
using (var result = new Mat())
{
byte[] imgBytes = img.ToBytes(".png");
string base64Img = Convert.ToBase64String(imgBytes);
ViewBag.Base64Img = base64Img;
int m = Cv2.GetOptimalDFTSize(img.Rows);
int n = Cv2.GetOptimalDFTSize(img.Cols);
Cv2.CopyMakeBorder(img, padded, 0, m - img.Rows, 0, n - img.Cols, BorderType.Constant, Scalar.All(0));
var planes = new Mat[]{};
var complexI = new Mat();
Cv2.Merge(planes, complexI);
Cv2.Dft(complexI, complexI);
// Compute the magnitude
planes = Cv2.Split(complexI);
var magI = new Mat();
Cv2.Magnitude(planes[0], planes[1], magI);
magI += Scalar.All(1);
Cv2.Log(magI, magI);
//magI = magI(Rect(0, 0, magI.Cols & -2, magI.Rows & -2));
int cx = magI.Cols / 2;
int cy = magI.Rows / 2;
var q0 = new Mat(magI, new Rect(0, 0, cx, cy));
var q1 = new Mat(magI, new Rect(cx, 0, cx, cy));
var q2 = new Mat(magI, new Rect(0, cy, cx, cy));
var q3 = new Mat(magI, new Rect(cx, cy, cx, cy));
var tmp = new Mat();
q0.CopyTo(tmp);
q3.CopyTo(q0);
tmp.CopyTo(q3);
q1.CopyTo(tmp);
q2.CopyTo(q1);
tmp.CopyTo(q2);
Cv2.Normalize(magI, magI, 0, 1, NormType.MinMax);
byte[] resultBytes = magI.ToBytes(".png");
string base64Result = Convert.ToBase64String(resultBytes);
ViewBag.Base64Result = base64Result;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return View();
}
示例3: Dft
public static void Dft(string path)
{
Mat img = Cv2.ImRead(path, LoadMode.GrayScale);
// expand input image to optimal size
Mat padded = new Mat();
int m = Cv2.GetOptimalDFTSize(img.Rows);
int n = Cv2.GetOptimalDFTSize(img.Cols); // on the border add zero values
Cv2.CopyMakeBorder(img, padded, 0, m - img.Rows, 0, n - img.Cols, BorderType.Constant, Scalar.All(0));
// Add to the expanded another plane with zeros
Mat paddedF32 = new Mat();
padded.ConvertTo(paddedF32, MatType.CV_32F);
Mat[] planes = { paddedF32, Mat.Zeros(padded.Size(), MatType.CV_32F) };
Mat complex = new Mat();
Cv2.Merge(planes, complex);
// this way the result may fit in the source matrix
Mat dft = new Mat();
Cv2.Dft(complex, dft);
// compute the magnitude and switch to logarithmic scale
// => log(1 + sqrt(Re(DFT(I))^2 + Im(DFT(I))^2))
Mat[] dftPlanes;
Cv2.Split(dft, out dftPlanes); // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
// planes[0] = magnitude
Mat magnitude = new Mat();
Cv2.Magnitude(dftPlanes[0], dftPlanes[1], magnitude);
magnitude += Scalar.All(1); // switch to logarithmic scale
Cv2.Log(magnitude, magnitude);
// crop the spectrum, if it has an odd number of rows or columns
Mat spectrum = magnitude[
new Rect(0, 0, magnitude.Cols & -2, magnitude.Rows & -2)];
// rearrange the quadrants of Fourier image so that the origin is at the image center
int cx = spectrum.Cols / 2;
int cy = spectrum.Rows / 2;
Mat q0 = new Mat(spectrum, new Rect(0, 0, cx, cy)); // Top-Left - Create a ROI per quadrant
Mat q1 = new Mat(spectrum, new Rect(cx, 0, cx, cy)); // Top-Right
Mat q2 = new Mat(spectrum, new Rect(0, cy, cx, cy)); // Bottom-Left
Mat q3 = new Mat(spectrum, new Rect(cx, cy, cx, cy)); // Bottom-Right
// swap quadrants (Top-Left with Bottom-Right)
Mat tmp = new Mat();
q0.CopyTo(tmp);
q3.CopyTo(q0);
tmp.CopyTo(q3);
// swap quadrant (Top-Right with Bottom-Left)
q1.CopyTo(tmp);
q2.CopyTo(q1);
tmp.CopyTo(q2);
// Transform the matrix with float values into a
Cv2.Normalize(spectrum, spectrum, 0, 1, NormType.MinMax);
// Show the result
Cv2.ImShow("Spectrum Magnitude", spectrum);
Cv2.WaitKey(0);
Cv2.DestroyAllWindows();
}
示例4: watershedExample
/// <summary>
/// https://github.com/Itseez/opencv_extra/blob/master/learning_opencv_v2/ch9_watershed.cpp
/// </summary>
private static void watershedExample()
{
var src = new Mat(@"..\..\Images\corridor.jpg", LoadMode.AnyDepth | LoadMode.AnyColor);
var srcCopy = new Mat();
src.CopyTo(srcCopy);
var markerMask = new Mat();
Cv2.CvtColor(srcCopy, markerMask, ColorConversion.BgrToGray);
var imgGray = new Mat();
Cv2.CvtColor(markerMask, imgGray, ColorConversion.GrayToBgr);
markerMask = new Mat(markerMask.Size(), markerMask.Type(), s: Scalar.All(0));
var sourceWindow = new Window("Source (Select areas by mouse and then press space)")
{
Image = srcCopy
};
var previousPoint = new Point(-1, -1);
sourceWindow.OnMouseCallback += (@event, x, y, flags) =>
{
if (x < 0 || x >= srcCopy.Cols || y < 0 || y >= srcCopy.Rows)
{
return;
}
if (@event == MouseEvent.LButtonUp || !flags.HasFlag(MouseEvent.FlagLButton))
{
previousPoint = new Point(-1, -1);
}
else if (@event == MouseEvent.LButtonDown)
{
previousPoint = new Point(x, y);
}
else if (@event == MouseEvent.MouseMove && flags.HasFlag(MouseEvent.FlagLButton))
{
var pt = new Point(x, y);
if (previousPoint.X < 0)
{
previousPoint = pt;
}
Cv2.Line(img: markerMask, pt1: previousPoint, pt2: pt, color: Scalar.All(255), thickness: 5);
Cv2.Line(img: srcCopy, pt1: previousPoint, pt2: pt, color: Scalar.All(255), thickness: 5);
previousPoint = pt;
sourceWindow.Image = srcCopy;
}
};
var rnd = new Random();
for (; ; )
{
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,
//.........这里部分代码省略.........
示例5: detectBarcode
private static string detectBarcode(string fileName, double thresh, bool debug = false, double rotation = 0)
{
Console.WriteLine("\nProcessing: {0}", fileName);
// load the image and convert it to grayscale
var image = new Mat(fileName);
if (rotation != 0)
{
rotateImage(image, image, rotation, 1);
}
if (debug)
{
Cv2.ImShow("Source", image);
Cv2.WaitKey(1); // do events
}
var gray = new Mat();
var channels = image.Channels();
if (channels > 1)
{
Cv2.CvtColor(image, gray, ColorConversion.BgrToGray);
}
else
{
image.CopyTo(gray);
}
// compute the Scharr gradient magnitude representation of the images
// in both the x and y direction
var gradX = new Mat();
Cv2.Sobel(gray, gradX, MatType.CV_32F, xorder: 1, yorder: 0, ksize: -1);
//Cv2.Scharr(gray, gradX, MatType.CV_32F, xorder: 1, yorder: 0);
var gradY = new Mat();
Cv2.Sobel(gray, gradY, MatType.CV_32F, xorder: 0, yorder: 1, ksize: -1);
//Cv2.Scharr(gray, gradY, MatType.CV_32F, xorder: 0, yorder: 1);
// subtract the y-gradient from the x-gradient
var gradient = new Mat();
Cv2.Subtract(gradX, gradY, gradient);
Cv2.ConvertScaleAbs(gradient, gradient);
if (debug)
{
Cv2.ImShow("Gradient", gradient);
Cv2.WaitKey(1); // do events
}
// blur and threshold the image
var blurred = new Mat();
Cv2.Blur(gradient, blurred, new Size(9, 9));
var threshImage = new Mat();
Cv2.Threshold(blurred, threshImage, thresh, 255, ThresholdType.Binary);
if (debug)
{
Cv2.ImShow("Thresh", threshImage);
Cv2.WaitKey(1); // do events
}
// construct a closing kernel and apply it to the thresholded image
var kernel = Cv2.GetStructuringElement(StructuringElementShape.Rect, new Size(21, 7));
var closed = new Mat();
Cv2.MorphologyEx(threshImage, closed, MorphologyOperation.Close, kernel);
if (debug)
{
Cv2.ImShow("Closed", closed);
Cv2.WaitKey(1); // do events
}
// perform a series of erosions and dilations
Cv2.Erode(closed, closed, null, iterations: 4);
Cv2.Dilate(closed, closed, null, iterations: 4);
if (debug)
{
Cv2.ImShow("Erode & Dilate", closed);
Cv2.WaitKey(1); // do events
}
//find the contours in the thresholded image, then sort the contours
//by their area, keeping only the largest one
Point[][] contours;
HiearchyIndex[] hierarchyIndexes;
Cv2.FindContours(
closed,
out contours,
out hierarchyIndexes,
mode: ContourRetrieval.CComp,
method: ContourChain.ApproxSimple);
if (contours.Length == 0)
{
throw new NotSupportedException("Couldn't find any object in the image.");
}
//.........这里部分代码省略.........
示例6: getBarcodeText
private static string getBarcodeText(Mat barcode)
{
// `ZXing.Net` needs a white space around the barcode
var barcodeWithWhiteSpace = new Mat(new Size(barcode.Width + 30, barcode.Height + 30), MatType.CV_8U, Scalar.White);
var drawingRect = new Rect(new Point(15, 15), new Size(barcode.Width, barcode.Height));
var roi = barcodeWithWhiteSpace[drawingRect];
barcode.CopyTo(roi);
Cv2.ImShow("Enhanced Barcode", barcodeWithWhiteSpace);
Cv2.WaitKey(1); // do events
return decodeBarcodeText(barcodeWithWhiteSpace.ToBitmap());
}