Canny: Lot of unwanted edges are detected

2019-04-17 11:04发布

问题:

please have a look at the following code

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace std;
using namespace cv;

int main()
{
    VideoCapture *camera = new VideoCapture();

    camera->open(0);



    if(!camera->isOpened())
    {
        cout << "No Camera" << endl;
        return -1;
    }

    Mat image,blur,canny;

    namedWindow("Video");

    while(true)
    {
        *camera>>image;

        imshow("Video",image);

        cv::GaussianBlur(image,blur,Size(7,7),1.5,1.5);
        cv::Canny(blur,canny,0,30,3);

        imshow("cANNY",canny);

        if(waitKey(30)>=0)
        {

            break;
        }

    }


    return 0;
}

This code generated following 2 outputs

Original

Canny

As you can see, the canny has detected LOT of edges which doesn't even exists. If this is an issue with the web camera frames per seconds, what kind of web cam should I use? how many Frames per seconds? I have tested this with the default web cam in DELL Inspiron 4030 and another USB 2.0 web cam. Both results are same.

If this is an issue with the code, how can I solve it?

My next target is background subtraction, and I feel that these unwanted stuff might fail me. Please help.

回答1:

You have threshold1==0. If you set it to 10, and set the blur sigma to 4, most of those spurious lines will disappear. (at least they do for me).

With my webcam these setting get a reasonable output:

    cv::GaussianBlur(image,blur,Size(0,0), 4);
    cv::Canny(blur,canny,25,30,3);

However the lines detected are still a bit noisy.

EDIT: The following doesn't help: Another thing you could do is average two consecutive frames, that would reduce the noise to 70%.

(Or buy a better camera)



回答2:

Your low threshold value should be more than zero:

int low_threshold = 10;//you can try with different values
cv::Canny(blur,canny,low_threshold,30,3);

You can also play with some some camera parameters such as saurtion and contrast:

VideoCapture *camera = new VideoCapture();
camera->set(CV_CAP_PROP_CONTRAST,contrast_value);//between 0-1
camera->set(CV_CAP_PROP_SATURATION, saturation_value);//between 0-1