当前位置:   article > 正文

canny算法:边界检测算法C++代码_canny边缘检测手写代码c

canny边缘检测手写代码c

一、算法原理

canny边缘检测算法原理

二、程序分块介绍

1、高斯模糊。

float GasArray[9]= {0.0751,0.1238,0.0571,0.1238,0.2043,0.1238,0.0751,0.1238,0.0751};\\高斯模糊算子
  • 1

两个矩阵的卷积(未采用傅里叶变换)

float *Array_Cov_f_f_f(float  *input,int input_x,int input_y,float  *cov,int cov_x,int cov_y)
{
    if(input_x<cov_x||input_y<cov_y)
    {
        printf("The size of input array is smaller than cov's.\n");
        return NULL;
    }
    float *output;
    int x,y;
    float sum = 0;

    x = input_x - cov_x + 1,y = input_y - cov_y + 1;
    output = (float*)malloc(x*y*sizeof(float));

    for(int i=0; i<x; i++)
        for(int j=0; j<y; j++)
        {
            sum  = 0;
            for( int k=0; k<cov_x; k++)
                for(int l=0; l<cov_y; l++)
                    sum+=input[(i+k)*input_y+j+l]*cov[k*cov_y+l];
            output[i*y+j] = sum;`在这里插入代码片`
        }
    return output;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

2、计算梯度幅值和方向。

float soble_x[9] = {-1,0,1,-2,0,2,-1,0,1};
float soble_y[9] = {1,2,1,0,0,0,-1,-2,-1};\\soble算子
  • 1
  • 2

先分别用两个算子与原图像矩阵进行卷积,再用两个矩阵计算梯度幅值和方向

幅值

float *Two_array_geometry_avg(float *input_1,float *input_2,int input_x,int input_y)
{
    float *output;

    output = (float*)malloc(input_x*input_y*sizeof(float));

    for(int i=0; i<input_x; i++)
        for(int j=0; j<input_y; j++)
            output[i*input_y+j] = sqrt(input_1[i*input_y+j]*input_1[i*input_y+j]+input_2[i*input_y+j]+input_2[i*input_y+j]);

    return output;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

方向

int *Gradient_direction(float *input_1,float *input_2,int input_x,int input_y)
{
    float direction;
    int *output;

    output = (int*)malloc(input_x*input_y*sizeof(int));

    for(int i=0; i<input_x; i++)
        for(int j=0; j<input_y; j++)
        {
            direction = atan(input_2[i*input_y+j]/input_1[i*input_y+j]);
            if(direction >= -0.3926990816987 && direction < 0.3926990816987)
                output[i*input_y+j] = 0;
            else if(direction >= 0.3926990816987 &&direction < 1.1780972450961)
                output[i*input_y+j] = 1;
            else if(direction >= 1.1780972450961 && direction < -1.1780972450961)
                output[i*input_y+j] =2;
            else
                output[i*input_y+j] =3;
        }
    return output;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

3、 非最大值抑制。

void Non_maximum_suppression(float *input_1,int *input_2,int input_x,int input_y)
{
    int x,y;
    x=input_x-1;
    y=input_y-1;
    for(int i=1; i<x; i++)
        for(int j=1; j<y; j++)
        {
            switch(input_2[i*input_y+j])
            {
            case 0:
                if(input_1[i*input_y+j+1]<input_1[i*input_y+j] &&input_1[i*input_y+j-1]<input_1[i*input_y+j]);
                else
                    input_1[i*input_y+j] = 0;
                break;
            case 1:
                if(input_1[(i+1)*input_y+j+1]<input_1[i*input_y+j] &&input_1[(i-1)*input_y+j-1]<input_1[i*input_y+j]);
                else
                    input_1[i*input_y+j] = 0;
                break;
            case 2:
                if(input_1[(i+1)*input_y+j]<input_1[i*input_y+j] &&input_1[(i-1)*input_y+j]<input_1[i*input_y+j]);
                else
                    input_1[i*input_y+j] = 0;
            case 3:
                if(input_1[(i+1)*input_y+j-1]<input_1[i*input_y+j] &&input_1[(i-1)*input_y+j+1]<input_1[i*input_y+j]);
                else
                    input_1[i*input_y+j] = 0;
            }
        }
    return ;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

4、双阈值

void Double_threshold(float *input_1,int input_x,int input_y,float big,float small)
{
    int x,y;
    float smaller = (big + small)/2;

    x=input_x-1;
    y=input_y-1;
    for(int i=1; i<x; i++)
        for(int j=1; j<y; j++)
        {
            if(small<input_1[i*input_y+j] &&big>input_1[i*input_y+j])
                 input_1[i*input_y+j] = small;
            else if(input_1[i*input_y+j]>big)
                input_1[i*input_y+j] = 255;
            else
                input_1[i*input_y+j] = 0;
        }
    return ;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

5、滞后边界跟踪

算法感觉有很大问题

void Neighborhood_tracking(float *input,int input_x,int input_y,float big,float small)
{
    int *sign;
    int x,y;


    x = input_x -1, y =input_y-1;
    sign = (int*)malloc(input_x*input_y*sizeof(int));

    memset(sign,0,sizeof(sign));
    for(int i=1; i<x; i++)
        for(int j=1; j<y; j++)
        {
            if(!sign[i*input_y+j])
            {
                if(input[i*input_y+j] == 255)
                {
                    dfs(input,input_x,input_y,i,j,small,sign);
                }
            }
        }

    free(sign);
    return ;
}

void dfs(float *input,int input_x,int input_y,int now_x,int now_y,float small,int *sign)
{
    if(sign[now_x*input_y+now_y] == 0)
    {
        sign[now_x*input_y+now_y] = 1;
        int i,j;
        for(int k=0; k<8; k++)
        {
            i=now_x+dx[k],j=now_y+dy[k];
            if(i>=input_x||j>=input_y||i<=0 ||j<=0)
                continue;
            if(input[i*input_y+j] == small)
            {
                input[i*input_y+j] = 255;
                dfs(input,input_x,input_y,i,j,small,sign);
            }
        }

    }
    return ;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

6、其他函数

将矩阵转化为图片

Mat Tr_array_Mat_gray(float *input,int input_x,int input_y)
{
    Mat output;
    output = Mat(input_x,input_y,0);
    for(int i=0; i<input_x; i++)
        for(int j=0; j<input_y; j++)
        {
            output.at<uchar>(i,j) = (uchar)input[i*input_y+j];
        }
    return output;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

将图片转化为矩阵

float *Tr_Mat_array_gray(Mat input)
{
    float *output;
    int x,y;
    x = input.rows,y = input.cols;

    output = (float *)malloc(x*y*sizeof(float));
    for(int i=0; i<x; i++)
        for(int j=0; j<y; j++)
        {
            output[i*y+j] = (float)input.at<uchar>(i,j);
        }
    return output;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

三、全部代码

#include <iostream>
#include<opencv2/opencv.hpp>
#include<cstdio>
#include<cmath>
#include<cstring>

using namespace std;
using namespace cv;

float GasArray[9]= {0.0751,0.1238,0.0571,0.1238,0.2043,0.1238,0.0751,0.1238,0.0751};
float soble_x[9] = {-1,0,1,-2,0,2,-1,0,1};
float soble_y[9] = {1,2,1,0,0,0,-1,-2,-1};
int dx[8]= {-1,-1,-1,1,1,1,0,0};
int dy[8]= {0,1,-1,-1,1,0,-1,1};

int gas_x = 3,gas_y = 3;
int soble = 3;

float *Array_Cov_f_f_f(float  *input,int input_x,int input_y,float  *cov,int cov_x,int cov_y);
float *Tr_Mat_array_gray(Mat input);
float *Two_array_geometry_avg(float *input_1,float *input_2,int input_x,int input_y);
int *Gradient_direction(float *input_1,float *input_2,int input_x,int input_y);
void Non_maximum_suppression(float *input_1,int *input_2,int input_x,int input_y);
void Double_threshold(float *input_1,int input_x,int input_y,float big,float small);
void Neighborhood_tracking(float *input,int input_x,int input_y,float big,float small);
Mat Tr_array_Mat_gray(float *input,int input_x,int intput_y);
void dfs(float *input,int input_x,int input_y,int now_x,int now_y,float small,int *sign);
float *Two_array_arithmetic_avg(float *input_1,float *input_2,int input_x,int input_y);

int main()
{
    Mat img,img_gas,img_soble_x,img_soble_y,img_soble,img_nms,img_dt,img_nt;
    float *img_array;
    float *img_gas_array;
    float *img_soble_x_array,*img_soble_y_array,*img_soble_array;
    int *direction;

    img_array = NULL;
    img_gas_array = NULL;
    img = imread("1.jpg",0);


    img_array = Tr_Mat_array_gray(img);

    img_gas_array = Array_Cov_f_f_f(img_array,img.rows,img.cols,GasArray,gas_x,gas_y);
    img_gas = Tr_array_Mat_gray(img_gas_array,img.rows-gas_x+1,img.cols-gas_y+1);

    img_soble_x_array = Array_Cov_f_f_f(img_gas_array,img_gas.rows,img_gas.cols,soble_x,soble,soble);
    img_soble_x = Tr_array_Mat_gray(img_soble_x_array,img_gas.rows-soble+1,img_gas.cols-soble+1);

    img_soble_y_array = Array_Cov_f_f_f(img_gas_array,img_gas.rows,img_gas.cols,soble_y,soble,soble);
    img_soble_y = Tr_array_Mat_gray(img_soble_y_array,img_gas.rows-soble+1,img_gas.cols-soble+1);

    img_soble_array = Two_array_arithmetic_avg(img_soble_x_array,img_soble_y_array,img_gas.rows-soble+1,img_gas.cols-soble+1);
    img_soble = Tr_array_Mat_gray(img_soble_array,img_gas.rows-soble+1,img_gas.cols-soble+1);

    direction = Gradient_direction(img_soble_array,img_soble_y_array,img_gas.rows-soble+1,img_gas.cols-soble+1);
    Non_maximum_suppression(img_soble_array,direction,img_gas.rows-soble+1,img_gas.cols-soble+1);
    img_nms = Tr_array_Mat_gray(img_soble_array,img_gas.rows-soble+1,img_gas.cols-soble+1);

    Double_threshold(img_soble_array,img_gas.rows-soble+1,img_gas.cols-soble+1,100,30);
    img_dt = Tr_array_Mat_gray(img_soble_array,img_gas.rows-soble+1,img_gas.cols-soble+1);

    Neighborhood_tracking(img_soble_array,img_gas.rows-soble+1,img_gas.cols-soble+1,100,30);
    img_nt = Tr_array_Mat_gray(img_soble_array,img_gas.rows-soble+1,img_gas.cols-soble+1);

    free(img_array);
    free(img_gas_array);
    free(img_soble_x_array);
    free(img_soble_y_array);
    free(img_soble_array);
    free(direction);

    imshow("origl:",img);
    imshow("gas:",img_gas);
    imshow("soble_x:",img_soble_x);
    imshow("soble_y:",img_soble_y);
    imshow("soble:",img_soble);
    imshow("nms:",img_nms);
    imshow("dt:",img_dt);
    imshow("nt:",img_nt);
    waitKey(90000);



    return 0;
}

void Neighborhood_tracking(float *input,int input_x,int input_y,float big,float small)
{
    int *sign;
    int x,y;


    x = input_x -1, y =input_y-1;
    sign = (int*)malloc(input_x*input_y*sizeof(int));

    memset(sign,0,sizeof(sign));
    for(int i=1; i<x; i++)
        for(int j=1; j<y; j++)
        {
            if(!sign[i*input_y+j])
            {
                if(input[i*input_y+j] == 255)
                {
                    dfs(input,input_x,input_y,i,j,small,sign);
                }
            }
        }

    free(sign);
    return ;
}

void dfs(float *input,int input_x,int input_y,int now_x,int now_y,float small,int *sign)
{
    if(sign[now_x*input_y+now_y] == 0)
    {
        sign[now_x*input_y+now_y] = 1;
        int i,j;
        for(int k=0; k<8; k++)
        {
            i=now_x+dx[k],j=now_y+dy[k];
            if(i>=input_x||j>=input_y||i<=0 ||j<=0)
                continue;
            if(input[i*input_y+j] == small)
            {
                input[i*input_y+j] = 255;
                dfs(input,input_x,input_y,i,j,small,sign);
            }
        }

    }
    return ;
}
void Double_threshold(float *input_1,int input_x,int input_y,float big,float small)
{
    int x,y;
    float smaller = (big + small)/2;

    x=input_x-1;
    y=input_y-1;
    for(int i=1; i<x; i++)
        for(int j=1; j<y; j++)
        {
            if(small<input_1[i*input_y+j] &&big>input_1[i*input_y+j])
                 input_1[i*input_y+j] = small;
            else if(input_1[i*input_y+j]>big)
                input_1[i*input_y+j] = 255;
            else
                input_1[i*input_y+j] = 0;
        }
    return ;
}
void Non_maximum_suppression(float *input_1,int *input_2,int input_x,int input_y)
{
    int x,y;
    x=input_x-1;
    y=input_y-1;
    for(int i=1; i<x; i++)
        for(int j=1; j<y; j++)
        {
            switch(input_2[i*input_y+j])
            {
            case 0:
                if(input_1[i*input_y+j+1]<input_1[i*input_y+j] &&input_1[i*input_y+j-1]<input_1[i*input_y+j]);
                else
                    input_1[i*input_y+j] = 0;
                break;
            case 1:
                if(input_1[(i+1)*input_y+j+1]<input_1[i*input_y+j] &&input_1[(i-1)*input_y+j-1]<input_1[i*input_y+j]);
                else
                    input_1[i*input_y+j] = 0;
                break;
            case 2:
                if(input_1[(i+1)*input_y+j]<input_1[i*input_y+j] &&input_1[(i-1)*input_y+j]<input_1[i*input_y+j]);
                else
                    input_1[i*input_y+j] = 0;
            case 3:
                if(input_1[(i+1)*input_y+j-1]<input_1[i*input_y+j] &&input_1[(i-1)*input_y+j+1]<input_1[i*input_y+j]);
                else
                    input_1[i*input_y+j] = 0;
            }
        }
    return ;
}
int *Gradient_direction(float *input_1,float *input_2,int input_x,int input_y)
{
    float direction;
    int *output;

    output = (int*)malloc(input_x*input_y*sizeof(int));

    for(int i=0; i<input_x; i++)
        for(int j=0; j<input_y; j++)
        {
            direction = atan(input_2[i*input_y+j]/input_1[i*input_y+j]);
            if(direction >= -0.3926990816987 && direction < 0.3926990816987)
                output[i*input_y+j] = 0;
            else if(direction >= 0.3926990816987 &&direction < 1.1780972450961)
                output[i*input_y+j] = 1;
            else if(direction >= 1.1780972450961 && direction < -1.1780972450961)
                output[i*input_y+j] =2;
            else
                output[i*input_y+j] =3;
        }
    return output;
}
float *Two_array_geometry_avg(float *input_1,float *input_2,int input_x,int input_y)
{
    float *output;

    output = (float*)malloc(input_x*input_y*sizeof(float));

    for(int i=0; i<input_x; i++)
        for(int j=0; j<input_y; j++)
            output[i*input_y+j] = sqrt(input_1[i*input_y+j]*input_1[i*input_y+j]+input_2[i*input_y+j]+input_2[i*input_y+j]);

    return output;
}

float *Two_array_arithmetic_avg(float *input_1,float *input_2,int input_x,int input_y)
{
    float *output;

    output = (float*)malloc(input_x*input_y*sizeof(float));

    for(int i=0; i<input_x; i++)
        for(int j=0; j<input_y; j++)
            output[i*input_y+j] = abs(input_1[i*input_y+j]) + abs(input_2[i*input_y+j]);
    return output;
}
float *Tr_Mat_array_gray(Mat input)
{
    float *output;
    int x,y;
    x = input.rows,y = input.cols;

    output = (float *)malloc(x*y*sizeof(float));
    for(int i=0; i<x; i++)
        for(int j=0; j<y; j++)
        {
            output[i*y+j] = (float)input.at<uchar>(i,j);
        }
    return output;
}

Mat Tr_array_Mat_gray(float *input,int input_x,int input_y)
{
    Mat output;
    output = Mat(input_x,input_y,0);
    for(int i=0; i<input_x; i++)
        for(int j=0; j<input_y; j++)
        {
            output.at<uchar>(i,j) = (uchar)input[i*input_y+j];
        }
    return output;
}
float *Array_Cov_f_f_f(float  *input,int input_x,int input_y,float  *cov,int cov_x,int cov_y)
{
    if(input_x<cov_x||input_y<cov_y)
    {
        printf("The size of input array is smaller than cov's.\n");
        return NULL;
    }
    float *output;
    int x,y;
    float sum = 0;

    x = input_x - cov_x + 1,y = input_y - cov_y + 1;
    output = (float*)malloc(x*y*sizeof(float));

    for(int i=0; i<x; i++)
        for(int j=0; j<y; j++)
        {
            sum  = 0;
            for( int k=0; k<cov_x; k++)
                for(int l=0; l<cov_y; l++)
                    sum+=input[(i+k)*input_y+j+l]*cov[k*cov_y+l];
            output[i*y+j] = sum;
        }
    return output;
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/846127
推荐阅读
相关标签
  

闽ICP备14008679号