
边缘像素是图像中的灰度突然改变的像素,边缘是连接的边缘像素的集合. 边缘检测是一种用于检测边缘像素的局部图像处理方法.
使用<;
中引入的拉普拉斯算子
输出图像是

卷积模板

该代码之前已经实现,因此不会在本文中进行测试
图像渐变


渐变矢量大小

在图像处理过程中,平方和和平方根的计算速度较慢,因此简化为以下计算方法

梯度矢量的方向与x轴之间的角度

根据不同的偏导数计算方法,获得了不同的边缘检测模板

原始图片
使用Sobel模板检测水平边缘


使用Sobel模板检测垂直边缘

添加两个

代码实现
1 void edge_detection(short** in_array, short** out_array, long height, long width) 2 { 3 short gx = 0, gy = 0; 4 short** a_soble1; 5 short** a_soble2; 6 7 a_soble1 = allocate_image_array(3, 3); 8 a_soble2 = allocate_image_array(3, 3); 9 for (int i = 0; i < 3; i++){ 10 for (int j = 0; j < 3; j++){ 11 a_soble1[i][j] = soble1[i][j]; 12 a_soble2[i][j] = soble2[i][j]; 13 } 14 } 15 for (int i = 0; i < height; i++){ 16 for (int j = 0; j < width; j++){ 17 gx = convolution(in_array, i, j, height, width, a_soble1, 3); 18 gy = convolution(in_array, i, j, height, width, a_soble2, 3); 19 // out_array[i][j] = gx; 20 // out_array[i][j] = gy; 21 out_array[i][j] = gx + gy; 22 if (out_array[i][j] < 0) 23 out_array[i][j] = 0; 24 else if (out_array[i][j] > 0xff) 25 out_array[i][j] = 0xff; 26 } 27 } 28 free_image_array(a_soble1, 3); 29 free_image_array(a_soble2, 3); 30 }

Sobel 45°检测模板


Sobel -45°检测模板

添加两个

代码已实现,只需替换模板值
1. 采样二维高斯函数以获得高斯低通滤波器,对输入图像进行滤波,滤波器模板的大小是大于或等于6 *σ的最小奇数整数

算法实现
1 void generate_gaussian_filter(double** gaussian_filter, long sigma) 2 { 3 double x, y; 4 long filter_size = 6 * sigma + 1; 5 6 for (int i = 0; i < filter_size; i++){ 7 for (int j = 0; j < filter_size; j++){ 8 x = i - filter_size / 2; 9 y = j - filter_size / 2; 10 gaussian_filter[i][j] = exp(-1.0 * ((pow(x, 2) + pow(y, 2)) / 2 * sigma * sigma)); 11 } 12 } 13 }
2. 使用以下模板,计算第一步中获得的图像的拉普拉斯算子


算法实现
1 void laplace(short** in_array, short** out_array, long height, long width) 2 { 3 short** a_sharpen; 4 5 a_sharpen = allocate_image_array(3, 3); 6 for (int i = 0; i < 3; i++){ 7 for (int j = 0; j < 3; j++){ 8 a_sharpen[i][j] = sharpen[i][j]; 9 } 10 } 11 for (int i = 0; i < height; i++){ 12 for (int j = 0; j < width; j++){ 13 out_array[i][j] = convolution(in_array, i, j, height, width, a_sharpen, 3); 14 } 15 } 16 free_image_array(a_sharpen, 3); 17 }
运行结果

3. 寻找零交叉,对于任何像素p,测试上下两个,对角线c图像识别,两个对角线的四个位置,当存在两对符号不同且绝对值差大于某个阈值的点时为零.
算法实现
1 int is_cross(short** in_array, long row, long column) 2 { 3 int cross_num = 0; 4 5 if (in_array[row-1][column-1] * in_array[row+1][column+1] < 0 && 6 abs(abs(in_array[row-1][column-1]) - abs(in_array[row+1][column+1])) > 0x66) 7 cross_num++; 8 if (in_array[row-1][column] * in_array[row+1][column] < 0&& 9 abs(abs(in_array[row-1][column]) - abs(in_array[row+1][column])) > 0x66) 10 cross_num++; 11 if (in_array[row-1][column+1] * in_array[row+1][column-1] < 0&& 12 abs(abs(in_array[row-1][column+1]) - abs(in_array[row+1][column-1])) > 0x66) 13 cross_num++; 14 if (in_array[row][column-1] * in_array[row][column+1] < 0&& 15 abs(abs(in_array[row][column-1]) - abs(in_array[row][column+1])) > 0x66) 16 cross_num++; 17 18 if (cross_num >= 2) 19 return 1; 20 else 21 return 0; 22 }
1 void marr(short** in_array, short** out_array, long height, long width) 2 { 3 long sigma = 2; 4 long filter_size = 6 * sigma + 1; 5 double** gaussian_filter; 6 short **gauss_array, **laplace_array; 7 8 gaussian_filter = allocate_double_array(filter_size, filter_size); 9 gauss_array = allocate_image_array(height, width); 10 laplace_array = allocate_image_array(height, width); 11 generate_gaussian_filter(gaussian_filter, sigma); 12 13 for (int i = 0; i < height; i++){ 14 for (int j = 0; j < width; j++){ 15 gauss_array[i][j] = convolutiond(in_array, i, j, height, width, gaussian_filter, filter_size); 16 } 17 } 18 printf("Gasuuian filter done\n"); 19 laplace(gauss_array, laplace_array, height, width); 20 printf("Laplace done\n"); 21 zero_cross(laplace_array, out_array, height, width); 22 printf("Zero cross done\n"); 23 24 free_double_array(gaussian_filter, filter_size); 25 free_image_array(gauss_array, height); 26 free_image_array(laplace_array, height); 27 }
最终运行结果

可以看出,该算法检测到的边缘与物体的真实边缘更加吻合,但是这些边缘由离散点组成c图像识别,因此需要进行边缘连接以进行进一步处理,本文将不再赘述. 在这里,有兴趣的读者可以进行更深入的研究.
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-177804-1.html
特工在我国从事反华活动吗
美国欺负到门上了