本文整理汇总了C#中Mat.ToBytes方法的典型用法代码示例。如果您正苦于以下问题:C# Mat.ToBytes方法的具体用法?C# Mat.ToBytes怎么用?C# Mat.ToBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mat
的用法示例。
在下文中一共展示了Mat.ToBytes方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Start
// Use this for initialization
void Start () {
Mat src = new Mat(Application.dataPath + "/lena.png", ImreadModes.GrayScale);
Mat dst = new Mat();
frame = new Mat ();
Cv2.Canny(src, dst, 50, 200);
tex = new Texture2D (dst.Width, dst.Height);
tex.LoadImage (dst.ToBytes (".png", new int[]{0}));
go.GetComponent<Renderer> ().material.mainTexture = tex;
cap = new VideoCapture (1);
}
示例2: Start
// Use this for initialization
private void Start () {
frame = new Mat ();
cap = new VideoCapture (1);
tex = new Texture2D (cap.FrameWidth, cap.FrameHeight);
cap.Read (frame);
dst = new Mat ();
thresh = new Mat ();
tex.LoadImage (frame.ToBytes (".png", new int[]{0}));
go.GetComponent<Renderer> ().material.mainTexture = tex;
}
示例3: MatProxy
public MatProxy(Mat image)
{
ImageData = image.ToBytes(".png");
}
示例4: DetectFace
public ActionResult DetectFace(HttpPostedFileBase imageData)
{
try
{
if (imageData == null) { throw new ArgumentException("File is not exist."); }
using (var img = Mat.FromStream(imageData.InputStream, LoadMode.Color))
{
var ExecutingAssemblyPath = new Uri(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase.ToString())).LocalPath;
double scale = 2.0;
using (var gray = new Mat())
using (var smallImg = new Mat((int)(img.Rows / scale), (int)(img.Cols / scale), MatType.CV_8UC1))
{
Cv2.CvtColor(img, gray, ColorConversion.BgrToGray);
Cv2.Resize(gray, smallImg, smallImg.Size(), 0, 0, Interpolation.Linear);
Cv2.EqualizeHist(smallImg, smallImg);
byte[] imgBytes = img.ToBytes(".png");
string base64Img = Convert.ToBase64String(imgBytes);
ViewBag.Base64Img = base64Img;
var obj = new CascadeClassifier();
var cascadeFilePath = Path.Combine(ExecutingAssemblyPath, "Content\\haarcascade_frontalface_alt.xml");
if (!obj.Load(cascadeFilePath)) { throw new InvalidOperationException("Failed to load classifier file."); }
var rects = obj.DetectMultiScale(smallImg);
var nestedObj = new CascadeClassifier();
var nestedCascadeFilePath = Path.Combine(ExecutingAssemblyPath, "Content\\haarcascade_eye.xml");
//var nestedCascadeFilePath = Path.Combine(ExecutingAssemblyPath, "Content\\haarcascade_eye_tree_eyeglasses.xml");
if (!nestedObj.Load(nestedCascadeFilePath)) { throw new InvalidOperationException("Failed to load classifier file."); }
foreach(var rect in rects)
{
Point faceCenter;
faceCenter.X = (int)((rect.X + rect.Width * 0.5) * scale);
faceCenter.Y = (int)((rect.Y + rect.Height * 0.5) * scale);
int faceRadius = (int)((rect.Width + rect.Height) * 0.25 * scale);
Cv2.Circle(img, faceCenter, faceRadius, new Scalar(80, 80, 255), 3, LineType.Link8, 0);
Mat smallImgROI = new Mat(smallImg, rect);
var nestedRects = nestedObj.DetectMultiScale(smallImgROI);
foreach(var nestedRect in nestedRects)
{
Point center;
center.X = (int)((rect.X + nestedRect.X + nestedRect.Width * 0.5) * scale);
center.Y = (int)((rect.Y + nestedRect.Y + nestedRect.Height * 0.5) * scale);
int radius = (int)((nestedRect.Width + nestedRect.Height) * 0.25 * scale);
Cv2.Circle(img, center, radius, new Scalar(80, 255, 80), 3, LineType.Link8, 0);
}
}
byte[] resultBytes = img.ToBytes(".png");
string base64Result = Convert.ToBase64String(resultBytes);
ViewBag.Base64OrgResult = base64Result;
byte[] grayBytes = gray.ToBytes(".png");
string base64Gray = Convert.ToBase64String(grayBytes);
ViewBag.Base64Gray = base64Gray;
byte[] smallBytes = smallImg.ToBytes(".png");
string base64Small = Convert.ToBase64String(smallBytes);
ViewBag.Base64Small = base64Small;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return View();
}
示例5: MatchTemplate
public ActionResult MatchTemplate()
{
try
{
if (Request.Files.Count != 2) { throw new ArgumentException("File is not uploaded."); }
HttpPostedFileBase postedImageData = Request.Files[0], postedTemplate = Request.Files[1];
using (var img = Mat.FromStream(postedImageData.InputStream, LoadMode.Color))
using (var template = Mat.FromStream(postedTemplate.InputStream, LoadMode.Color))
{
using (var result = new Mat())
{
byte[] imgBytes = img.ToBytes(".png");
string base64Img = Convert.ToBase64String(imgBytes);
ViewBag.Base64Img = base64Img;
byte[] templateBytes = template.ToBytes(".png");
string base64Template = Convert.ToBase64String(templateBytes);
ViewBag.Base64Template = base64Template;
Cv2.MatchTemplate(img, template, result, MatchTemplateMethod.CCoeffNormed);
var roi = new Rect(0,0, template.Cols, template.Rows);
Point minPoint, maxPoint;
double minVal, maxVal;
Cv2.MinMaxLoc(result, out minVal, out maxVal, out minPoint, out maxPoint);
roi.X = maxPoint.X;
roi.Y = maxPoint.Y;
Cv2.Rectangle(img, roi, new Scalar(0, 0, 255), 3);
byte[] searchBytes = result.ToBytes(".png");
string base64Search = Convert.ToBase64String(searchBytes);
ViewBag.Base64Search = base64Search;
byte[] resultBytes = img.ToBytes(".png");
string base64Result = Convert.ToBase64String(resultBytes);
ViewBag.Base64Result = base64Result;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return View();
}
示例6: HoughTransform
public ActionResult HoughTransform(HttpPostedFileBase imageData)
{
try
{
if (imageData == null) { throw new ArgumentException("File is not exist."); }
using (var img = Mat.FromStream(imageData.InputStream, LoadMode.Color))
{
using(var gray = new Mat())
using(var workLine = new Mat())
using(var workCircle = new Mat())
using(var dstStandardHough = img.Clone())
using(var dstProbabilistic = img.Clone())
using (var dstCircle = img.Clone())
{
Cv2.CvtColor(img, gray, ColorConversion.BgrToGray);
Cv2.Canny(gray, workLine, 50, 200);
byte[] imgBytes = img.ToBytes(".png");
string base64Img = Convert.ToBase64String(imgBytes);
ViewBag.Base64Img = base64Img;
#region Standard Hough Transform
{
var lines = Cv2.HoughLines(workLine, 1, Cv.PI / 180, 200);
foreach (var line in lines)
{
Point pt1, pt2;
double a = Math.Cos(line.Theta), b = Math.Sin(line.Theta);
double x0 = a * line.Rho, y0 = b * line.Rho;
pt1.X = (int)(x0 + 1000 * (-b));
pt1.Y = (int)(y0 + 1000 * (a));
pt2.X = (int)(x0 - 1000 * (-b));
pt2.Y = (int)(y0 - 1000 * (a));
Cv2.Line(dstStandardHough, pt1, pt2, new Scalar(0, 0, 255), 3);
}
}
#endregion
#region Probabilistic Hough Transform
{
var lines = Cv2.HoughLinesP(workLine, 1, Cv.PI / 180, 200);
foreach (var line in lines)
{
Cv2.Line(dstProbabilistic, line.P1, line.P2, new Scalar(0, 0, 255), 3);
}
}
#endregion
#region Circle
{
Cv2.GaussianBlur(gray, workCircle, new Size(11, 11), 2, 2);
var circles = Cv2.HoughCircles(workCircle, HoughCirclesMethod.Gradient, 1, 100, 20, 50);
foreach(var circle in circles)
{
Point center = new Point(circle.Center.X, circle.Center.Y);
Cv2.Circle(dstCircle, center, (int)(circle.Radius), new Scalar(0, 0, 255), 2);
}
}
#endregion
byte[] grayBytes = workLine.ToBytes(".png");
string base64Gray = Convert.ToBase64String(grayBytes);
ViewBag.Base64Gray = base64Gray;
byte[] resultSBytes = dstStandardHough.ToBytes(".png");
string base64ResultS = Convert.ToBase64String(resultSBytes);
ViewBag.Base64OrgResultS = base64ResultS;
byte[] resultPBytes = dstProbabilistic.ToBytes(".png");
string base64ResultP = Convert.ToBase64String(resultPBytes);
ViewBag.Base64OrgResultP = base64ResultP;
byte[] resultCBytes = dstCircle.ToBytes(".png");
string base64ResultC = Convert.ToBase64String(resultCBytes);
ViewBag.Base64OrgResultC = base64ResultC;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return View();
}
示例7: 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();
}
示例8: 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();
}