private void Form1_Load(object sender, EventArgs e) { // Load the template image. imageViewer2.Image.Type = ImageType.Hsl32; imageViewer2.Image.ReadVisionFile(System.IO.Path.Combine(imagePath, "template.png")); // Set up the color pattern matching options. matchColorPatternOptions = new MatchColorPatternOptions(MatchMode.ShiftInvariant, 2, ImageFeatureMode.ColorAndShape, ColorSensitivity.Low); matchColorPatternOptions.ColorWeight = 500; matchColorPatternOptions.SearchStrategy = SearchStrategy.Balanced; matchColorPatternOptions.MinimumMatchScore = 775; // Enable the timer. timer1.Enabled = true; timer1_Tick(timer1, EventArgs.Empty); }
// Click the Match button to find the patterns in the image. The results // are then displayed using regions. private void matchPatternButton_Click(object sender, EventArgs e) { // Get match input parameters. MatchColorPatternOptions matchOptions = new MatchColorPatternOptions(); matchOptions.MatchMode = (MatchMode)Enum.Parse(typeof(MatchMode), (string)matchMode.SelectedItem); matchOptions.MinimumMatchScore = (double)minimumScore.Value; matchOptions.NumberOfMatchesRequested = (int)matchesRequested.Value; matchOptions.MinimumContrast = (int)minimumContrast.Value; matchOptions.ColorWeight = (double)colorScoreWeight.Value; matchOptions.FeatureMode = (ImageFeatureMode)Enum.Parse(typeof(ImageFeatureMode), (string)matchFeatureMode.SelectedItem); matchOptions.ColorSensitivity = (ColorSensitivity)Enum.Parse(typeof(ColorSensitivity), (string)colorSensitivity.SelectedItem); matchOptions.SearchStrategy = (SearchStrategy)Enum.Parse(typeof(SearchStrategy), (string)searchStrategy.SelectedItem); matchOptions.SubpixelAccuracy = subpixelAccuracy.Checked; if (matchOptions.MatchMode == MatchMode.RotationInvariant && matchOptions.FeatureMode == ImageFeatureMode.Color) { MessageBox.Show("Rotation-Invariant Color Pattern Matching requires a feature mode including shape", "Color Pattern Matching Error"); return; } // Do the match. Collection <PatternMatch> matches = Algorithms.MatchColorPattern(imageViewer1.Image, imageViewer2.Image, matchOptions, imageViewer1.Roi); // Display the matches. imageViewer1.Image.Overlays.Default.Clear(); foreach (PatternMatch match in matches) { // First draw the bounding box. imageViewer1.Image.Overlays.Default.AddPolygon(new PolygonContour(match.Corners), Rgb32Value.RedColor); // Now draw the center point. imageViewer1.Image.Overlays.Default.AddOval(new OvalContour(match.Position.X - 5, match.Position.Y - 5, 11, 11), Rgb32Value.RedColor); // Finally draw the crosshair. imageViewer1.Image.Overlays.Default.AddLine(new LineContour(new PointContour(match.Position.X - 10, match.Position.Y), new PointContour(match.Position.X + 10, match.Position.Y)), Rgb32Value.RedColor); imageViewer1.Image.Overlays.Default.AddLine(new LineContour(new PointContour(match.Position.X, match.Position.Y - 10), new PointContour(match.Position.X, match.Position.Y + 10)), Rgb32Value.RedColor); } matchesFound.Text = matches.Count.ToString(); learnPatternButton.Enabled = false; }