Click here to Skip to main content
15,302,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've followed an Emgu tutorial, which works fine for finding and loading one set of contours onto one output image.

Where my code need deviates from that tutorial, and also from the seeming capacity of my wits at this time, is that I need to find and display contours based on an image binarization threshold which updates, AT the time of the update of that threshold, given by trackbar1.Value.

There are some remnants and inefficiencies in the code listed below, attempts to troubleshoot the issue, I'm sure you understand.

The issue is, when the trackbar1.Value is changed, the contours are in fact added to and displayed on the output Image, but they also seem to modify img_Gray, the one from which I derive the contours. Thusly, I've ended up with an image completely saturated with contour lines, due to reading contours of contours ad infinitum.

It's probably something simple, but I'm at wits end. If someone could help me out, I'd appreciate it.

using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using Emgu.CV.Structure;
    using Emgu.CV.Util;
    using Emgu.CV;
    namespace Emgu_15_Contour_Extraction
        public partial class Form1 : Form
            Image<Bgr, byte> img_Input;
            Image<Gray, byte> img_Gray;
            Image<Bgr, byte> img_Output;
            public Form1()
            private void openToolStripMenuItem_Click(object sender, EventArgs e)
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "Image Files| *.BMP; *.JPG; *.GIF";
                if (ofd.ShowDialog() == DialogResult.OK)
                    img_Input = new Image<Bgr, byte>(ofd.FileName);
                    pictureBox1.Image = img_Input.Bitmap;
            private void trackBar1_Scroll(object sender, EventArgs e)
                if (img_Input != null)
                    img_Gray = img_Input.Convert<Gray, byte>().ThresholdBinary(new Gray(trackBar1.Value), new Gray(255));
                    Emgu.CV.Util.VectorOfVectorOfPoint Contours = new Emgu.CV.Util.VectorOfVectorOfPoint();
                    Mat Heirarchy = new Mat();
                    Contours = new VectorOfVectorOfPoint();
                    CvInvoke.FindContours(img_Gray, Contours, Heirarchy, Emgu.CV.CvEnum.RetrType.External, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple);
                    pictureBox3.Image = img_Gray.Bitmap;
                    img_Output = null;
                    img_Output = img_Input;
                    CvInvoke.DrawContours(img_Output, Contours, -1, new MCvScalar(0, 0, 255));
                    pictureBox2.Image = img_Output.Bitmap;
                    img_Gray = null;

What I have tried:

I have tried everything within my grasp at this time, I just need a simple answer to what I know is a very simple question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900