我现在在做轮廓跟踪,是翻译VC上的,可有点问题,我把代码帖上,大家帮我看看啊!
procedure TraceDIB1(Sbmp:tbitmap);
var
Dbmp:tbitmap;
lpsrc,lpDst:pbytearray;
i,j,x,y,a,b,c,d:integer;
num_1,num_2,num_0,num0_1,num0_2,num0_0,pixel_1,pixel_2,pixel_0,maxpixel_0,maxpixel_1,maxpixel_2:byte;
bFindStartPoint,bFindPoint:bool;
Direction:matrx;
StartPoint,CurrentPoint:tpoint;
BeginDirect:integer;
begin
Dbmp:=tbitmap.Create ;
Dbmp.Height:=sbmp.Height ;
Dbmp.Width:=Sbmp.Width ;
setlength(Direction,8,2);
Direction[0,0]:=-1;  Direction[0,1]:=1;
Direction[1,0]:=0;   Direction[1,1]:=1;
Direction[2,0]:=1;   Direction[2,1]:=1;
Direction[3,0]:=1;   Direction[3,1]:=0;
Direction[4,0]:=1;   Direction[4,1]:=-1;
Direction[5,0]:=0;   Direction[5,1]:=-1;
Direction[6,0]:=-1;  Direction[6,1]:=-1;
Direction[7,0]:=-1;  Direction[7,1]:=0;
bFindStartPoint:=false;      //先找到最左上方的边界点
for j:=1 to sbmp.Height-1  do
  begin
    while(bFindStartPoint=false) do
      begin
       lpsrc:=sbmp.ScanLine[j];
       lpdst:=dbmp.ScanLine[j];
       for i:=1 to Sbmp.Width-1 do
         begin
           while(bFindStartPoint=false) do
             begin
               num_1:=0;          //初始化
               num_2:=0;
               num_0:=0;
               num0_1:=0;
               num0_2:=0;
               num0_0:=0;
               pixel_0:=lpsrc[3*i];   //取得当前指针处的像素值
               pixel_1:=lpsrc[3*i+1];
               pixel_2:=lpsrc[3*i+2];
               for y:=1 to 3 do       //每个象素点的上一行与下一行的3个象素点
                 begin
(指针可以这样设吗?)lpsrc:=sbmp.ScanLine[j+y-1];
                   for x:= 1 to 3 do
                     begin            //每个象素的左一列与右一列的3个象素点
                       num_0:=lpsrc[3*(x-1)];   //计算像素值
                       num_1:=lpsrc[3*(x-1)+1];
                       num_2:=lpsrc[3*(x-1)+2];
                       if (num_0>num0_0)and(num_1>num0_1)and(num_2>num0_2)then
                         num0_0:=num_0;
                         num0_1:=num_1;
                         num0_2:=num_2;
                     end;
                 end;
                 MaxPixel_0:=num0_0;    //相邻8个象素点的最大像素值
                 MaxPixel_1:=num0_1;
                 MaxPixel_2:=num0_2;
                 if(Maxpixel_0<>pixel_0)and(Maxpixel_1<>pixel_1)and(Maxpixel_2<>pixel_2)then  //如果当前点是边界点
                    begin
                      bFindStartPoint:=true;  //则找到了边界跟踪的起点
                      StartPoint.Y:=j;        //记录起始点在图像中的位置
                      StartPoint.X:=i;
                      lpdst[3*i+2]:=0;
                      lpdst[3*i+1]:=0;
                      lpdst[3*i]:=0;
                   end;
           end;
       end;
    end;
  end;
  //由于起始点是在左下方,故起始扫描沿左上方
  BeginDirect:=0;
  bFindStartPoint:=false;  //跟踪边界
  Currentpoint.Y :=StartPoint.Y;  //从初始点开始扫描
  CurrentPoint.X:=StartPoint.X;
  while(bFindStartPoint=false)do  //如果跟踪还没回到原来的位置
    begin
       bFindPoint:=false;   //没有找到新的边界点
       while(bFindPoint=false) do
         begin
           num0_0:=0;        //初始化
           num0_1:=0;
           num0_2:=0;
           num_0:=0;
           num_1:=0;
           num_2:=0;
           b:=CurrentPoint.Y+Direction[BeginDirect][1];
           a:=CurrentPoint.X+Direction[BeginDirect][0];
           lpsrc:=sbmp.ScanLine[b];
           pixel_0:=lpsrc[3*a];
           pixel_1:=lpsrc[3*a+1];
           pixel_2:=lpsrc[3*a+2];
           for y:=1 to 3 do
             begin
               lpsrc:=sbmp.ScanLine[j+y-1];(指针可以这样设吗?)
               for x:=1 to 3 do
                 begin
                   num_0:=lpsrc[3*(x-1)];
                   num_1:=lpsrc[3*(x-1)+1];
                   num_2:=lpsrc[3*(x-1)+2];
                   if(num_0>num0_0)and(num_1>num0_1)and(num_2>num0_2)then
                     begin
                       num0_0:=num_0;
                       num0_1:=num_1;
                       num0_2:=num_2;
                     end;
                 end;
             end;
             MaxPixel_0:=num0_0;
             MaxPixel_1:=num0_1;
             MaxPixel_2:=num0_2;
             if(MaxPixel_0<>pixel_0)and(MaxPixel_1<>pixel_1)and(MaxPixel_2<>pixel_2)then
               begin
                bFindPoint:=true;
                CurrentPoint.Y:=CurrentPoint.Y+Direction[BeginDirect][1];
                CurrentPoint.X :=CurrentPoint.X+Direction[BeginDirect][0];
                if(CurrentPoint.y=StartPoint.Y )and(CurrentPoint.x=StartPoint.X )then
                  begin
                    bFindStartPoint:=true;
                  end;
                d:=CurrentPoint.Y;
                c:=CurrentPoint.X;
                lpdst:=dbmp.ScanLine[d] ;
                lpdst[3*c+2]:=0;
                lpdst[3*c+1]:=0;
                lpdst[3*c]:=0;
                dec(BeginDirect);
                if (BeginDirect=-1) then BeginDirect:=7;
                dec(BeginDirect);
                if (BeginDirect=-1) then BeginDirect:=7;
               end
               else
                 begin
                   inc(BeginDirect);
                   if(BeginDirect=8)then BeginDirect:=0;
                 end;
         end;
  end;
  form1.Image1.Picture.Bitmap.Assign(Dbmp);
