shang:=tbm.Height;
you:=0;
xia:=0;
zuo:=tbm.Width;for h:=5 to tbm.Height-5 do
begin
//row:=tbm.ScanLine[h];
for w:=5 to tbm.Width-5 do
begin
if tbm.Canvas.Pixels[h,w]<>clblack then
begin
if w<zuo then
zuo:=w;
if w>you then
you:= w;
if h>xia then
xia:=h;
if h<shang then
shang:=h;
//找出非黑的最大最小像素
end;
end;
end; 这个图片是黑色背景中间一个红色球体 要找出球体范围 但是总是不对 为什么
you:=0;
xia:=0;
zuo:=tbm.Width;for h:=5 to tbm.Height-5 do
begin
//row:=tbm.ScanLine[h];
for w:=5 to tbm.Width-5 do
begin
if tbm.Canvas.Pixels[h,w]<>clblack then
begin
if w<zuo then
zuo:=w;
if w>you then
you:= w;
if h>xia then
xia:=h;
if h<shang then
shang:=h;
//找出非黑的最大最小像素
end;
end;
end; 这个图片是黑色背景中间一个红色球体 要找出球体范围 但是总是不对 为什么
再从上到下扫描
再从下到上扫描
这样就找到红色部分的范围了
测试图片见下图,输出结果:3,12,3,12
procedure TF_Main.btn1Click(Sender: TObject);
var
bmp: TBitmap;
i, j: Integer;
zuo, you, shang, xia: Integer;
begin
img1.Picture.LoadFromFile( 'g:\temp.bmp' );
bmp := img1.Picture.Bitmap; zuo := 16;
you := -1;
shang := 16;
xia := -1;
for i := 0 to bmp.Height-1 do
begin
for j := 0 to bmp.Width-1 do
begin if bmp.Canvas.Pixels[i,j] = clblack then
begin
if j <zuo then
zuo := j;
if j > you then
you := j;
if i > xia then
xia := i;
if i <shang then
shang := i;
end;
end;
end; ShowMessage( IntToStr( shang ) + ',' + IntToStr( xia ) + ',' +
IntToStr( zuo ) + ',' + IntToStr( you ) );
end;
括号里是先写横坐标, 再写纵坐标, 所以应该改成: tbm.Canvas.Pixels[w, h]