zoukankan      html  css  js  c++  java
  • 简单的图片对比函数

    function Compare(bit1: TBitmap; bit2: TBitmap; nC: Integer; Img: TImage): Integer;
    var
      x,y , nRe: Integer;
      bx, by, bbs: Byte;
      bColor: TColor;
      temBit: TBitmap;
    begin
      if Img <> nil then
      begin
        temBit := TBitmap.Create;
        temBit.Width := bit1.Width;
        temBit.Height := bit1.Height;
      end;
    
      nRe := 0;
      for x := 0 to bit1.Width -1 do
      begin
        for y:= 0 to bit1.Height -1 do
        begin
          bx := GetBValue(bit1.Canvas.pixels[x,y]);
          by := GetBValue(bit2.Canvas.pixels[x,y]);
          bbs := abs(bx- by);
          if bbs < nC then
          begin
            bColor := clBlack;
          end else
          begin
            bColor := clWhite;
            Inc(nRe);
          end;
          if Img <> nil then
            temBit.Canvas.pixels[x,y]:= bColor;
        end;
      end;
      if Img <> nil then
        Img.Picture.Bitmap.Assign(temBit);
      temBit.Free;
      Result := nRe;
    end;
    


     

    //一种更快的方法
    function Compare2(bit1: TBitmap; bit2: TBitmap; nC: Integer; Img: TImage): Integer;
    var
      x,y , nRe: Integer;
      bbs: Byte;
      bColor: TColor;
      temBit: TBitmap;
      pix1, pix2: PByteArray;
    begin
      if Img <> nil then
      begin
        temBit := TBitmap.Create;
        temBit.Width := bit1.Width;
        temBit.Height := bit1.Height; 
      end;
    
      nRe := 0;
      for y:= 0 to bit1.Height -1 do
      begin
        pix1 := bit1.Scanline[y];
        pix2 := bit2.Scanline[y];
        for x:= 0 to bit1.Width-1 do
        begin
          bbs := abs(pix1[x*3]- pix2[x*3]);
          if bbs < nC then
          begin
            bColor := clBlack;
          end else
          begin
            bColor := clWhite;
            Inc(nRe);
          end;
          if Img <> nil then
            temBit.Canvas.pixels[x,y]:= bColor;
        end;
      end;
      if Img <> nil then
      begin
        Img.Picture.Bitmap.Assign(temBit);
        temBit.Free;
      end;
      Result := nRe;
    end;
    

    取灰度值, 设置容差, 返回差度

  • 相关阅读:
    SpringBoot HATEOAS用法简介
    犀函
    dubbo 相关面试题 有用(转)
    想使用消息队列,先考虑下这些问题!
    appium在Mac上环境搭建
    3. SOFAJRaft源码分析— 是如何进行选举的?
    Redis相关知识
    替代微信ipad协议(转)
    c#面试题(1)(转)
    例题6-5 Boxes in a line uVa12657
  • 原文地址:https://www.cnblogs.com/doorsky/p/2277771.html
Copyright © 2011-2022 走看看