end;

解决方案 »

  1.   

    //轮廓提取var
       b0, b1: Tbitmap;
       i, j: Integer;
       p1, p2, p3, p4: pbyteArray;
    begin
       b0 := Tbitmap.Create;
       b1 := Tbitmap.Create;
       b0.Assign(Image1.Picture.Bitmap);
       b1.Assign(Image1.Picture.Bitmap);
       b0.PixelFormat := pf24bit;
       b1.PixelFormat := pf24bit;
       for i := 1 to b0.Height - 2 do
       begin
          p1 := b0.ScanLine[i - 1];
          p2 := b0.ScanLine[i];
          p3 := b0.ScanLine[i + 1];
          p4 := b1.ScanLine[i];
          for j := 1 to b0.Width - 2 do
          begin
             if (p2[3 * j + 2] = 0) and (p2[3 * j + 1] = 0) and (p2[3 * j] = 0) then
             begin            if ((p2[3 * (j - 1) + 2] = 0) and (p2[3 * (j - 1) + 1] = 0) and
                   (p2[3 * (j - 1)] = 0)) and
                ((p2[3 * (j + 1) + 2] = 0) and (p2[3 * (j + 1) + 1] = 0) and
                   (p2[3 * (j + 1)] = 0)) and
                ((p1[3 * (j + 1) + 2] = 0) and (p1[3 * (j + 1) + 1] = 0) and
                   (p1[3 * (j + 1)] = 0)) and
                ((p1[3 * (j) + 2] = 0) and (p1[3 * (j) + 1] = 0) and (p1[3 * (j)]
                   = 0)) and
                ((p1[3 * (j - 1) + 2] = 0) and (p1[3 * (j - 1) + 1] = 0) and
                   (p1[3 * (j - 1)] = 0)) and
                ((p3[3 * (j - 1) + 2] = 0) and (p3[3 * (j - 1) + 1] = 0) and
                   (p3[3 * (j - 1)] = 0)) and
                ((p3[3 * (j) + 2] = 0) and (p3[3 * (j) + 1] = 0) and (p3[3 * (j)]
                   = 0)) and
                ((p3[3 * (j + 1) + 2] = 0) and (p3[3 * (j + 1) + 1] = 0) and
                   (p3[3 * (j + 1)] = 0)) then
                begin
                   p4[3 * j + 2] := 255;
                   p4[3 * j + 1] := 255;
                   p4[3 * j] := 255;
                end;
             end;      end;
          Image1.Picture.Bitmap.Assign(b1);
       end;
       b1.Free;
       b0.Free;
    end;