Search code examples
image-processingcontouremgucvcanny-operator

Why Contours ar not shown as blue after canny edge detection


I want to find contours after canny edge detection. But they shown up in white color even though BGR is set as (255,0,0). What is wrong in the code?

private void Capture_ImageGrabbed1(object sender, EventArgs e)
{
    try
    {
        Mat m = new Mat();
        capture.Retrieve(m);
        pic1.Image = m.ToImage<Bgr, byte>().Bitmap;

        if(mode == 1)
        {

        var image = m.ToImage<Bgr, byte>();
        var grayScaleImage = image.Convert<Gray, byte>();
        var blurredImage = grayScaleImage.SmoothGaussian(5, 5, 0, 0);
        var cannyImage = new UMat();
        CvInvoke.Canny(blurredImage, cannyImage, 10, 50);

        VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
        CvInvoke.FindContours(cannyImage, contours, null, Emgu.CV.CvEnum.RetrType.External, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple);
        CvInvoke.DrawContours(cannyImage, contours, -1, new MCvScalar(255, 0, 0),2);

        pic2.Image = cannyImage.Bitmap;

        }
    }
    catch (Exception)
    {

        throw;
    }
}

pic2.Image is the picturebox. Code is working but no blue contours as in attached enter image description herepicture.


Solution

  • private void Capture_ImageGrabbed1(object sender, EventArgs e)
    {
        try
        {
            Mat m = new Mat();
            capture.Retrieve(m);
            pic1.Image = m.Bitmap;
    
            if(mode == 1)
            {
    
            var image = m.ToImage<Bgr, byte>();
            var grayScaleImage = image.Convert<Gray, byte>();
            var blurredImage = grayScaleImage.SmoothGaussian(5, 5, 0, 0);
            var cannyImage = new UMat();
            CvInvoke.Canny(blurredImage, cannyImage, 10, 50);
    
            VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
            CvInvoke.FindContours(cannyImage, contours, null, Emgu.CV.CvEnum.RetrType.External, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple);
            var cannyOut = cannyImage.ToImage<Bgr, byte>();
            CvInvoke.DrawContours(cannyOut, contours, -1, new MCvScalar(255, 0, 0),2);
            
            pic2.Image = cannyOut.Bitmap;
    
            }
        }
        catch (Exception)
        {
    
            throw;
        }
    }