荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: Rage (Rage My Dream), 信区: Program
标  题: 用C++ Builder对图像进行特殊效果处理
发信站: 荔园晨风BBS站 (Wed May 23 14:23:02 2001), 转信

在Windows编程中图像处理相对比较复杂,好在C++ Builder提供了一些图形类,它
们通过对Windows中的图形对象进行包装,从而大大简化了图像操作的难度。下面
就通过对图像进行柔化、锐化、浮雕效果等几个特殊效果处理来具体说明一下。


一、准备工作 位图图形实际上是像素的二维数组,它记录了每个像素的颜色信息
,而TCanvas类提供了Pixels属性,用它可以存取指定像素的颜色值,通过这个属
性将位图图形的部分或全部像素的颜色值进行相应的变换处理,就可以实现图像的
特殊效果处理。在Windows中颜色是根据红、绿、蓝三种颜色的饱和度来定义的,
在这里我们要将像素颜色值的红、绿、蓝分量从像素值中分离出来,分别加以保存
,所以需要定义一个结构来存放颜色分量:

struct rgb_str{

unsigned char r_color;

unsigned char g_color;

unsigned char b_color;};

rgb_str rgb[2000][2000];

建立全局变量:Graphics::TBitmap *bitmap;

//用来存放变换后的位图
int i,j,width,height;

在窗体上放置一个TImage组件和OpenPictureDialog组件,将TImage的AutoSize属
性设为true,将OpenPictureDialog的Filter设为*.bmp。当用户选择Open命令后,
打开相应的对话框,让用户选择要处理的图像文件,然后程序将图像的所有像素的
颜色分量保存到rgb数组中:

void __fastcall TForm1::mnuFileOpenClick

(TObject *Sender)
{ TColor color;

if(OpenPictureDialog1- >Execute()){

Image1- >Picture->LoadFromFile

(OpenPictureDialog1- >FileName);

width=Image1- >Picture- >Width; height=Image1->Picture->Height;

for(i=0;i< width-1;i++)

for(j=0;j< height-1;j++)

{ color=Image1- >Canvas->Pixels[i][j];
rgb[i][j].r_color=GetRValue(color);

rgb[i][j].g_color=GetGValue(color);

rgb[i][j].b_color=GetBValue(color);

}

bitmap=new Graphics::TBitmap;

bitmap- >Width=width;

bitmap- >Height=height;

}


}

二、图像的柔化处理

柔化就是对图像进行平滑处理,减少相邻像素间的颜色差别,一般选用3*3像素块
,将中间的像素值改成这9个像素的平均像素值,从而达到柔化效果。其代码如下


void __fastcall TForm1::btnSmoothClick

(TObject *Sender)

int red,green,blue;

for(i=1;i< width-2;i++)

for(j=1;j< height-2;j++)

{ red=rgb[i-1][j-1].r_color+rgb[i][j-1].r

_color+rgb[i+1][j-1].r_color+

rgb[i-1][j].r_color+rgb[i][j].r

_color+rgb[i+1][j].r_color+

rgb[i-1][j+1].r_color+rgb[i][j+1].r

_color+rgb[i+1][j+1].r_color;

green=rgb[i-1][j-1].g_color+rgb[i][j-1].g

_color+rgb[i+1][j-1].g_color+

rgb[i-1][j].g_color+rgb[i][j].g

_color+rgb[i+1][j].g_color+

rgb[i-1][j+1].g_color+rgb[i][j+1].g

_color+rgb[i+1][j+1].g_color;

blue=rgb[i-1][j-1].b_color+rgb[i][j-1].b

_color+rgb[i+1][j-1].b_color+

rgb[i-1][j].b_color+rgb[i][j].b

_color+rgb[i+1][j].b_color +

rgb[i-1][j+1].b_color+rgb[i][j+1].b

_color+rgb[i+1][j+1].b_color;

bitmap- >Canvas- >Pixels[i][j]

=RGB(red/9,green/9,blue/9);

}

Image1- >Picture- >Bitmap- >Assign(bitmap);

}
三、图像的锐化处理

图像的锐化处理正好与柔化处理相反,它的目的是突出图像的变化部分,这里采用
的算法是将要处理的像素与它左对角线的像素之间的差值乘上一个锐化度数,然后
再加上原先的像素值:new_value=original_value+degree*difference,你可以通
过改变degree的值来调节锐化效果。这里需要注意的是得到的像素新值可能会超出
颜色值的有效范围(0-255),所以程序要检验结果的有效性,为此需定义两个函数


int min(int value1,int value2)

{ if(value1 >value2)return value2;

else return value1;

}int max(int value1,int value2)

{ if(value1 >value2)return value1;

else return value2;}

锐化处理的代码如下:

void __fastcall TForm1::btnSharpeClick
(TObject *Sender)

{

float degree=0.3;

int red,green,blue;

for(i=1;i< width-1;i++)

for(j=1;j< height-1;j++)

{ red=rgb[i][j].r_color+degree*(rgb[i][j].r

_color-rgb[i-1][j-1].r_color);

green=rgb[i][j].g_color+degree*(rgb[i][j].g

_color-rgb[i-1][j-1].g_color);

blue=rgb[i][j].b_color+degree*(rgb[i][j].b

_color-rgb[i-1][j-1].b_color);

red=min(255,max(0,red));
green=min(255,max(0,green));

blue=min(255,max(0,blue));

bitmap- >Canvas->Pixels[i][j]=RGB (red,green,blue);

} Image1- >Picture- >Bitmap- >Assign(bitmap);



四、图像的浮雕效果实现
浮雕效果就是只将图像的变化部分突出出来,而相同颜色部分则被淡化,使图像出
现纵深感,从而达到浮雕效果,这里采用的算法是将要处理的像素取值为与处于对
角线上的另一个像素间的差值,这样只有颜色变化区才会出现色彩,而颜色平淡区
因差值几乎为零则变成黑色,你可以通过加上一个常量来增加一些亮度:
new_value=difference+const_value,具体代码如下:

void __fastcall TForm1::btnEmbossClick

(TObject *Sender)

{ int red,green,blue;

const int const_value=128;

for(i=0;i< width-2;i++)

for(j=0;j< height-2;j++)

{ red=abs(rgb[i][j].r_color-rgb[i+1][j+1].r

_color+const_value);
}
green=abs(rgb[i][j].g_color-rgb[i+1][j+1].g

_color+const_value);

blue=abs(rgb[i][j].b_color-rgb[i+1][j+1].b_

color+const_value);

bitmap- >Canvas- >Pixels[i][j]=RGB

(red,green,blue);

}

Image1- >Picture- >Bitmap- >Assign(bitmap);

}

上面介绍了图像处理中的几个常见操作,所采用的算法相对比较简单,感兴趣的朋
友可以举一返三,通过改进上述算法,达到更好的特殊效果。以上代码在C++
Builder3、Pwin98下编译、运行通过。

--



                                                    RAGE



※ 来源:·荔园晨风BBS站 bbs.szu.edu.cn·[FROM: 192.168.28.215]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店