圖形學-鋭化-拉普拉斯(Laplacian)運算元

來源:互聯網
上載者:User

在映像擷取,傳輸過程中有許多因素會使映像變得模糊。映像模糊的實質是映像受到了求和,平均或者積分運算,因此可以用相反的運算來消除模糊,叫做映像的鋭化。

映像鋭化的其中一個方法是拉普拉斯(Laplacian)運算元,拉普拉斯運算元是線性2次微分運算元,具有各向同性和位移不變性。

對於連續映像f(x,y),他的拉普拉斯運算元為

D^2 f = @^2 f(x,y)/@x^2+@^2 f(x,y)/@y^2     編輯關係,左式D表示Delta符號,D^2表示Delta平方,@是偏導符號,@^2 f(x,y)/@x^2 是f(x,y)對x的2次偏導數....-__-++!

處理過程為
g(x,y)=f(x,y)-k*D^2 f
即目標灰階是原始灰階減去拉普拉斯運算元的k倍,k是係數

對於數位影像矩陣,拉普拉斯運算元變成
D^2 f(x,y)=Dx^2 f(x,y)+ Dy^2 f(x,y)
上面Dx^2 f(x,y)=Dx(Dx f(x,y))=Dx(f(x+1,y)-f(x,y))=D f(x+1,y)-D f(x,y)=f(x+1,y)-f(x,y)-f(x,y)+f(x-1,y)
=f(x+1,y)+f(x-1,y)-2*f(x,y)
類似求得
Dy^2 f(x,y)=f(x,y+1)+f(x,y-1)-2* f(x,y)
所以 D^2 f(x,y)=f(x+1,y)+f(x-1,y)+f(x,y-1)+f(x,y+1)-4*f(x,y)
鋭化後得映像為
g(x,y)=f(x,y)-D^2 f(x,y)

下面是根據整個演算法對24位彩色BMP映像鋭化得BCB6程式
//------------------------------------------BCB6程式

#include <vcl.h>
#pragma hdrstop
#include<stdio.h>
#include "Unit1.h"
#include"File1.h"
#include<math.h>

#pragma pack(1)

/*
        程式:圖形學-拉普拉斯運算元映像鋭化
        作者:sboom(Lingch)
        日期:12月26日
*/
//BMP檔案頭
struct BITMAPFILEHEADER_
{
 short type;
        int bfSize;
 short re1,re2;
        int Offbits;
};
//BMP資訊頭
struct BITMAPINFO_
{
 long size;
 long width,height;
 short planes,bitCount;
 long comp,sizeImg;
 long xpels,ypels;
 long used,important;
};
//BMP彩色表項
struct COLOR_
{
        char blue,green,red;
};
//------將BMP彩色表的資料校正到BCB 的TColor的資料。
TColor* SwitchColor(unsigned char r,unsigned char g,unsigned char b)
{
        TColor *re=new TColor;
        *re=(r  | g<<8 | b<<16 );
        *re=*re & 0x00ffffff;
        return re;
}

void xxx()
{
        FILE *f=fopen("f://8.bmp","rb");
        if(f==NULL)             /*判斷檔案是否開啟成功*/
        {
             ShowMessage("File open error");
             return;
        }

        fseek(f,0,0);//移動到開頭

        //----------讀BMP檔案頭
        BITMAPFILEHEADER_ *bmph=new BITMAPFILEHEADER_();
        if(fread((char*)bmph,sizeof(BITMAPFILEHEADER_),1,f)==NULL)
        {
              ShowMessage("File read error");
             return;
        }

        //-----------讀BMP資訊頭
        BITMAPINFO_ *bmpi=new BITMAPINFO_();
        if(fread((char*)bmpi,sizeof(BITMAPINFO_),1,f)==NULL)
        {
                ShowMessage("File read error2");
                return;
        }
        fseek(f,bmph->Offbits,0);
        //----------顯示一些資訊
        Form1->Edit1->Text=IntToStr(bmph->bfSize);
        Form1->Edit2->Text=IntToStr(bmpi->width);
        Form1->Edit3->Text=IntToStr(bmpi->height);
        Form1->Edit4->Text=IntToStr(bmpi->comp);
        Form1->Edit5->Text=IntToStr(bmpi->used);

        int i,j,k,l,wc,pos;
        long N=bmph->bfSize- bmph->Offbits;//象素總數
        COLOR_ *image=new COLOR_[N]; //位元影像矩陣
        COLOR_ *newimage=new COLOR_[N];//濾波後的位元影像矩陣

        fread(image,N*3,1,f);//讀入位元影像矩陣
        long t=0;

       //!!!!!!!!!!!!!!!下面進行鋭化處理!!!!!!!!!!!!!!!!!!!!!!!!
       for(i=1;i<N-bmpi->width;i++)      //最後一行無法求梯度
       {
                 t=image[i+1].red+image[i-1].red+image[i+bmpi->width-1].red+image[i+bmpi->width+1].red-4*image[i].red;
                 newimage[i].red=image[i].red-t;

                

t=image[i+1].green+image[i-1].green+image[i+bmpi->width-1].green+image[i+bmpi->width+1].green-4*image[i].green;
                 newimage[i].green=image[i].green-t;

                 t=image[i+1].blue+image[i-1].blue+image[i+bmpi->width-1].blue+image[i+bmpi->width+1].blue-4*image[i].blue;
                 newimage[i].blue=image[i].blue-t;
       }
        //!!!!!!!!!!!!!!!進行鋭化處理結束!!!!!!!!!!!!!!!!!!!!!!!!
        //---------------顯示圖形
        COLOR_ color;
        TColor *tc;
        if(bmpi->width%4==0)//-----------因為BMP映像4位元組對齊
                wc=bmpi->width/4*4;
        else
                wc=(bmpi->width/4+1)*4;

        pos=0;
        for( i=0;i<bmpi->height;i++)
        {
                for(j=0;j<wc;j++)
                {
                //-----原始圖形
                color=image[pos];
                tc=SwitchColor(color.red,color.green,color.blue);
                Form1->Canvas->Pixels[10+j][600-i]=*tc;
                //------新圖形
                color=newimage[pos];
                tc=SwitchColor(color.red,color.green,color.blue);
                Form1->Canvas->Pixels[400+j][600-i]=*tc;
                pos++;
                }
        }      
        fclose(f);
}

//------------------------------------------
在對一幅24位BMP彩色映像鋭化後發現,灰階變化平緩得平面地區鋭化效果可以和PhotoShop得鋭化慮鏡相比,但灰階突變得邊沿地區會變得非常突出,幾乎成了邊沿提取。在我得映像中,舞台上得幕布是鋭化得最自然得,而舞台上得汽車邊沿邊框過於突出,邊沿線成為鮮豔顏色的線條。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.