谁来帮助MM?(100分)

  • 主题发起人 主题发起人 dxtian
  • 开始时间 开始时间
D

dxtian

Unregistered / Unconfirmed
GUEST, unregistred user!
如何改变一副位图的亮度与饱和度?急!急!!急!!!
 
象这样的问题你可以到vcl.vclxx.org
 
太专业了!!!
用程序的话可能要好多代码阿
 
给你段代码参考一下吧:
procedure TForm1.Button1Click(Sender: TObject);
var
x,y,i: integer;
BitMap : TBitMap;
ptr : PByteArray;
begin
BitMap := TBitMap.Create;
try
BitMap.LoadFromFile('lanmannt.bmp');
BitMap.PixelFormat := pf24bit;
for i := 0 to 255 do begin
for y := 0 to BitMap.Height - 1 do begin
ptr := BitMap.ScanLine[y];
for x := 0 to ((BitMap.Width * 3) - 1) do
if ptr[x] > 0 then ptr[x] := (ptr[x] - 1);
end;
Image1.Canvas.Draw(0,0,BitMap);
Application.ProcessMessages;
end;
finally
BitMap.free;
end;
end;
 
转贴一篇文章,希望能对你有帮助!!!

改变图象的对比度、亮度、饱和度

// Bitmap.ScanLine[X] 可以获取图像象素的内存地址,24Bits的Bitmap的每一象
// 素是以三原色RGB的次序存放的,改变RGB的值就可调节Bitmap的色彩.
// R, G, B: -255~255
procedure RGB(var Bmp: TBitmap; R, G, B: Integer);
var
X, Y: Integer;
I: Byte;
ColorTable: array[0..255] of TRGBColor;
pRGB: PRGBColor;
begin
for I := 0 to 255 do
begin
ColorTable.R := Byte(I + R);
ColorTable.G := Byte(I + G);
ColorTable.B := Byte(I + B);
end;

for Y := 0 to Bmp.Height - 1 do
begin
pRGB := Bmp.ScanLine[Y];
for X := 0 to Bmp.Width - 1 do
begin
pRGB.R := ColorTable[pRGB.R].R;
pRGB.G := ColorTable[pRGB.G].G;
pRGB.B := ColorTable[pRGB.B].B;
end;
Inc(pRGB);
end;
end;

// 改变图像的亮度,也只需调用RGB(Bmp, X, X, X)改变三原色.
// 调节Bitmap的对比度
// 应用公式: 新颜色值 = (旧颜色值 - 128) * 系数 + 128
procedure Contrast(var Bmp: TBitmap; Amount: Integer);
// Amount: -255~255
var
X, Y: Integer;
I: Byte;
ColorTable: array[0..255] of TRGBColor;
pRGB: PRGBColor;
begin
for I := 0 to 126 do
begin
Y := (Abs(128 - I) * Amount) div 256;
ColorTable.r := GetRValue(Byte(I - Y));
ColorTable.g := GetGValue(Byte(I - Y));
ColorTable.b := GetBValue(Byte(I - Y));
end;
for I := 127 to 255 do
begin
Y := (Abs(128 - I) * Amount) div 256;
ColorTable.r := GetRValue(Byte(I + Y));
ColorTable.g := GetGValue(Byte(I + Y));
ColorTable.b := GetBValue(Byte(I + Y));
end;
for Y := 0 to Bmp.Height - 1 do
begin
pRGB := Bmp.ScanLine[Y];
for X := 0 to Bmp.Width - 1 do
begin
pRGB.R := ColorTable[pRGB.R].R;
pRGB.G := ColorTable[pRGB.G].G;
pRGB.B := ColorTable[pRGB.B].B;
Inc(pRGB);
end;
end;
end;

// 改变饱和度
procedure Saturation(var Bmp: TBitmap; Amount: Integer); // Amount: 0~510
var
Grays: array[0..767] of Integer;
Alpha: array[0..255] of Word;
Gray, X, Y: Integer;
pRGB: PRGBColor;
I: Byte;
begin
for I := 0 to 255 do Alpha := (I * Amount) shr 8;
x := 0;
for I := 0 to 255 do
begin
Gray := I - Alpha;
Grays[X] := Gray; Inc(X);
Grays[X] := Gray; Inc(X);
Grays[X] := Gray; Inc(X);
end;
for Y := 0 to Bmp.Height - 1 do
begin
pRGB := Bmp.ScanLine[Y];
for X := 0 to Bmp.Width - 1 do
begin
Gray := Grays[pRGB.R + pRGB.G + pRGB.B];
pRGB.R := Byte(Gray + Alpha[pRGB.R]);
pRGB.G := Byte(Gray + Alpha[pRGB.G]);
pRGB.B := Byte(Gray + Alpha[pRGB.B]);
Inc(pRGB);
end;
end;
end;

//对于Jpg的处理,与Bitmap类似,因为TJpegImage内部有TBitmap.

//Bmp := TBitmap.Create;
//Bmp.Assign(Jpg);

