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};\\高斯模糊算子

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

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;
}

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算子

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

幅值

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;
}

方向

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;
}

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 ;
}

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 ;
}

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 ;
}

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;
}

将图片转化为矩阵

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;
}

三、全部代码

#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;
}


你可能感兴趣的:(opencv,算法,c++)