private void Button_Click(object sender, RoutedEventArgs e) { BgrHsvClassifier classifier = new BgrHsvClassifier(); classifier.Train(); SVMClassifier svm = new SVMClassifier(); svm.TrainFromCsv(@"D:\Development\OpenCV\SetVision\recordings.csv"); svm.Save(@"D:\Development\OpenCV\SetVision\rgbsvm.yaml"); }
/// <summary> /// LocateCards works by analyzing the contours in the image. /// For instance, the Diamond in Set is a polygon with exactly 4 vertices. /// The Oval has no such features yet. /// The Squiggle is not convex, but concave and has edges in a 'right bend', instead of only 'left bends' /// /// All these shapes are inside the contour of a (white) card, which is a rounded square. /// Cards may also be the (only) exterior boundaries /// </summary> /// <param name="table">An image displaying the table with the Set cards</param> /// <returns>A dict locating which cards are present where in the image</returns> public Dictionary<Card, Point> LocateCards(Image<Bgr, Byte> table, Settings settings) { classifier = new BgrHsvClassifier(); classifier.Train(); #region process image //Convert the image to grayscale and filter out the noise Image<Gray, Byte> gray = table.Convert<Gray, Byte>(); Gray cannyThreshold = new Gray(50); //180 Gray cannyThresholdLinking = new Gray(30); //120 Gray circleAccumulatorThreshold = new Gray(100); //120 #region old Image<Gray, Byte> cannyEdges = gray.Canny(cannyThreshold, cannyThresholdLinking); if (settings.debuglevel >= 3) { ImageViewer.Show(cannyEdges, "cannyEdges before Closing"); } #endregion //#region new //Image<Gray, Byte> thresholded = new Image<Gray, byte>(gray.Size); //CvInvoke.cvAdaptiveThreshold(gray.Ptr, thresholded.Ptr, // 255, // ADAPTIVE_THRESHOLD_TYPE.CV_ADAPTIVE_THRESH_GAUSSIAN_C, // THRESH.CV_THRESH_BINARY_INV, 9, 5); //StructuringElementEx el1 = new StructuringElementEx(3, 3, 1, 1, CV_ELEMENT_SHAPE.CV_SHAPE_RECT); //thresholded = thresholded.Erode(1);//thresholded.MorphologyEx(el1, CV_MORPH_OP.CV_MOP_CLOSE, 1);// //Image<Gray, Byte> cannyEdges = thresholded;//.Canny(new Gray(1), new Gray(10)); //#endregion //StructuringElementEx el = new StructuringElementEx(5, 5, 2, 2, CV_ELEMENT_SHAPE.CV_SHAPE_RECT); StructuringElementEx el = new StructuringElementEx(3, 3, 1, 1, CV_ELEMENT_SHAPE.CV_SHAPE_RECT); cannyEdges = cannyEdges.MorphologyEx(el, CV_MORPH_OP.CV_MOP_CLOSE, 1); if (settings.debuglevel >= 3) { ImageViewer.Show(cannyEdges, "cannyEdges after Closing"); } #endregion Contour<Point> contours = cannyEdges.FindContours( CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, //was CV_CHAIN_APPROX_SIMPLE RETR_TYPE.CV_RETR_TREE); ContourNode tree = new ContourNode(contours); FilterTree(tree); #region debug if (settings.debuglevel >= 3) { var debug = table.Clone(); DrawContours(tree, debug); ImageViewer.Show(debug, "Contours after filtering"); } #endregion AssignShapes(tree); AssignImages(tree, table, true); #region debug if (settings.debuglevel >= 3) { var debug1 = table.Clone(); DrawContours(tree, debug1); ImageViewer.Show(debug1); } #endregion FilterTree(tree); #region debug if (settings.debuglevel >= 3) { var debug2 = table.Clone(); DrawContours(tree, debug2); ImageViewer.Show(debug2); } #endregion AssignColors(tree, table, settings); #region debug if (settings.debuglevel >= 3) { TreeViz.VizualizeTree(tree); } #endregion AssignFills(tree); Dictionary<Card, Point> cardlocs = new Dictionary<Card, Point>(); foreach (KeyValuePair<Card, ContourNode> pair in GiveCards(tree)) { ContourNode node = pair.Value; Card card = pair.Key; PointF fcenter = node.Contour.GetMinAreaRect().center; Point center = new Point((int)fcenter.X, (int)fcenter.Y); cardlocs.Add(card, center); } #region debug if (settings.debuglevel >= 1) { TreeViz.VizualizeTree(tree); } if (settings.debuglevel >= 2) { DrawContours(tree, table); ImageViewer.Show(table); } #endregion return cardlocs; }