Im currently try to make Canny edge detection using stb_imahe and c++ without 3rfparty library My code:
// Generate gaussian kernel
int size = 5, center = (size-1) / 2;
float sigma = 1.4f;
std::vector<std::vector<double>> kernel(size, std::vector<double>(size));
double sum = 0.0;
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size; j++)
{
int x=j-(center+1),y=i-(center+1);
kernel[i][j]=(1.0/(2*M_PI*sigma*sigma))*exp(-(x*x+y*y)/(2*sigma*sigma));
sum+=kernel[i][j];
}
}debug(kernel);
// Normalize gausian kernel
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size; j++)
{
kernel[i][j]/=sum;
}
} debug(kernel);
// Apply gaussian filter to image
std::vector<unsigned char> temp(size*channels);
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
for(int c = 0; c < 3; c++)
{
double sum = 0;
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size; j++)
{
int neighbor_x = x + j - center;
int neighbor_y = y + i - center;
int clamped_x = std::min(std::max(neighbor_x, 0), width - 1);
int clamped_y = std::min(std::max(neighbor_y, 0), height - 1);
sum += kernel[i][j] * data[(clamped_y*width+clamped_x)*channels+c];
}
}
temp[(y*width+x)*channels+c]=static_cast<unsigned char>(sum);
}
}
}
std::copy(temp.data(), temp.data()+size*channels, data);
temp.clear();
stbi_write_png("gaussian.png", width, height, channels, data, width*channels);
the picture i use from wikipedia rgb format from Canny edge detection
(in my code i copy to here is removing the grayscale conversion)
when i run in grayscale is normal but the gaussian is have problem it show the top part is likely true (apply blur) but the remaining is still normal and i swap some part for checking it wasnt apply blur
Please help me