EX2: 用CImg改写canny算法 EX2

1. canny.h

#ifndef _CANNY_
#define _CANNY_
#include "CImg.h"
#include 

using namespace std;
using namespace cimg_library;

class canny {
private:
    CImg img; //Original Image
    CImg grayscaled; // Grayscale
    CImg gFiltered; // Gradient
    CImg sFiltered; //Sobel Filtered
    CImg angles; //Angle Map
    CImg non; // Non-maxima supp.
    CImg thres; //Double threshold and final
public:
    canny(char const*); //Constructor
    CImg toGrayScale();
    vector > createFilter(int, int, double); //Creates a gaussian filter
    CImg useFilter(CImg, vector >); //Use some filter
    CImg sobel(); //Sobel filtering
    CImg nonMaxSupp(); //Non-maxima supp.
    CImg threshold(CImg, int, int); //Double threshold and finalize picture
};

#endif

2. canny.cpp

Z
#define _USE_MATH_DEFINES
#include "canny.h"
#include 
#include 

using namespace std; 
canny::canny(char const* filename)
{
    CImg temp(filename);
    img = temp;

    if (0) // Check for invalid input
    {
        cout << "Could not open or find the image" << std::endl;
    }
    else
    {
        vector > filter = createFilter(3, 3, 1);

        //Print filter
        for (int i = 0; i canny::toGrayScale() {
    grayscaled = CImg(img.rows, img.cols, 1); // one channel
    cimg_forXY(img, x, y) 
    {
        int b = img(x, y, 0);
        int g = img(x, y, 1);
        int r = img(x, y, 2);
        double newValue = (r * 0.2126 + g * 0.7152 + b * 0.0722);
        grayscaled(x, y) = newValue;
    }
    return grayscaled;
}

vector> canny::createFilter(int row, int column, double sigmaIn)
{
    vector> filter(row, vectordouble(col, -1));

    int row = img.row;
    int col = img.col;
    float coordSum = 0;
    float constant = 2.0 * sigmaIn * sigmaIn;

    // Sum is for normalization
    float sum = 0.0;

    for (int x = - row/2; x <= row/2; x++)
    {
        for (int y = -column/2; y <= column/2; y++)
        {
            coordSum = (x*x + y*y);
            filter[x + row/2][y + column/2] = (exp(-(coordSum) / constant)) / (M_PI * constant);
            sum += filter[x + row/2][y + column/2];
        }
    }

    // Normalize the Filter
    for (int i = 0; i < row; i++)
        for (int j = 0; j < column; j++)
            filter[i][j] /= sum;

    return filter;
}

CImg canny::useFilter(CImg img_in, vector> filterIn)
{
    int size = (int)filterIn.size()/2;
    CImg filteredImg = CImg(img_in.rows - 2*size, img_in.cols - 2*size, 1);
    for (int i = size; i < img_in.rows - size; i++)
    {
        for (int j = size; j < img_in.cols - size; j++)
        {
            double sum = 0;
            
            for (int x = 0; x < filterIn.size(); x++)
                for (int y = 0; y < filterIn.size(); y++)
                {
                    sum += filterIn[x][y] * (double)(img_in(i + x - size, j + y - size));
                }
            
            filteredImg(i-size, j-size) = sum;
        }

    }
    return filteredImg;
}

CImg canny::sobel()
{

    //Sobel X Filter
    double x1[] = {-1.0, 0, 1.0};
    double x2[] = {-2.0, 0, 2.0};
    double x3[] = {-1.0, 0, 1.0};

    vector> xFilter(3);
    xFilter[0].assign(x1, x1+3);
    xFilter[1].assign(x2, x2+3);
    xFilter[2].assign(x3, x3+3);
    
    //Sobel Y Filter
    double y1[] = {1.0, 2.0, 1.0};
    double y2[] = {0, 0, 0};
    double y3[] = {-1.0, -2.0, -1.0};
    
    vector> yFilter(3);
    yFilter[0].assign(y1, y1+3);
    yFilter[1].assign(y2, y2+3);
    yFilter[2].assign(y3, y3+3);
    
    //Limit Size
    int size = (int)xFilter.size()/2;
    
    CImg filteredImg = CImg(gFiltered.rows - 2*size, gFiltered.cols - 2*size);
    
    angles = CImg(gFiltered.rows - 2*size, gFiltered.cols - 2*size, 1); //AngleMap

    for (int i = size; i < gFiltered.rows - size; i++)
    {
        for (int j = size; j < gFiltered.cols - size; j++)
        {
            double sumx = 0;
            double sumy = 0;
            
            for (int x = 0; x < xFilter.size(); x++)
                for (int y = 0; y < xFilter.size(); y++)
                {
                    sumx += xFilter[x][y] * (double)(gFiltered(i + x - size, j + y - size)); //Sobel_X Filter Value
                    sumy += yFilter[x][y] * (double)(gFiltered(i + x - size, j + y - size)); //Sobel_Y Filter Value
                }
            double sumxsq = sumx*sumx;
            double sumysq = sumy*sumy;
            
            double sq2 = sqrt(sumxsq + sumysq);
            
            if(sq2 > 255) //Unsigned Char Fix
                sq2 =255;
            filteredImg(i-size, j-size) = sq2;
 
            if(sumx==0) //Arctan Fix
                angles(i-size, j-size) = 90;
            else
                angles(i-size, j-size) = atan(sumy/sumx);
        }
    }
    
    return filteredImg;
}


CImg canny::nonMaxSupp()
{
    CImg nonMaxSupped = CImg(sFiltered.rows-2, sFiltered.cols-2, CV_8UC1);
    for (int i=1; i(i,j-1)))
                    nonMaxSupped(i-1, j-1) = 0;
            }
            //Vertical Edge
            if (((-112.5 < Tangent) && (Tangent <= -67.5)) || ((67.5 < Tangent) && (Tangent <= 112.5)))
            {
                if ((sFiltered.at(i,j) < sFiltered(i+1,j)) || (sFiltered(i,j) < sFiltered(i-1,j)))
                    nonMaxSupped.at(i-1, j-1) = 0;
            }
            
            //-45 Degree Edge
            if (((-67.5 < Tangent) && (Tangent <= -22.5)) || ((112.5 < Tangent) && (Tangent <= 157.5)))
            {
                if ((sFiltered(i,j) < sFiltered(i-1,j+1)) || (sFiltered(i,j) < sFiltered(i+1,j-1)))
                    nonMaxSupped.at(i-1, j-1) = 0;
            }
            
            //45 Degree Edge
            if (((-157.5 < Tangent) && (Tangent <= -112.5)) || ((22.5 < Tangent) && (Tangent <= 67.5)))
            {
                if ((sFiltered(i,j) < sFiltered(i+1,j+1)) || (sFiltered(i,j) < sFiltered(i-1,j-1)))
                    nonMaxSupped(i-1, j-1) = 0;
            }
        }
    }
    return nonMaxSupped;
}

