Search code examples
c++opencvedge-detectionconnected-componentscanny-operator

Edge detection affected by light illumination


I have this raw image in grayscale:

enter image description here

I would like to detect the edge of the object. However, it is affected by illumination near the edge. This is what I obtained after Gaussian blur and Canny edge detection:

enter image description here

This is my code:

    cv::cvtColor(imgOriginal, imgGrayscale, CV_BGR2GRAY);       // convert to grayscale

    cv::GaussianBlur(crop,                  // input image
        imgBlurred,                         // output image
        cv::Size(5, 5),                     // smoothing window width and height in pixels
        5);                                 // sigma value, determines how much the image will be blurred

    cv::Canny(imgBlurred,           // input image
        imgCanny,                   // output image
        0,                          // low threshold
        100);                       // high threshold

The light source is beneath the object. The illumination at the edge of object is from the light source or reflection of light. They are always at the same place.

The illumination is detected as edge as well. I have tried several other ways such as connected component labelling and binarize image with sample code (a beginner here) but to avail. Is there any way to detect clean edge illumination?


Solution

  • The background light patches may be removeable using some erosion with a fairly large kernel, since the object is much bigger than the light patches

    Another common technique you can try is using distance transform + watershed. The distance transform will likely return points that you are certain are inside the object (since the object has little dark areas). Watershed will try to find regions that are connected (by comparing gradients) to the confirmed points. You may need to combine multiple regions after the watershed if the distance transform gives multiple points inside the object.