Skip to main content

How to do Low Pass Filter using the C++ Builder XE5 of the Embarcadero?

How to do Low Pass Filter using the C++ Builder XE5 of the Embarcadero?

How to do low pass filter using the C++ Builder XE5 of the Embarcadero?

Introduction

The low pass filter is able to remove noise and blur image. This is an average low pass filter. It is very simple and you have just summed in 3x3 matrix and the divide by 9. The result is the center pixel value.
This program is for beginners, you only write 44 lines of code and then you can make it.
The C++ Builder has a lot of components, we just use it and then prepare to arrange those components we need. We need a TMainMenu, a TOpenPictureDialog, a TImage, a TScrollbar and two TLabel. How to arrange those positions, you decide it. However, we have a sample for you.
We have a scroll bar to decide to do times the low pass filter, more times and more blur.

Equipment

Operation System: Microsoft Windows 7 (64 bit)
Development Utility: Embarcadero C++ Builder XE5

Usage

image.png

image.png


//---------------------------------------------------------------------------

#include 
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
// Create three points of TBitmap object
Graphics::TBitmap *TheBitmap, *TempBitmap, *OriginBitmap;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Exit1Click(TObject *Sender)
{
    // Exit the program
    Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OpenFile1Click(TObject *Sender)
{
    // If you got a file of the picture, you are able to load it.
    if ( OpenPictureDialog1->Execute() )
    {
        // Disable automatic resize.
        Image1->AutoSize=false;
        // Enable an automatic stretch.
        Image1->Stretch=true;
        // Loads a picture file.
        Image1->Picture->LoadFromFile(OpenPictureDialog1->FileName);
        // Set a point to the picture of loading.
        TheBitmap=Image1->Picture->Bitmap;
        // Create a new TBitmap object to keep original picture.
        OriginBitmap = new Graphics::TBitmap();
        // To keep original picture.
        OriginBitmap->Assign(TheBitmap);
        // Set up TImage object to keep original picture.
        Image1->Picture->Bitmap->Assign(TheBitmap);
        // If you open picture file successful and then set a flag.
        // The means that you have been opened a file.
        OpenFile = 1;
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ScrollBar1Change(TObject *Sender)
{
    // If you have not opened a file, the flag will be zero, and then returns.
    if (OpenFile == 0) {
        return;
    }
    int Deg_Lowpass = 0;
    Byte *ptr, *tptr, *uptr, *dptr;
    int up, down, left, right, sum;
    // To get a counter of low pass filter from the scroll bar.
    Deg_Lowpass = (int) ScrollBar1->Position;
    // Set up the TImage restore to the original picture.
    Image1->Picture->Bitmap->Assign(OriginBitmap);
    // To do the times of low pass filter
    for (int i = 0; i < Deg_Lowpass; i++) {
        // Create a new TBitmap object, This is a temporal bitmap to make an image
        // processing.
        TempBitmap = new Graphics::TBitmap();
        // To get a current bitmap.
        TempBitmap->Assign(TheBitmap);
        // To do scan line of the whole picture.
        for (int y=0; y < TheBitmap->Height; y++) {
            // Arranging up and down to keep the inner picture of height.
            up = y - 1;
            down = y + 1;
            if ( up < 0 ) {
                up = TheBitmap->Height - 1;
            }
            if ( down == TheBitmap->Height ) {
                down = 0;
            }
            // Set Y position of the image point.
            ptr = (Byte*) TheBitmap->ScanLine[y];
            // Set left and right position of the image point.
            tptr = (Byte*) TempBitmap->ScanLine[y];
            // Set up position of the image point.
            uptr = (Byte*) TempBitmap->ScanLine[up];
            // Set down position of the image point.
            dptr = (Byte*) TempBitmap->ScanLine[down];
            // To do scan horizontal of the line of the image.
            for (int x=0; x < TheBitmap->Width; x++) {
                // Arranging left and right to keep the inner picture of width.
                left = x - 1;
                right = x + 1;
                if (left < 0 ) {
                    left = TheBitmap->Width - 1;
                }
                if ( right == TheBitmap->Width ) {
                    right = 0;
                }
                // To calculate the pixel of sum of low pass filter.
                sum = (int) (uptr[left] +uptr[x] + uptr[right] +
                tptr[left] + tptr[x] + tptr[right] +
                dptr[left] + dptr[x] + dptr[right]);
                // Set the center of pixel value of low pass filter.
                ptr[x] = (Byte) (sum/9);
            } // End x
        } // End y
        // Release the temporal bitmap.
        delete TempBitmap;
    } // End I
    // Refresh and draw the TImage object
    Repaint();
    // Set result of low pass filter of the picture.
    Image1->Picture->Bitmap->Assign(TheBitmap);
}
//---------------------------------------------------------------------------

You can download source code and binary code form this hyperlink as below.
https://drive.google.com/file/d/0BzHb_OyLyVZlZGpPYnd4bVBuT3M/view?usp=sharing

Exception

If you encounter the error, don’t worry. You just step by step to solve it.

Reference

[1] Gary Bradski and Adrian Kaehler, “Learning OpenCV: Computer Vision with the OpenCV Library,” O’REILLY, September 2008, ISBN:978-0-596-51613-0

Acknowledge

Thank you (Embarcadero C++ Builder XE5, Lenna Sjööblom) very much for this great development utility and beautiful photo.

Popular posts from this blog

Python 日期與時間的處理

Visual Basic 6.0 (VB6) 程式語言案例學習 (10. 條碼列印程式)

寫作:波蘭文學習之旅:1-1. 波蘭文字母與發音(注音版)

Python 日期與時間的處理

Image

Visual Basic 6.0 (VB6) 程式語言案例學習 (10. 條碼列印程式)

Image

寫作:波蘭文學習之旅:1-1. 波蘭文字母與發音(注音版)

Image

數位影像處理:最佳化處理策略之快速消除扭曲演算法

Image

Visual Basic 6.0 (VB6) 程式語言案例學習 (04. 人事考勤管理系統)

Image

用10種程式語言做影像二值化(Image binarization)

Visual Basic 6.0 (VB6) 程式語言案例學習 (07. 收據列印程式)

Image

Visual Basic .Net (VB.Net) 程式語言案例學習 (03. 場地預約系統)

Image

Visual Basic 6.0 (VB6) 程式語言案例學習 (11. 生產線拍照程式)

Image

Visual Basic .Net (VB.Net) 程式語言案例學習 (06. 題庫測驗系統)

Image