//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
//用选择法排序
void TForm1::Sort(int *Array)
{
int i,j;
int temp;
for(i=0;i<8;i++)
for(j=i+1;j<9;j++)
if(Array[i]>Array[j])
{
temp=Array[i];
Array[i]=Array[j];
Array[j]=temp;
}
}
//---------------------------------------------------------------------------
//取9个点象素值的中间值
int TForm1::MiddleValue(int s,int t,String str)
{
int *Array;
Array=new int[9];
int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(str=="Red")
Array[3*i+j]=rgb[s-1+i][t-1+j].r;
else if(str=="Green")
Array[3*i+j]=rgb[s-1+i][t-1+j].g;
else
Array[3*i+j]=rgb[s-1+i][t-1+j].b;
Sort(Array);
return Array[4];
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(OpenPictureDialog1->Execute())
{
Image1->Picture->LoadFromFile(OpenPictureDialog1->FileName);
width=Image1->Picture->Width;
height=Image1->Picture->Height;
//读入位图各象素点图像的RGB值
int i,j;
rgb=new RGBColor*[width];
TColor color;
for(i=0;i<width;i++)
{
rgb[i]=new RGBColor[height];
for(j=0;j<height;j++)
{
color=Image1->Canvas->Pixels[i][j];
rgb[i][j].r=GetRValue(color);
rgb[i][j].g=GetGValue(color);
rgb[i][j].b=GetBValue(color);
}
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Graphics::TBitmap* myBitmap;
myBitmap=new Graphics::TBitmap;
myBitmap->Width=width;
myBitmap->Height=height;
//柔化图像
int red,green,blue;
int i,j;
for(i=0;i<width;i++)
for(j=0;j<height;j++)
{
if(i==0||i==width-1||j==0||j==height-1)
{
red=rgb[i][j].r;
green=rgb[i][j].g;
blue=rgb[i][j].b;
}
else
{
red=(rgb[i-1][j-1].r+rgb[i-1][j].r+rgb[i-1][j+1].r
+rgb[i][j-1].r+rgb[i][j].r+rgb[i][j+1].r
+rgb[i+1][j-1].r+rgb[i+1][j].r+rgb[i+1][j+1].r)/9;
green=(rgb[i-1][j-1].g+rgb[i-1][j].g+rgb[i-1][j+1].g
+rgb[i][j-1].g+rgb[i][j].g+rgb[i][j+1].g
+rgb[i+1][j-1].g+rgb[i+1][j].g+rgb[i+1][j+1].g)/9;
blue=(rgb[i-1][j-1].b+rgb[i-1][j].b+rgb[i-1][j+1].b
+rgb[i][j-1].b+rgb[i][j].b+rgb[i][j+1].b
+rgb[i+1][j-1].b+rgb[i+1][j].b+rgb[i+1][j+1].b)/9;
}
myBitmap->Canvas->Pixels[i][j]=TColor(RGB(red,green,blue));
}
Image2->Picture->Bitmap->Assign(myBitmap);
delete myBitmap;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
Graphics::TBitmap* myBitmap;
myBitmap=new Graphics::TBitmap;
myBitmap->Width=width;
myBitmap->Height=height;
//中值滤波
int red,green,blue;
int i,j;
for(i=0;i<width;i++)
for(j=0;j<height;j++)
{
if(i==0||i==width-1||j==0||j==height-1)
{
red=rgb[i][j].r;
green=rgb[i][j].g;
blue=rgb[i][j].b;
}
else
{
red=MiddleValue(i,j,"Red");
green=MiddleValue(i,j,"Green");
blue=MiddleValue(i,j,"Blue");
}
myBitmap->Canvas->Pixels[i][j]=TColor(RGB(red,green,blue));
}
Image3->Picture->Bitmap->Assign(myBitmap);
delete myBitmap;
}
//---------------------------------------------------------------------------