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;                               这个图片是黑色背景中间一个红色球体 要找出球体范围 但是总是不对 为什么 

解决方案 »

  1.   

    因为你只判断一个,当然错啦,你要and起来比如 12,100和 33,80 你的结果就成了 12,80 所以是错的. 
      

  2.   

    var L,R,T,B:ineger;从左到右扫描,如果有红色,把当前X值赋值给L,退出本次循环再从右到左扫描
    再从上到下扫描
    再从下到上扫描
    这样就找到红色部分的范围了
      

  3.   

    我试了一下,可以啊
    测试图片见下图,输出结果: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;
      

  4.   

    楼主先确认一下你所说的黑色背景是不是纯黑的(R=0, G=0, B=0)
      

  5.   

    怎么确认    我在photoshop上看的背景都是 0,0,0
      

  6.   

    背景为全黑没问题的话, 改一下这里: tbm.Canvas.Pixels[h, w] 
    括号里是先写横坐标, 再写纵坐标, 所以应该改成: tbm.Canvas.Pixels[w, h]