Bilateral Filter

實驗報告

專業(yè):軟件工程________
姓名:陳錳____________
學(xué)號:3170105197______
日期:2018/12/16_______

課程名稱:____圖像信息處理___ 指導(dǎo)老師:____宋明黎____成績:__________________
實驗名稱:___Bilateral Filter____

一、實驗?zāi)康暮鸵?/h4>

學(xué)習(xí)和認(rèn)識圖像的濾波和增強原理,加深對圖像離散值中的一階微分和二階微分原理的理解,通過實踐操作熟悉均值濾波、中值濾波、高斯濾波和拉普拉斯變換方法,進(jìn)一步掌握空間濾波原理和技術(shù)。

二、實驗內(nèi)容和原理

三、實驗結(jié)果

1.結(jié)果分析
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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容