public void Add() { using var m = new Mat <double>(); m.Add(1.2); m.Add(3.4); m.Add(5.6); Assert.Equal(1.2, m.Get <double>(0), 6); Assert.Equal(3.4, m.Get <double>(1), 6); Assert.Equal(5.6, m.Get <double>(2), 6); }
public void Add() { using (var m = new Mat <double>()) { m.Add(1.2); m.Add(3.4); m.Add(5.6); var indexer = m.GetIndexer(); Assert.Equal(1.2, m.Get <double>(0), 6); Assert.Equal(3.4, m.Get <double>(1), 6); Assert.Equal(5.6, m.Get <double>(2), 6); } }
private void cutLeftImageToEightParts() // step1, choose the centre region of left image, cut it into 8 equal parts { int topHeight = (int)(0.45 * leftImg.Rows); int buttomHeight = (int)(0.55 * leftImg.Rows); Mat leftImg_roi = leftImg.RowRange(topHeight, buttomHeight); // our region of interest, which is the centre region of left image part1 = leftImg_roi.ColRange((int)(0.1 * leftImg.Cols), (int)(0.2 * leftImg.Cols)); part2 = leftImg_roi.ColRange((int)(0.2 * leftImg.Cols), (int)(0.3 * leftImg.Cols)); part3 = leftImg_roi.ColRange((int)(0.3 * leftImg.Cols), (int)(0.4 * leftImg.Cols)); part4 = leftImg_roi.ColRange((int)(0.4 * leftImg.Cols), (int)(0.5 * leftImg.Cols)); part5 = leftImg_roi.ColRange((int)(0.5 * leftImg.Cols), (int)(0.6 * leftImg.Cols)); part6 = leftImg_roi.ColRange((int)(0.6 * leftImg.Cols), (int)(0.7 * leftImg.Cols)); part7 = leftImg_roi.ColRange((int)(0.7 * leftImg.Cols), (int)(0.8 * leftImg.Cols)); part8 = leftImg_roi.ColRange((int)(0.8 * leftImg.Cols), (int)(0.9 * leftImg.Cols)); part1_centre = new Mat(); part2_centre = new Mat(); part3_centre = new Mat(); part4_centre = new Mat(); part5_centre = new Mat(); part6_centre = new Mat(); part7_centre = new Mat(); part8_centre = new Mat(); part1_centre.Add(new Point3d((int)(0.15 * leftImg.Cols), (int)(leftImg.Height / 2), 1)); part2_centre.Add(new Point3d((int)(0.25 * leftImg.Cols), (int)(leftImg.Height / 2), 1)); part3_centre.Add(new Point3d((int)(0.35 * leftImg.Cols), (int)(leftImg.Height / 2), 1)); part4_centre.Add(new Point3d((int)(0.45 * leftImg.Cols), (int)(leftImg.Height / 2), 1)); part5_centre.Add(new Point3d((int)(0.55 * leftImg.Cols), (int)(leftImg.Height / 2), 1)); part6_centre.Add(new Point3d((int)(0.65 * leftImg.Cols), (int)(leftImg.Height / 2), 1)); part7_centre.Add(new Point3d((int)(0.75 * leftImg.Cols), (int)(leftImg.Height / 2), 1)); part8_centre.Add(new Point3d((int)(0.85 * leftImg.Cols), (int)(leftImg.Height / 2), 1)); }
public static void Subtract(this Mat mat, double value) { mat.Add(-value); //Mat op = new Mat(mat.Size, mat.Depth, mat.NumberOfChannels); //op.SetTo(new MCvScalar(value)); //CvInvoke.Subtract(mat, op, mat); //op.Dispose(); }
Mat asMat(byte[] imgRGB, int offset = 0) { int i = offset; Mat img = new Mat(); for (; i < imgRGB.Length; i++) { img.Add((byte)imgRGB[i]); } return(img); }
public static (double, double, double) HeadPose(IDictionary <FacePart, IEnumerable <FacePoint> > item, int width, int height) { using (Mat <Point2d> landmarks = new Mat <Point2d>()) { FacePoint[] noseTip = item[FacePart.NoseBridge].ToArray(); FacePoint[] chin = item[FacePart.Chin].ToArray(); FacePoint[] leftEye = item[FacePart.LeftEye].ToArray(); FacePoint[] rightEye = item[FacePart.RightEye].ToArray(); FacePoint[] bottomLip = item[FacePart.BottomLip].ToArray(); landmarks.Add(new Point2d(noseTip[4].Point.X, noseTip[4].Point.Y)); // Nose tip 鼻尖 landmarks.Add(new Point2d(chin[8].Point.X, chin[8].Point.Y)); // Chin 下巴 landmarks.Add(new Point2d(leftEye[0].Point.X, leftEye[0].Point.Y)); // Left eye left corner 左眼左上角 landmarks.Add(new Point2d(rightEye[3].Point.X, rightEye[3].Point.Y)); // Right eye right corner 右眼右上角 landmarks.Add(new Point2d(bottomLip[6].Point.X, bottomLip[6].Point.Y)); // Left Mouth corner 左嘴角 landmarks.Add(new Point2d(bottomLip[0].Point.X, bottomLip[0].Point.Y)); // Right mouth corner 右嘴角 using (Mat camera_matrix = GetCameraMatrix(width, height)) using (Mat <double> coeffs = new Mat <double>(4, 1)) { coeffs.SetTo(0); using (Mat <double> rotation = new Mat <double>()) { using (Mat translation = new Mat <double>()) { Cv2.SolvePnP(model_points, landmarks, camera_matrix, coeffs, rotation, translation); } var ang = GetEulerAngle(rotation); return(ang); } } } }
public static void exec() { //var image = new Mat(@"image\result_success.bmp", ImreadModes.GrayScale); var image_success = new Mat(@"image\result_success.bmp"); var image_failure = new Mat(@"image\result_failure.bmp"); var hsv_success = new Mat(); var hsv_failure = new Mat(); Cv2.CvtColor(image_success, hsv_success, ColorConversionCodes.BGR2HSV); Cv2.CvtColor(image_failure, hsv_failure, ColorConversionCodes.BGR2HSV); var hist_success = new Mat(); foreach (var s in hsv_success.Split()) { var h = GetHistogram4Hue(s); hist_success.Add(h); break; } var hist_failure = new Mat(); foreach (var s in hsv_failure.Split()) { var h = GetHistogram4Hue(s); hist_failure.Add(h); break; } Cv2.ImShow("success", hsv_success); Cv2.ImShow("failure", hsv_failure); Cv2.ImShow("hist_success", hist_success); Cv2.ImShow("hist_failure", hist_failure); Cv2.WaitKey(); }
private void button5_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); var opRes = openFileDialog.ShowDialog(); if (opRes != DialogResult.OK) { return; } openFileDialog.Dispose(); textBox1.Text = openFileDialog.FileName; using (var dliImg = FaceRecognition.LoadImageFile(textBox1.Text)) { IEnumerable <IDictionary <FacePart, IEnumerable <FacePoint> > > landmarks = _faceRecognition.FaceLandmark(dliImg); foreach (IDictionary <FacePart, IEnumerable <FacePoint> > item in landmarks) { Mat <Point2d> points = new Mat <Point2d>(); FacePoint[] noseTip = item[FacePart.NoseBridge].ToArray(); FacePoint[] chin = item[FacePart.Chin].ToArray(); FacePoint[] leftEye = item[FacePart.LeftEye].ToArray(); FacePoint[] rightEye = item[FacePart.RightEye].ToArray(); FacePoint[] bottomLip = item[FacePart.BottomLip].ToArray(); points.Add(new Point2d(noseTip[4].Point.X, noseTip[4].Point.Y)); // Nose tip 鼻尖 points.Add(new Point2d(chin[8].Point.X, chin[8].Point.Y)); // Chin 下巴 points.Add(new Point2d(leftEye[0].Point.X, leftEye[0].Point.Y)); // Left eye left corner 左眼左上角 points.Add(new Point2d(rightEye[3].Point.X, rightEye[3].Point.Y)); // Right eye right corner 右眼右上角 points.Add(new Point2d(bottomLip[6].Point.X, bottomLip[6].Point.Y)); // Left Mouth corner 左嘴角 points.Add(new Point2d(bottomLip[0].Point.X, bottomLip[0].Point.Y)); // Right mouth corner 右嘴角 Points = points; width = dliImg.Width; height = dliImg.Height; break; } } }
public override void ReadXml(System.Xml.XmlReader reader) { reader.MoveToContent(); reader.ReadStartElement(); base.ReadXml(reader); LODState = (LODStateEnum)Enum.Parse(typeof(LODStateEnum), reader.ReadElementString("LODState", "")); if (reader.Name == "LODs") { reader.ReadStartElement(); int counter = 0; while (reader.NodeType != System.Xml.XmlNodeType.EndElement) { String s = reader.ReadElementString("ModelPath", ""); Paths[counter] = s; counter++; } reader.ReadEndElement(); } for (int j = 0; j < Paths.Count(); ++j) { if (Paths[j] != "") { LODs[j] = ResourceManager.Instance.LoadModel(Paths[j]); } else { LODs[j] = LODs[0]; } } if (reader.Name == "Materials") { if (!TrashSoupGame.Instance.EditorMode) { contentPath = "../../../../TrashSoupContent/Materials/"; } else { contentPath = "../../../TrashSoup/TrashSoupContent/Materials/"; } reader.ReadStartElement(); while (reader.NodeType != System.Xml.XmlNodeType.EndElement) { if (reader.Name == "Name") { String newName = reader.ReadElementString("Name", ""); if (newName == "testSBMat2") { SkyboxMaterial sm = new SkyboxMaterial(); sm = ResourceManager.Instance.LoadSkyboxMaterial(Path.GetFullPath(contentPath) + newName + ".xml"); Mat.Add(sm); } if (newName == "testWaterMat") { WaterMaterial wm = new WaterMaterial(); wm = ResourceManager.Instance.LoadWaterMaterial(Path.GetFullPath(contentPath) + newName + ".xml"); Mat.Add(wm); } if (newName == "testMirrorMat") { MirrorMaterial mm = new MirrorMaterial(); mm = ResourceManager.Instance.LoadMirrorMaterial(Path.GetFullPath(contentPath) + newName + ".xml"); Mat.Add(mm); } if (newName != "testSBMat2" && newName != "testWaterMat" && newName != "testMirrorMat") { Material m = new Material(); m = ResourceManager.Instance.LoadMaterial(Path.GetFullPath(contentPath) + newName + ".xml"); Mat.Add(m); } } } reader.ReadEndElement(); } this.LodControlled = reader.ReadElementContentAsBoolean("LodControlled", ""); //ResourceManager.Instance.LoadEffects(TrashSoupGame.Instance); reader.ReadEndElement(); }
private void button1_Click(object sender, EventArgs e) { OpenFileDialog folder = new OpenFileDialog(); folder.ShowDialog(); //folder.Multiselect = true; for (int i = 0; i < folder.FileNames.Length; i++) { string fileName = folder.FileNames[i]; using (var evf_Bmp = new Bitmap(fileName)) using (var dliImg = FaceRecognition.LoadImage(evf_Bmp)) { IEnumerable <IDictionary <FacePart, IEnumerable <FacePoint> > > landmarks = _faceRecognition.FaceLandmark(dliImg); Graphics g = Graphics.FromImage(evf_Bmp); g.SmoothingMode = SmoothingMode.HighSpeed; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.PixelOffsetMode = PixelOffsetMode.HighQuality; foreach (IDictionary <FacePart, IEnumerable <FacePoint> > item in landmarks) { Color color = Color.GreenYellow; Mat <Point2d> imgPoint = new Mat <Point2d>(); FacePoint[] noseTip = item[FacePart.NoseBridge].ToArray(); FacePoint[] chin = item[FacePart.Chin].ToArray(); FacePoint[] leftEye = item[FacePart.LeftEye].ToArray(); FacePoint[] rightEye = item[FacePart.RightEye].ToArray(); FacePoint[] bottomLip = item[FacePart.BottomLip].ToArray(); #if DEBUG foreach (var point in noseTip) { g.FillEllipse(new SolidBrush(Color.Red), (float)point.Point.X - 1, (float)point.Point.Y - 1, 3, 3); } foreach (var point in chin) { g.FillEllipse(new SolidBrush(Color.Red), (float)point.Point.X - 1, (float)point.Point.Y - 1, 3, 3); } foreach (var point in leftEye) { g.FillEllipse(new SolidBrush(Color.Red), (float)point.Point.X - 1, (float)point.Point.Y - 1, 3, 3); } foreach (var point in rightEye) { g.FillEllipse(new SolidBrush(Color.Red), (float)point.Point.X - 1, (float)point.Point.Y - 1, 3, 3); } foreach (var point in bottomLip) { g.FillEllipse(new SolidBrush(Color.Red), (float)point.Point.X - 1, (float)point.Point.Y - 1, 3, 3); } #endif imgPoint.Add(new Point2d(noseTip[3].Point.X, noseTip[3].Point.Y)); // Nose tip 鼻尖 imgPoint.Add(new Point2d(chin[8].Point.X, chin[8].Point.Y)); // Chin 下巴 imgPoint.Add(new Point2d(leftEye[0].Point.X, leftEye[0].Point.Y)); // Left eye left corner 左眼左上角 imgPoint.Add(new Point2d(rightEye[3].Point.X, rightEye[3].Point.Y)); // Right eye right corner 右眼右上角 imgPoint.Add(new Point2d(bottomLip[6].Point.X, bottomLip[6].Point.Y)); // Left Mouth corner 左嘴角 imgPoint.Add(new Point2d(bottomLip[0].Point.X, bottomLip[0].Point.Y)); // Right mouth corner 右嘴角 foreach (var point in imgPoint) { g.FillEllipse(new SolidBrush(color), (float)point.X - 1, (float)point.Y - 1, 3, 3); } var rotation = HeadPose(item, dliImg.Width, dliImg.Height); g.DrawString(rotation.Item1.ToString("0.000"), new Font("微软雅黑", 20F, FontStyle.Bold), Brushes.White, 10, 10); g.DrawString(rotation.Item2.ToString("0.000"), new Font("微软雅黑", 20F, FontStyle.Bold), Brushes.White, 10, 30); g.DrawString(rotation.Item3.ToString("0.000"), new Font("微软雅黑", 20F, FontStyle.Bold), Brushes.White, 10, 50); imgPoint.Dispose(); break; } g.Dispose(); evf_Bmp.Save($"{Path.GetDirectoryName(fileName)}\\{Path.GetFileNameWithoutExtension(fileName)}_point.{Path.GetExtension(fileName)}"); } } }