关于图像处理:如何使图像锐化与柔化?

cbc_21 2001-07-27 04:56:16
加精
求救:图像的锐化与柔化及调整亮度与对比度处理代码?哪儿有实例下载呀?
...全文
503 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
gqxs 2001-08-26
  • 打赏
  • 举报
回复
:)
ddeng 2001-07-27
  • 打赏
  • 举报
回复
哼!还是错了,气死我了!!
void __stdcall AdjustLightness(Graphics::TBitmap *Bmp,int Amount)
{
int x,y;
Byte Table[255];
TRGBColor24 *SrcP;

if( Amount>0) {
for(x=0;x<=255;x++) {
Table[x]=IntToByte(x+((Amount*(x^255))>>8));
}
}
else {
for(x=0;x<=255;x++) {
Table[x]=IntToByte(x-((abs(Amount)*x)>>8));
}
}
for(y=0;y<=Bmp->Height-1;y++) {
SrcP=static_cast<TRGBColor24 *>(Bmp->ScanLine[y]);
for(x=0;x<=Bmp->Width-1;x++) {
SrcP[x].R=Table[SrcP[x].R];
SrcP[x].G=Table[SrcP[x].G];
SrcP[x].B=Table[SrcP[x].B];
}
}
}
这下错不了了吧!! ^Q^
ddeng 2001-07-27
  • 打赏
  • 举报
回复
使用bitmap- >Canvas- >Pixels[i][j]效率会很低,建议使用ScanLine;
另外,我在楼上忘记说了,TRGBColor24是自己定义的:
struct TRGBColor24{Byte B,G,R;};
所处理的bmp的PixelFormat为pf24bit,使用前需要确认,必要时要转换。
不好意思,大括号位置弄错了:(,应该是下面这样的,对不起!
void __stdcall AdjustLightness(Graphics::TBitmap *Bmp,int Amount)
{
int x,y;
Byte Table[255];
TRGBColor24 *SrcP;

if( Amount>0) {
for(x=0;x<=255;x++) {
Table[x]=IntToByte(x+((Amount*(x^255))>>8));
}
}
else {
for(x=0;x<=255;x++) {
Table[x]=IntToByte(x-((abs(Amount)*x)>>8));
}
for(y=0;y<=Bmp->Height-1;y++) {
SrcP=static_cast<TRGBColor24 *>(Bmp->ScanLine[y]);
for(x=0;x<=Bmp->Width-1;x++) {
SrcP[x].R=Table[SrcP[x].R];
SrcP[x].G=Table[SrcP[x].G];
SrcP[x].B=Table[SrcP[x].B];
}
}
}
}
ddeng 2001-07-27
  • 打赏
  • 举报
回复
柔化可以使用区域平均法,调整亮度和对比度也就是改变所有像素的RGB值,调整亮度请参考:
void __stdcall AdjustLightness(Graphics::TBitmap *Bmp,int Amount)
{
int x,y;
Byte Table[255];
TRGBColor24 *SrcP;

if( Amount>0) {
for(x=0;x<=255;x++) {
Table[x]=IntToByte(x+((Amount*(x^255))>>8));
}
}
else {
for(x=0;x<=255;x++) {
Table[x]=IntToByte(x-((abs(Amount)*x)>>8));
for(y=0;y<=Bmp->Height-1;y++) {
SrcP=static_cast<TRGBColor24 *>(Bmp->ScanLine[y]);
for(x=0;x<=Bmp->Width-1;x++) {
SrcP[x].R=Table[SrcP[x].R];
SrcP[x].G=Table[SrcP[x].G];
SrcP[x].B=Table[SrcP[x].B];
}
}
}
}
}
zxq80 2001-07-27
  • 打赏
  • 举报
回复
源代码:

图像的柔化处理
---- 柔化就是对图像进行平滑处理,减少相邻像素间的颜色差别,一般选用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);



13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