2016年12月28日 星期三

02360635 鄭瑞銘 Homework 5

空間濾波—平滑化、銳利化

使用的方法相同,都是以目標像素和周邊九宮格內的像素,經由相關式子做計算。

參考公式:



原先只使用了兩個TImage元件分別命名為imgShowimgOrigin,用來貯存修改後的圖片以及原始圖片,這邊為了能重複對圖片做平滑或銳利,平滑銳利交互處理,另外加入了TImage元件命名為imgStore,原因是imgOrigin設定上是我不想要去更動的圖片資料,若要重複對圖片修改的話需要另外設一個空間來貯存每次修改前的圖片資料。

然後這個功能主要是使用imgShowimgStore,所以平滑化和銳利化處理中都有添入隱藏imgOrigin、顯示imgStore的程式碼。


為此功能增加的變數宣告:

BYTE *bPictureImageStoreRow;  // 貯存修改前的色彩值

BYTE *bPictureImageRow0; // i -1
BYTE *bPictureImageRow1; //目標像素所在的第 i
BYTE *bPictureImageRow2; // i +1
int iSum;



平滑化處理函式:

void __fastcall TForm1::miSmoothingClick(TObject *Sender)
{
if ( imgOrigin->Visible )
{
        imgOrigin->Visible = false;
        imgStore->Visible = true;
}

//貯存修改前的圖片資料
for (i = 0; i < iImageHeight; i++)
{
        bPictureImageStoreRow = (Byte*)imgStore->Picture->Bitmap->ScanLine[i];
        bPictureImageShowRow = (Byte*)imgShow->Picture->Bitmap->ScanLine[i];
        for (j = 0; j < iImageWidth; j++)
        {
                bPictureImageStoreRow[3*j+0] = bPictureImageShowRow[3*j+0];
                bPictureImageStoreRow[3*j+1] = bPictureImageShowRow[3*j+1];
                bPictureImageStoreRow[3*j+2] = bPictureImageShowRow[3*j+2];
        }
}

//平滑化處理
for (i = 1; i < iImageHeight-1; i++)
{
        bPictureImageShowRow = (Byte*)imgShow->Picture->Bitmap->ScanLine[i];
        bPictureImageRow0 = (Byte*)imgStore->Picture->Bitmap->ScanLine[i-1];
        bPictureImageRow1 = (Byte*)imgStore->Picture->Bitmap->ScanLine[i];
        bPictureImageRow2 = (Byte*)imgStore->Picture->Bitmap->ScanLine[i+1];
        for (j =1; j < iImageWidth-1; j++)
        {
                for (k = 0; k < 3; k++)
                {
                iSum = bPictureImageRow0[3*(j-1)+k] + bPictureImageRow0[3*j+k] + bPictureImageRow0[3*(j+1)+k]
                         + bPictureImageRow1[3*(j-1)+k] + bPictureImageRow1[3*j+k] + bPictureImageRow1[3*(j+1)+k]
                         + bPictureImageRow2[3*(j-1)+k] + bPictureImageRow2[3*j+k] + bPictureImageRow2[3*(j+1)+k];

                bPictureImageShowRow[3*j+k] = iSum / 9;
                }
        }
}
imgShow->Refresh();
imgStore->Refresh();
}



銳利化處理函式:

void __fastcall TForm1::miSharpingClick(TObject *Sender)
{
if ( imgOrigin->Visible )
{
        imgOrigin->Visible = false;
        imgStore->Visible = true;
}

//貯存修改前的圖片資料
for (i = 0; i < iImageHeight; i++)
{
        bPictureImageStoreRow = (Byte*)imgStore->Picture->Bitmap->ScanLine[i];
        bPictureImageShowRow = (Byte*)imgShow->Picture->Bitmap->ScanLine[i];
        for (j = 0; j < iImageWidth; j++)
        {
                bPictureImageStoreRow[3*j+0] = bPictureImageShowRow[3*j+0];
                bPictureImageStoreRow[3*j+1] = bPictureImageShowRow[3*j+1];
                bPictureImageStoreRow[3*j+2] = bPictureImageShowRow[3*j+2];
        }
}

銳利化處理
for (i = 1; i < iImageHeight-1; i++)
{
        bPictureImageShowRow = (Byte*)imgShow->Picture->Bitmap->ScanLine[i];
        bPictureImageRow0 = (Byte*)imgStore->Picture->Bitmap->ScanLine[i-1];
        bPictureImageRow1 = (Byte*)imgStore->Picture->Bitmap->ScanLine[i];
        bPictureImageRow2 = (Byte*)imgStore->Picture->Bitmap->ScanLine[i+1];
        for (j =1; j < iImageWidth-1; j++)
        {
                for (k = 0; k < 3; k++)
                {
                iSum = 5 * bPictureImageRow1[3*j+k]
                         - bPictureImageRow0[3*j+k]
                         - bPictureImageRow1[3*(j-1)+k] - bPictureImageRow1[3*(j+1)+k]
                         - bPictureImageRow2[3*j+k];
                /*
                iSum = 9 * bPictureImageRow1[3*j+k]
                         - bPictureImageRow0[3*(j-1)+k] - bPictureImageRow0[3*j+k] - bPictureImageRow0[3*(j+1)+k]
                         - bPictureImageRow1[3*(j-1)+k] - bPictureImageRow1[3*(j+1)+k]
                         - bPictureImageRow2[3*(j-1)+k] - bPictureImageRow2[3*j+k] - bPictureImageRow2[3*(j+1)+k];
                */
                if (iSum > 255)
                        bPictureImageShowRow[3*j+k] = 255;
                else if (iSum < 0)
                        bPictureImageShowRow[3*j+k] = 0;
                else
                        bPictureImageShowRow[3*j+k] = iSum;
                }
        }
}
imgShow->Refresh();
imgStore->Refresh();
}


部分運行畫面:

功能建置在Image->Filtering選單下



平滑化運行畫面




銳利化運行畫面




沒有留言:

張貼留言