procedure TForm1.FormCreate(Sender: TObject);
begin
bmp := TBitmap.Create;
bmp.Assign(image1.Picture.Graphic);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
RGB(Bmp, StrToInt(edit1.text),StrToInt(edit2.text),StrToInt(edit3.text));
image1.Picture.Graphic.Assign(bmp);
image1.Refresh;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Contrast(Bmp, strtoint(edit1.text));
image1.Picture.Graphic.Assign(bmp);
image1.Refresh;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
Saturation(Bmp, strtoint(edit1.text));
image1.Picture.Graphic.Assign(bmp);
image1.Refresh;
end;
 
调整亮度:需要同时增加或减少像素点的R,G,B值,增加或减少的R,G,B值应该相等!比如均增加
或减少10;
调整饱和度:首先设定一个阀值,通常是128;然后判断像素点的R,G,B值,凡是大于128的,
增加一个值,小于128的,减少一个值!
 
饱和度就是对比度
卷起千堆雪tyn 说的有道理
 
to all:以上调整对比度的代码似乎不对.
我的图象基础太差,还请大家给我一些代码!
 
市面上有这样的书
 
看来还是美美的问题比较好回答
 
试试
procedure RGB(var Bmp: TBitmap; R, G, B: Integer);
var
X, Y: Integer;
I: Byte;
ColorTable: array[0..255] of TRGBColor;
pRGB: PRGBColor;
begin
for I := 0 to 255 do
begin
ColorTable.R := Byte(I + R);
ColorTable.G := Byte(I + G);
ColorTable.B := Byte(I + B);
end;

for Y := 0 to Bmp.Height - 1 do
begin
pRGB := Bmp.ScanLine[Y];
for X := 0 to Bmp.Width - 1 do
begin
pRGB.R := ColorTable[pRGB.R].R;
pRGB.G := ColorTable[pRGB.G].G;
pRGB.B := ColorTable[pRGB.B].B;
end;
Inc(pRGB);
end;
end;

// 改变图像的亮度,也只需调用RGB(Bmp, X, X, X)改变三原色.
// 调节Bitmap的对比度
// 应用公式: 新颜色值 = (旧颜色值 - 128) * 系数 + 128
procedure Contrast(var Bmp: TBitmap; Amount: Integer);
// Amount: -255~255
var
X, Y: Integer;
I: Byte;
ColorTable: array[0..255] of TRGBColor;
pRGB: PRGBColor;
begin
for I := 0 to 126 do
begin
Y := (Abs(128 - I) * Amount) div 256;
ColorTable.r := GetRValue(Byte(I - Y));
ColorTable.g := GetGValue(Byte(I - Y));
ColorTable.b := GetBValue(Byte(I - Y));
end;
for I := 127 to 255 do
begin
Y := (Abs(128 - I) * Amount) div 256;
ColorTable.r := GetRValue(Byte(I + Y));
ColorTable.g := GetGValue(Byte(I + Y));
ColorTable.b := GetBValue(Byte(I + Y));
end;
for Y := 0 to Bmp.Height - 1 do
begin
pRGB := Bmp.ScanLine[Y];
for X := 0 to Bmp.Width - 1 do
begin
pRGB.R := ColorTable[pRGB.R].R;
pRGB.G := ColorTable[pRGB.G].G;
pRGB.B := ColorTable[pRGB.B].B;
Inc(pRGB);
end;
end;
end;

// 改变饱和度
procedure Saturation(var Bmp: TBitmap; Amount: Integer); // Amount: 0~510
var
Grays: array[0..767] of Integer;
Alpha: array[0..255] of Word;
Gray, X, Y: Integer;
pRGB: PRGBColor;
I: Byte;
begin
for I := 0 to 255 do Alpha := (I * Amount) shr 8;
x := 0;
for I := 0 to 255 do
begin
Gray := I - Alpha;
Grays[X] := Gray; Inc(X);
Grays[X] := Gray; Inc(X);
Grays[X] := Gray; Inc(X);
end;
for Y := 0 to Bmp.Height - 1 do
begin
pRGB := Bmp.ScanLine[Y];
for X := 0 to Bmp.Width - 1 do
begin
Gray := Grays[pRGB.R + pRGB.G + pRGB.B];
pRGB.R := Byte(Gray + Alpha[pRGB.R]);
pRGB.G := Byte(Gray + Alpha[pRGB.G]);
pRGB.B := Byte(Gray + Alpha[pRGB.B]);
Inc(pRGB);
end;
end;
end;

//对于Jpg的处理,与Bitmap类似,因为TJpegImage内部有TBitmap.

//Bmp := TBitmap.Create;
//Bmp.Assign(Jpg);

procedure TForm1.FormCreate(Sender: TObject);
begin
bmp := TBitmap.Create;
bmp.Assign(image1.Picture.Graphic);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
RGB(Bmp, StrToInt(edit1.text),StrToInt(edit2.text),StrToInt(edit3.text));
image1.Picture.Graphic.Assign(bmp);
image1.Refresh;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Contrast(Bmp, strtoint(edit1.text));
image1.Picture.Graphic.Assign(bmp);
image1.Refresh;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
Saturation(Bmp, strtoint(edit1.text));
image1.Picture.Graphic.Assign(bmp);
image1.Refresh;
end;

 

Similar threads

回复
0
查看
1K
不得闲
回复
0
查看
995
不得闲
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
后退
顶部