2个图片,用SCANLINE 如何比较不同?

解决方案 »

  1.   

    http://hi.baidu.com/83925com/blog/item/7454921bdca5d8d3ad6e75c6.html
    google搜索出来的。网上貌似很多
      

  2.   

    图像比较一般是先先二值化,再差影法将两个图片相减,如果最后的像素点都是0,则认为一样
    如果想对文件比较,一般是先散列生成摘要,然后对摘要进行比较
    可以去看看《Delphi数字图像处理及高级应用》
      

  3.   

    首先图像要格式要转成一样,比如16bit,逐行或者隔行scanline 后,按位比较.
      

  4.   

    //方法1
    procedure TForm1.Button1Click(Sender: TObject);
    type
      PRGBTripleArray = ^TRGBTripleArray;
      TRGBTripleArray = array[0..32767] of TRGBTriple;
    var
      x,y:integer;
      p0,p1: PRGBTripleArray;
    begin
      Image1.Picture.LoadFromFile('C:\WINNT\wallpaper.bmp');
      Image2.Picture.LoadFromFile('C:\WINNT\wallpaper1.bmp');  if (Image1.Picture.Bitmap.Height<>Image2.Picture.Bitmap.Height)or
       (Image1.Picture.Bitmap.Width <>Image2.Picture.Bitmap.Width ) then
       begin
          caption:='不同';
          Exit;
       end;  Image1.Picture.Bitmap.PixelFormat :=pf24bit;
      Image2.Picture.Bitmap.PixelFormat :=pf24bit;
      for y:=0 to Image1.Picture.Bitmap.Height -1 do
      begin
        p0:=Image1.Picture.Bitmap.ScanLine[y];
        p1:=Image2.Picture.Bitmap.ScanLine[y];
        for x:=0 to Image1.Picture.Bitmap.Width -1 do
        if (p0[x].rgbtBlue =p1[x].rgbtBlue)and
        (p0[x].rgbtGreen  =p1[x].rgbtGreen )and
        (p0[x].rgbtRed  =p1[x].rgbtRed )  then
        begin
          caption:='相同';
        end
        else
        begin
          caption:='不同';
          Exit;
        end;  end;end;//方法2
    function IsHomology(bmp1, bmp2: TbitMap): boolean;
    type
      PRGBTripleArray = ^TRGBTripleArray;
      TRGBTripleArray = array[0..32767] of TRGBTriple;
    var
      x, y: integer;
      p0, p1: PRGBTripleArray;
      sBmp, dBmp: TBitMap;
    begin
      Result := False;  if (bmp1.Height <> bmp2.Height) or
        (bmp1.Width <> bmp2.Width) then
      begin
        Exit;
      end;  sBmp := TBitmap.Create;
      dBmp := TBitmap.Create;
      try
        sBmp.Assign(bmp1);
        dBmp.Assign(bmp2);
        sBmp.PixelFormat := pf24bit;
        dBmp.PixelFormat := pf24bit;
        for y := 0 to sBmp.Height - 1 do
        begin
          p0 := sBmp.ScanLine[y];
          p1 := dBmp.ScanLine[y];
          for x := 0 to sBmp.Width - 1 do
            if (p0[x].rgbtBlue = p1[x].rgbtBlue) and
              (p0[x].rgbtGreen = p1[x].rgbtGreen) and
              (p0[x].rgbtRed = p1[x].rgbtRed) then
            begin
              Result := True;
            end
            else
            begin
              Break;
            end;
        end;
      finally
        sBmp.Free;
        dBmp.Free;
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      if IsHomology(SpeedButton1.Glyph, SpeedButton2.Glyph) then
        showmessage('相同');
    end;
    //**********************************************
    //方法3 比较,并将不同的复制
    //**********************************************
    function IsHomology(bmp1, bmp2: TbitMap): boolean;
    type
      PRGBTripleArray = ^TRGBTripleArray;
      TRGBTripleArray = array[0..32767] of TRGBTriple;
    var
      x, y: integer;
      p0, p1: PRGBTripleArray;
      sBmp, dBmp: TBitMap;
    begin
      Result := True;  if (bmp1.Height <> bmp2.Height) or
        (bmp1.Width <> bmp2.Width) then
      begin
        Exit;
      end;  sBmp := TBitmap.Create;
      dBmp := TBitmap.Create;
      try
        sBmp.Assign(bmp1);
        dBmp.Assign(bmp2);
        sBmp.PixelFormat := pf24bit;
        dBmp.PixelFormat := pf24bit;
        for y := 0 to sBmp.Height - 1 do
        begin
          p0 := sBmp.ScanLine[y];
          p1 := dBmp.ScanLine[y];
          for x := 0 to sBmp.Width - 1 do
            if (p0[x].rgbtBlue = p1[x].rgbtBlue) and
              (p0[x].rgbtGreen = p1[x].rgbtGreen) and
              (p0[x].rgbtRed = p1[x].rgbtRed) then
            begin
    //          Result := True;
            end
            else
            begin
              Result := False;
              p1[x] := p0[x];
            end;
        end;
        Bmp2.Assign(dBmp);
      finally
        sBmp.Free;
        dBmp.Free;
      end;
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      IsHomology(Image2.Picture.Bitmap, Image1.Picture.Bitmap);
    end;
      

  5.   

    其实两个图像文件比较是否相同你应该描述一下用途的。比如简单的图像识别你应该用取关键点的方法来实现这是速度最快的方法,如果检测文件的说明部分、色库或略缩图都是否完全一致那么就要用计算Hash的办法来比较了,比较的方法很多应该考虑用途再写代码这样才能保证运算速度。
      

  6.   

    我试了一下IsHomology,但我没用好还是怎么的,两图片大小相同,但内容稍有不同,比较后提示是相同的图片。为什么?
      

  7.   

    for x := 0 to sBmp.Width - 1 do
            begin
            if (p0[x].rgbtBlue = p1[x].rgbtBlue) and (p0[x].rgbtGreen = p1[x].rgbtGreen) and (p0[x].rgbtRed = p1[x].rgbtRed) then //不管这三个比较的结果相等不相等,光标运行到这的时候跳到★
              begin
               Result := True;
              end else
              begin
               Break;  //1
              end;
            end;//★
          end;
    应该是不同时跳到1,相同时跳到2的吧?怎么跑出去了?
      

  8.   

    4楼的代码不管用啊,明明同一张图加载到两个IAMGE中说不一样,不一样的说一样
      

  9.   

    记录下扫描时的RGB进行比较!
      

  10.   

    呵呵,没必要那么麻烦,将两张图片加载后另存为尺寸大小一样的BMP文件,再获取两个BMP文件的哈希值比较行了