CImg canny::threshold(CImg imgin,int low, int high)
{
    if(low > 255)
        low = 255;
    if(high > 255)
        high = 255;
    
    CImg EdgeMat = CImg(imgin.rows, imgin.cols, imgin.type());
    
    for (int i=0; i high)
                EdgeMat(i,j) = 255;
            else if(EdgeMat(i,j) < low)
                EdgeMat(i,j) = 0;
            else
            {
                bool anyHigh = false;
                bool anyBetween = false;
                for (int x=i-1; x < i+2; x++) 
                {
                    for (int y = j-1; y EdgeMat.cols) //Out of bounds
                            continue;
                        else
                        {
                            if(EdgeMat(x,y) > high)
                            {
                                EdgeMat(i,j) = 255;
                                anyHigh = true;
                                break;
                            }
                            else if(EdgeMat(x,y) <= high && EdgeMat(x,y) >= low)
                                anyBetween = true;
                        }
                    }
                    if(anyHigh)
                        break;
                }
                if(!anyHigh && anyBetween)
                    for (int x=i-2; x < i+3; x++) 
                    {
                        for (int y = j-1; y EdgeMat.rows || y > EdgeMat.cols) //Out of bounds
                                continue;
                            else
                            {
                                if(EdgeMat(x,y) > high)
                                {
                                    EdgeMat(i,j) = 255;
                                    anyHigh = true;
                                    break;
                                }
                            }
                        }
                        if(anyHigh)
                            break;
                    }
                if(!anyHigh)
                    EdgeMat(i,j) = 0;
            }
        }
    }
    return EdgeMat;
}

3. 运行效果

3.1 lena

EX2: 用CImg改写canny算法 EX2_第1张图片

EX2: 用CImg改写canny算法 EX2_第2张图片

3.2 bigben

EX2: 用CImg改写canny算法 EX2_第3张图片

EX2: 用CImg改写canny算法 EX2_第4张图片

3.3 stpetro

EX2: 用CImg改写canny算法 EX2_第5张图片

EX2: 用CImg改写canny算法 EX2_第6张图片

EX2: 用CImg改写canny算法 EX2_第7张图片

3.4 twows


EX2: 用CImg改写canny算法 EX2_第8张图片

4. 参数说明

4.1 toGrayScale

主要在于double newValue = (r * 0.2126 + g * 0.7152 + b * 0.0722);这句话,把每个点转为灰色

4.2 高斯模糊

首先调用createFilter生成卷积核,再用useFilter对图像进行卷积

4.3 sobel

用Gx和Gy两个卷积核对图像进行卷积,得到梯度变化大的边界。再用非极大值抑制法剔除非边缘的点。

4.4 双阈值法

剔除那些梯度变化过小或过大的点,以消除噪声

你可能感兴趣的:(EX2: 用CImg改写canny算法 EX2)