美文网首页
Bilateral Filter

Bilateral Filter

作者: 言不由衷_4cca | 来源:发表于2018-12-27 19:42 被阅读0次

    实验报告

    专业:软件工程________
    姓名:陈锰____________
    学号:3170105197______
    日期:2018/12/16_______

    课程名称:____图像信息处理___ 指导老师:____宋明黎____成绩:__________________
    实验名称:___Bilateral Filter____

    一、实验目的和要求

    学习和认识图像的滤波和增强原理,加深对图像离散值中的一阶微分和二阶微分原理的理解,通过实践操作熟悉均值滤波、中值滤波、高斯滤波和拉普拉斯变换方法,进一步掌握空间滤波原理和技术。

    二、实验内容和原理

    三、实验结果

    1.结果分析
    Hermione_former
    Hermione_later
    2.源代码
    #include<stdio.h>
    #include<stdlib.h>
    #include<atlimage.h>
    #define SIZE 5              //Define the size for the mask
    #define OFFSET (SIZE / 2)   //Define the offset of the mask
    #define SIGMA_S 20          //Define sigma_s for space domain
    #define SIGMA_R 10          //Define sigma_r for range domain
    
    double Wbf(double ** mask_s, double ** mask_r);  //Caculate the normalization coefficient
    double ** Mask_S(double **mask, double sigma_s);  //Generate mask_s for space domain
    double ** Mask_R(double **mask, double sigma_r, int x, int y);  //Generate mask_r for range domain
    void BilateralFilter(CImage image, double ** mask_s, double ** mask_r);  //Implementation of Bilateral Filter
    int main()
    {
        CImage image;
        image.Load("testBMP.bmp");
        double ** mask_s = NULL, **mask_r = NULL;
        //Initialize mask_s, mask_r
        mask_s = (double**)malloc(SIZE * sizeof(double*));
        mask_r = (double**)malloc(SIZE * sizeof(double*));
        for (int i = 0; i < SIZE; i++)
        {
            mask_s[i] = (double*)malloc(SIZE * sizeof(double));
            mask_r[i] = (double*)malloc(SIZE * sizeof(double));
        }
        BilateralFilter(image, mask_s, mask_r);
        getchar();
        return 0;
    }
    
    
    double Gaussian(double sigma, double x)
    {
        //Gaussian function without the constant coefficient
        return exp(-0.5 * x * x / (sigma * sigma));
    }
    
    double ** Mask_S(double **mask, double sigma_s)
    {
        //Mask_S is obtained based on the position of pixels around the key pixel
        //Horizontal and vertical distance are considered alike
        for (int i = -OFFSET; i <= OFFSET; i++)
            for (int j = -OFFSET; j <= OFFSET; j++)
                mask[i + OFFSET][j + OFFSET] = Gaussian(sigma_s, i) * Gaussian(sigma_s, j);
        return mask;
    }
    double ** Mask_R(double **mask, double sigma_r, int x, int y)
    {
        CImage Bmp;
        COLORREF color1, color2;
        int r, g, b;
        Bmp.Load("testBMP.bmp");
        //Get RGB value of the key piexl
        color1 = Bmp.GetPixel(x, y);
        r = GetRValue(color1);
        g = GetGValue(color1);
        b = GetBValue(color1);
        for (int i = -OFFSET; i <= OFFSET; i++)
            for (int j = -OFFSET; j <= OFFSET; j++)
            {
                //R,G,B channels are considered based on the difference value btween key pixel and others
                color2 = Bmp.GetPixel(x + i, y + j);
                //Caculate the difference value btween key pixel and others respectively
                int dR = r - GetRValue(color2);
                int dG = g - GetGValue(color2);
                int dB = b - GetBValue(color2);
                //Combine the effect of the three channels and mask_r is acquired
                mask[i + OFFSET][j + OFFSET] = Gaussian(sigma_r, dR) * Gaussian(sigma_r, dG) * Gaussian(sigma_r, dB);
            }
        return mask;
    }
    
    double Wbf(double ** mask_s, double ** mask_r)
    {
        double wbf = 0;
        //Multiple each pair of elements in this two masks, the sum them up
        for (int i = 0; i < SIZE; i++)
            for (int j = 0; j < SIZE; j++)
                wbf += mask_s[i][j] * mask_r[i][j];
        return wbf;
    }
    
    //Implementation of weighted bilateral filter
    void BilateralFilter(CImage image, double ** mask_s, double ** mask_r)
    {
        printf("Waite a minute...");
        CImage bmp = image;
        //Get mask_s invoking Mask_S
        mask_s = Mask_S(mask_s, SIGMA_S);
        for (int i = OFFSET; i < bmp.GetWidth() - OFFSET; i++)
        {
            for (int j = OFFSET; j < bmp.GetHeight() - OFFSET; j++)
            {
                COLORREF color;
                double r = 0, g = 0, b = 0;
                //Get real time mask_r invoking Mask_R
                mask_r = Mask_R(mask_r, SIGMA_R, i, j);
                double w = Wbf(mask_s, mask_r);
                //Caculate the value of goal pixel with mask_s and mask_r
                for (int pi = -OFFSET; pi <= OFFSET; pi++)
                    for (int pj = -OFFSET; pj <= OFFSET; pj++)
                    {
                        color = image.GetPixel(i + pi, j + pj);
                        double k = mask_s[OFFSET + pi][OFFSET + pj] * mask_r[OFFSET + pi][OFFSET + pj];
                        r += k * GetRValue(color);
                        g += k * GetGValue(color);
                        b += k * GetBValue(color);
                    }
                //Rearrange the value of RGB avoid overflow
                r = (int)(r / w + 0.5);
                g = (int)(g / w + 0.5);
                b = (int)(b / w + 0.5);
                r = r > 255 ? 255 : r;
                r = r < 0 ? 0 : r;
                g = g > 255 ? 255 : g;
                g = g < 0 ? 0 : g;
                b = b > 255 ? 255 : b;
                b = b < 0 ? 0 : b;
                //Set pixel
                bmp.SetPixelRGB(i, j, (BYTE)r, (BYTE)g, (BYTE)b);
            }
        }
        //Save the new image and a success prompt will be given on the console
        bmp.Save("BilateralFilter.bmp");
        printf("\nBilateral Filter Successful!\n");
    }
    

    List * GenerateGraph(int *hashTable, List *graph, int N)
    {
    int i, j;
    List * p;
    graph = (List )calloc(N, sizeof(struct list));
    for(i = 0; i < N; i++)
    if(hashTable[i] >= 0)
    {
    graph[i] = (List)malloc(sizeof(struct list));
    graph[i]->Name = i;
    graph[i]->Next = NULL;
    }
    for(i = 0; i < N; i++)
    if(hashTable[i] >= 0 && hashTable[i] % N != i)
    for(j = hashTable[i] % N; j != i; j = (j == N - 1 ? 0 : j + 1))
    graph[j] = Insert(i, graph[j]);
    return graph;
    }

    List Insert(int name, List L)
    {
        int i;
        List p;
        p = (List)malloc(sizeof(struct list));
        p->Name = name;
        p->Next = NULL;
        if(L->Next == NULL)
            L->Next = p;
        else
        {
            p->Next = L->Next;
            L->Next = p;
        }
        return L;
    }

    相关文章

      网友评论

          本文标题:Bilateral Filter

          本文链接:https://www.haomeiwen.com/subject/sdmdlqtx.html