Replicating OpenCV's Canny Edge Detection [closed]

asked 2020-06-01 05:32:03 -0600

cohadruid gravatar image

updated 2020-06-01 08:22:00 -0600

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.

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-10-12 07:49:25.585425

Comments

if you share the whole code we can try it.

sturkmen gravatar imagesturkmen ( 2020-06-01 08:12:33 -0600 )edit

Hi, the code is here

cohadruid gravatar imagecohadruid ( 2020-06-06 06:25:12 -0600 )edit

i think you can find the answer here

sturkmen gravatar imagesturkmen ( 2020-10-12 07:50:29 -0600 )edit