📄 unit1.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__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||j==0)
{
red=0;
green=0;
blue=0;
}
else
{
red=abs(rgb[i][j].r-rgb[i-1][j-1].r);
green=abs(rgb[i][j].g-rgb[i-1][j-1].g);
blue=abs(rgb[i][j].b-rgb[i-1][j-1].b);
}
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 red1,green1,blue1,red2,green2,blue2;
int i,j;
for(i=0;i<width;i++)
for(j=0;j<height;j++)
{
if(i==0||j==0||i==width-1||j==height-1)
{
red=0;
green=0;
blue=0;
}
else
{
red1=abs(rgb[i+1][j-1].r+rgb[i+1][j].r+rgb[i+1][j+1].r
-rgb[i-1][j-1].r-rgb[i-1][j].r-rgb[i-1][j+1].r);
green1=abs(rgb[i+1][j-1].g+rgb[i+1][j].g+rgb[i+1][j+1].g
-rgb[i-1][j-1].g-rgb[i-1][j].g-rgb[i-1][j+1].g);
blue1=abs(rgb[i+1][j-1].b+rgb[i+1][j].b+rgb[i+1][j+1].b
-rgb[i-1][j-1].b-rgb[i-1][j].b-rgb[i-1][j+1].b);
red2=abs(rgb[i+1][j+1].r+rgb[i][j+1].r+rgb[i-1][j+1].r
-rgb[i+1][j-1].r-rgb[i][j-1].r-rgb[i-1][j-1].r);
green2=abs(rgb[i+1][j+1].g+rgb[i][j+1].g+rgb[i-1][j+1].g
-rgb[i+1][j-1].g-rgb[i][j-1].g-rgb[i-1][j-1].g);
blue2=abs(rgb[i+1][j+1].b+rgb[i][j+1].b+rgb[i-1][j+1].b
-rgb[i+1][j-1].b-rgb[i][j-1].b-rgb[i-1][j-1].b);
red=(red1>red2)?red1:red2;
green=(green1>green2)?green1:green2;
blue=(blue1>blue2)?blue1:blue2;
red=(red>255)?255:red;
green=(green>255)?255:green;
blue=(blue>255)?255:blue;
}
myBitmap->Canvas->Pixels[i][j]=TColor(RGB(red,green,blue));
}
Image3->Picture->Bitmap->Assign(myBitmap);
delete myBitmap;
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -