Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Replicating OpenCV's Canny Edge Detection

Hi,

I'm currently working on replicating the Canny edge detector in OpenCV. I've managed to replicate the built-in Sobel operator using a 3x3 Gaussian filter and by calculating the magnitude as a weighted sum of absolute values of Gx and Gy (L2gradient is set to FALSE when calling Canny).

Sample outputs below:

original image

OpenCV Sobel

My Sobel operator

My Sobel operator after non-max

OpenCV Canny (L2gradient = FALSE)

My Canny ED

I assume the problem is in thresholding. This is how I imagined doing it:

for (int rows = 1; rows < height-1; rows++)
{
    for (int cols = 1; cols < width-1; cols++)
    {
        temp = custom_canny.at<uint8_t>(rows, cols);

        //thresholding
        if (temp >= THRESHOLD_HIGH) temp = 255;
        else if (temp < THRESHOLD_LOW) temp = 0;

        else
        {
            if (cannypixels[rows * width + cols - 1] >= THRESHOLD_HIGH || cannypixels[rows * width + cols + 1] >= THRESHOLD_HIGH || cannypixels[(rows - 1) * width + cols] >= THRESHOLD_HIGH || cannypixels[(rows + 1) * width + cols] >= THRESHOLD_HIGH || cannypixels[(rows + 1) * width + cols - 1] >= THRESHOLD_HIGH || cannypixels[(rows - 1) * width + cols - 1] >= THRESHOLD_HIGH || cannypixels[(rows - 1) * width + cols + 1] >= THRESHOLD_HIGH || cannypixels[(rows + 1) * width + cols + 1] >= THRESHOLD_HIGH) temp = 255;
            else temp = 0;
            //temp = custom_canny.at<uint8_t>(rows, cols); //255;
        }

        temp_canny.at<uint8_t>(rows, cols) = temp;
        //printf("%d\t", ocv_canny_false.at<uint8_t>(rows, cols));
    }
    //printf("\n");
}

I'd be grateful for any ideas on what the problem might be.

Replicating OpenCV's Canny Edge Detection

Hi,

I'm currently working on replicating the Canny edge detector in OpenCV. I've managed to replicate the built-in Sobel operator using a 3x3 Gaussian filter and by calculating the magnitude as a weighted sum of absolute values of Gx and Gy (L2gradient is set to FALSE when calling Canny).

Sample outputs below:

original image

OpenCV Sobel

My Sobel operator

My Sobel operator after non-max

OpenCV Canny (L2gradient = FALSE)

My Canny EDoriginal image

OpenCV Sobel

My Sobel operator

My Sobel operator after non-max

OpenCV Canny (L2gradient = FALSE)

My Canny ED

I assume the problem is in thresholding. This is how I imagined doing it:

for (int rows = 1; rows < height-1; rows++)
{
    for (int cols = 1; cols < width-1; cols++)
    {
        temp = custom_canny.at<uint8_t>(rows, cols);

        //thresholding
        if (temp >= THRESHOLD_HIGH) temp = 255;
        else if (temp < THRESHOLD_LOW) temp = 0;

        else
        {
            if (cannypixels[rows * width + cols - 1] >= THRESHOLD_HIGH || cannypixels[rows * width + cols + 1] >= THRESHOLD_HIGH || cannypixels[(rows - 1) * width + cols] >= THRESHOLD_HIGH || cannypixels[(rows + 1) * width + cols] >= THRESHOLD_HIGH || cannypixels[(rows + 1) * width + cols - 1] >= THRESHOLD_HIGH || cannypixels[(rows - 1) * width + cols - 1] >= THRESHOLD_HIGH || cannypixels[(rows - 1) * width + cols + 1] >= THRESHOLD_HIGH || cannypixels[(rows + 1) * width + cols + 1] >= THRESHOLD_HIGH) temp = 255;
            else temp = 0;
            //temp = custom_canny.at<uint8_t>(rows, cols); //255;
        }

        temp_canny.at<uint8_t>(rows, cols) = temp;
        //printf("%d\t", ocv_canny_false.at<uint8_t>(rows, cols));
    }
    //printf("\n");
}

I'd be grateful for any ideas on what the problem might be.

Replicating OpenCV's Canny Edge Detection

Hi,

I'm currently working on replicating the Canny edge detector in OpenCV. I've managed to replicate the built-in Sobel operator using a 3x3 Gaussian filter and by calculating the magnitude as a weighted sum of absolute values of Gx and Gy (L2gradient is set to FALSE when calling Canny).

Sample outputs below:

original image:

original image

OpenCV Sobel:

OpenCV Sobel

My Sobel operator:

My Sobel operator

My Sobel operator after non-max:

My Sobel operator after non-max

OpenCV Canny (L2gradient = FALSE):

OpenCV Canny (L2gradient = FALSE)

My Canny ED:

My Canny ED

I assume the problem is in thresholding. This is how I imagined doing it:

for (int rows = 1; rows < height-1; rows++)
{
    for (int cols = 1; cols < width-1; cols++)
    {
        temp = custom_canny.at<uint8_t>(rows, cols);

        //thresholding
        if (temp >= THRESHOLD_HIGH) temp = 255;
        else if (temp < THRESHOLD_LOW) temp = 0;

        else
        {
            if (cannypixels[rows * width + cols - 1] >= THRESHOLD_HIGH || cannypixels[rows * width + cols + 1] >= THRESHOLD_HIGH || cannypixels[(rows - 1) * width + cols] >= THRESHOLD_HIGH || cannypixels[(rows + 1) * width + cols] >= THRESHOLD_HIGH || cannypixels[(rows + 1) * width + cols - 1] >= THRESHOLD_HIGH || cannypixels[(rows - 1) * width + cols - 1] >= THRESHOLD_HIGH || cannypixels[(rows - 1) * width + cols + 1] >= THRESHOLD_HIGH || cannypixels[(rows + 1) * width + cols + 1] >= THRESHOLD_HIGH) temp = 255;
            else temp = 0;
            //temp = custom_canny.at<uint8_t>(rows, cols); //255;
        }

        temp_canny.at<uint8_t>(rows, cols) = temp;
        //printf("%d\t", ocv_canny_false.at<uint8_t>(rows, cols));
    }
    //printf("\n");
}

I'd be grateful for any ideas on what the problem might be.