请教灰阶图像的直方图算法(100分)

  • 主题发起人 主题发起人 花牛007
  • 开始时间 开始时间

花牛007

Unregistered / Unconfirmed
GUEST, unregistred user!
本人是个新手,只有这么多Money了。请指教!
 
请解释题目,是把 RGB 转 B&W?是统计灰度?
 
已是灰阶图像,统计灰阶直方图
 
var
Accs : array [0..255] of integer;
procedure TForm1.BitBtn1Click(Sender : tobject);
var
i,j : integer;
begin
fillchar(Accs, sizeof(Accs),0);
with image1 do
for i := 0 to Height - 1 do
for j := 0 to width - 1 do
begin
inc(Accs[getrvalue(picture.bitmap.canvas.pixels[j,i])]);
end;
for i := 0 to 255 do
begin
image2.canvas.Rectangle(i, 255 - accs, i+1, 255);
end;
end;

 
其实就是算出256阶灰度各个灰度级上的象素点数:代码如下
procedure TMainForm.GrayDiagramClick(Sender: TObject);
var
p: PByteArray;
i, x, y, j: Integer;
Bmp, bmp2: TBitmap;
Gray: byte;
color: Tcolor;
grayform: tgrayform;
maxvalue: integer;
Grayclass: array[0..255] of integer;
begin
Bmp := TBitmap.Create;
Bmp.Assign(childForm.Image1.Picture.Bitmap);
Bmp.PixelFormat := pf24Bit;
for y := 0 to Bmp.Height - 1 do
begin
p := Bmp.scanline[y];
for x := 0 to Bmp.Width - 1 do
begin
Gray := round(p[x * 3 + 2] * 0.3 + p[x * 3 + 1] * 0.59 + p[x
* 3]
* 0.11);
for i := 0 to 255 do
begin

if Gray = i then
begin
Grayclass := Grayclass + 1;
end;
end;

end

end;

bmp.Free;
grayform := Tgrayform.Create(self);
GrayForm.Show;
for i := 0 to 255 do
begin
begin
Grayform.Image1.Canvas.MoveTo(i, 273);
Grayform.Image1.Canvas.LineTo(i, 273 - round(50 *
(log10(grayclass
+
1))));
end;
end;
end;
 
最好的方法如下:
function Histogram(image:TImage;x1,y1,x2,y2:integer):string;
var
his: array [0..255] of real;
ihis: array [0..255,1..3] of integer;
sum: longword;
XStart,XEnd,YStart,YEnd,x,y: integer;
sumreal:real;
bmp:TBitmap;
p: PByteArray;
begin
bmp:=TBitmap.Create;
bmp.Assign(image.Picture.Bitmap );
Bmp.PixelFormat := pf24Bit;
XStart:=min(x1,x2);
XEnd:=max(x1,x2);
YStart:=min(y1,y2);
YEnd:=max(y1,y2);
if XStart=XEnd then
begin
XStart:=0;
XEnd:=bmp.Width-1;
end;
if YStart=YEnd then
begin
YStart:=0;
YEnd:=bmp.Height-1;
end;
if XStart<0 then Xstart:=0;
if XEnd>bmp.Width then XEnd:=bmp.Width-1;
if YStart<0 then YStart:=0;
if YEnd>bmp.Height then YEnd:=bmp.Height-1;

for x:=0 to 255 do
for y:=1 to 3 do
ihis[x,y]:=0;

sum:=0;
for y:=YStart to YEnd do
begin
p:=bmp.ScanLine[y];
for x:=XStart to XEnd do
begin
inc(ihis[p[x*3],1]);
inc(ihis[p[x*3+1],2]);
inc(ihis[p[x*3+2],3]);
inc(sum);
end;
end;

for x:=0 to 255 do
his[x]:=(ihis[x,1]+ihis[x,2]+ihis[x,3])/3.0;

sumreal:=0.0;
for x:=0 to 255 do
sumreal:=sumreal+(x*his[x])/sum;
bmp.Free;
result:=floattostr(sumreal);
end;
 
多人接受答案了。
 
谢谢各位朋友的相助,特别感谢wjames,huazai,轻松虎等大力支持。
 
后退
顶部