public void Initialize(Bitmap bmp) { an = new SkeletonAnnotation(bmp); selectJoint = null; nearestJoint = null; transform = new Matrix(); }
// // joint // private void AssignJointAnnotation(SkeletonAnnotation an, JointAnnotation joint, bool record = true) { if (an == null || joint == null) { DeleteEditingBone(); return; } if (an.joints.Any(j => j.name == joint.name && j.position == joint.position)) { return; } if (record) { AddOperation(new Operation() { funcName = "AssignJointAnnotation", parameters = new List <object>() { an, joint, }, }); } an.joints.Add(joint); }
private void CreateOrCompleteEditingBone(SkeletonAnnotation an, JointAnnotation joint) { if (an == null || joint == null) { DeleteEditingBone(); return; } if (addingBone == null) { addingBone = new BoneAnnotation(joint, new JointAnnotation("[dummy]", joint.position)); selectBone = null; selectJoint = null; } else { addingBone.dst = joint; if (addingBone.src != addingBone.dst) { AssignBoneAnnotation(an, addingBone); } addingBone = new BoneAnnotation(joint, new JointAnnotation("[dummy]", joint.position)); selectBone = null; selectJoint = null; } }
void DeleteJointAnnotation(JointAnnotation joint) { if (an.joints.Contains(joint)) { an.joints.Remove(joint); } }
private void skeletonFittingCanvas_MouseDown(object sender, MouseEventArgs e) { SkeletonAnnotation an = GetEditingAnnotation(); if (an == null) { return; } PointF convLocation = InvertCoordinate(e.Location, skeletonFittingCanvasTransform); if (e.Button == System.Windows.Forms.MouseButtons.Left) { // joint if (jointAddRadioButton.Checked) { AssignJointAnnotation(an, new JointAnnotation(jointNameTextBox.Text, convLocation)); } if (jointSelectRadioButton.Checked) { JointAnnotation joint = an.GetNearestJoint(e.Location, 20, skeletonFittingCanvasTransform); SelectJointAnnotation(an, joint); if (joint != null) { jointNameTextBox.Text = joint.name; } } // bone if (boneAddRadioButton.Checked) { var joint = an.GetNearestJoint(e.Location, 20, skeletonFittingCanvasTransform); if (joint != null) { CreateOrCompleteEditingBone(an, joint); } else { DeleteEditingBone(); } } if (boneSelectRadioButton.Checked) { SelectBoneAnnotation(an, an.GetNearestBone(e.Location, 20, skeletonFittingCanvasTransform)); } skeletonFittingCanvas.Invalidate(); } // 画像をずらす if (e.Button == System.Windows.Forms.MouseButtons.Right) { if (tabControl.SelectedTab.Name == "tabSkeletonFitting") { skeletonFittingCanvasPrevMousePos = e.Location; } } }
public void undo_DeleteJointAnnotation(SkeletonAnnotation an, JointAnnotation joint, List <BoneAnnotation> bones) { AssignJointAnnotation(an, joint, false); foreach (var bone in bones) { AssignBoneAnnotation(an, bone, false); } }
private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (selectRadioButton.Checked) { nearestJoint = an.GetNearestJoint(Form1.InvertCoordinate(e.Location, transform), 20, new Matrix()); } if (an != null) { pictureBox1.Invalidate(); } }
void SaveAnnotations(string root, string dirName, Dictionary <string, SkeletonAnnotation> anDict) { try { string dir = Path.Combine(root, dirName); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } foreach (var f in Directory.GetFiles(dir)) { File.Delete(f); } List <string> lines = new List <string>(); for (int i = 0; i < anDict.Count; i++) { var kv = anDict.ElementAt(i); string f = Path.Combine(dir, kv.Key + ".png"); if (kv.Value.bmp != null) { kv.Value.bmp.Save(f); } lines.Add("SkeletonAnnotation[" + kv.Key + "]"); for (int j = 0; j < kv.Value.joints.Count; j++) { JointAnnotation joint = kv.Value.joints[j]; lines.Add("JointAnnotation[" + j + "]," + joint.name + "," + joint.position.X + "," + joint.position.Y); } for (int j = 0; j < kv.Value.bones.Count; j++) { BoneAnnotation bone = kv.Value.bones[j]; if (kv.Value.joints.Contains(bone.src) && kv.Value.joints.Contains(bone.dst)) { int srcIdx = kv.Value.joints.IndexOf(bone.src); int dstIdx = kv.Value.joints.IndexOf(bone.dst); lines.Add("BoneAnnotation[" + j + "]," + srcIdx + "," + dstIdx); } } lines.Add(""); } string p = Path.Combine(dir, "skeletonAnnotation.ska"); File.WriteAllLines(p, lines.ToArray()); } catch (Exception e) { MessageBox.Show(e.ToString() + e.StackTrace); } }
void SelectJointAnnotation(JointAnnotation joint) { if (an.joints.Contains(joint)) { selectJoint = joint; } else { selectJoint = null; } }
private void UpdateEditingBone(SkeletonAnnotation an, JointAnnotation joint) { if (an == null || joint == null) { DeleteEditingBone(); return; } if (addingBone != null) { addingBone.dst = joint; } }
JointAnnotation GetNearestJoint(SkeletonAnnotation an, PointF point, float threshold, CompositionCanvasControl canvas) { JointAnnotation nearest = null; float minSqDist = threshold * threshold; foreach (var joint in an.joints) { PointF pt = canvas.PointToClient(new Point((int)joint.position.X, (int)joint.position.Y)); float dx = point.X - pt.X; float dy = point.Y - pt.Y; float sqDist = dx * dx + dy * dy; if (minSqDist > sqDist) { nearest = joint; minSqDist = sqDist; } } return(nearest); }
public JointAnnotation GetNearestJoint(PointF point, float threshold, Matrix transform) { JointAnnotation nearest = null; float minSqDist = threshold * threshold; foreach (var joint in joints) { PointF[] jointPt = new[] { joint.position }; transform.TransformPoints(jointPt); float dx = point.X - jointPt[0].X; float dy = point.Y - jointPt[0].Y; float sqDist = dx * dx + dy * dy; if (minSqDist > sqDist) { nearest = joint; minSqDist = sqDist; } } return(nearest); }
private void DeleteJointAnnotation(SkeletonAnnotation an, JointAnnotation joint, bool record = true) { if (an == null || joint == null) { DeleteEditingBone(); return; } if (record) { AddOperation(new Operation() { funcName = "DeleteJointAnnotation", parameters = new List <object>() { an, joint, new List <BoneAnnotation>(an.bones) }, }); } // 関連するボーンを削除 for (int i = an.bones.Count - 1; i >= 0; i--) { if ((an.bones[i].src.name == joint.name && an.bones[i].src.position == joint.position) || (an.bones[i].dst.name == joint.name && an.bones[i].dst.position == joint.position)) { an.bones.RemoveAt(i); } } // 関節を削除 for (int i = an.joints.Count - 1; i >= 0; i--) { if (an.joints[i].name == joint.name && an.joints[i].position == joint.position) { an.joints.RemoveAt(i); } } }
public static SkeletonAnnotation Load(string filepath, Bitmap refSkeletonBmp) { if (!File.Exists(filepath)) { return(null); } filepath = Path.GetFullPath(filepath); SkeletonAnnotation an = new SkeletonAnnotation(refSkeletonBmp); an.joints = File.ReadAllLines(filepath) .Where(line => !string.IsNullOrWhiteSpace(line) && line.Contains(':')) .Select(line => { var tokens = line.Split(':'); if (tokens.Length != 2) { return(null); } var xys = tokens[1].Split(','); if (xys.Length != 2) { return(null); } float x, y; if (!float.TryParse(xys[0], out x) || !float.TryParse(xys[1], out y)) { return(null); } return(new JointAnnotation(tokens[0], new PointF(x, y))); }) .Where(j => j != null) .ToList(); an.bones = File.ReadAllLines(filepath) .Where(line => !string.IsNullOrWhiteSpace(line) && line.Contains('>')) .Select(line => { var tokens = line.Split('>'); if (tokens.Length != 2) { return(null); } JointAnnotation src = null, dst = null; foreach (var j in an.joints) { if (j.name == tokens[0]) { src = j; } if (j.name == tokens[1]) { dst = j; } } if (src == null || dst == null) { return(null); } return(new BoneAnnotation(src, dst)); }) .Where(b => b != null) .ToList(); return(an); }
public void redo_DeleteJointAnnotation(SkeletonAnnotation an, JointAnnotation joint, List <BoneAnnotation> bones) { DeleteJointAnnotation(an, joint, false); }
void AddJointAnnotation(JointAnnotation joint) { an.joints.Add(joint); }
private void SelectJointAnnotation(SkeletonAnnotation an, JointAnnotation jointAnnotation) { addingBone = null; selectBone = null; selectJoint = an.joints.Contains(jointAnnotation) ? jointAnnotation : null; }
public void undo_AssignJointAnnotation(SkeletonAnnotation an, JointAnnotation joint) { DeleteJointAnnotation(an, joint, false); }
public BoneAnnotation(JointAnnotation src, JointAnnotation dst) { this.src = src; this.dst = dst; }