ホーム > カテゴリ > 画像処理工学 >

画像の合成(グレースケール加算)のサンプルコード

画像に「合成(グレースケール加算)」の処理をするサンプルです。ソースコードはDelphi5で作成しましたがその他の言語でも流用できるかと思います。

画像処理の結果

合成をする元の2枚のイメージです。

画像の合成結果です。(2枚のイメージを1枚にする)

ソースコード

[EffectBass.pas - 汎用モジュール]

//Bass Unit
unit EffectBass;

interface
uses
 Windows,SysUtils, Classes, Graphics;

type
 //24bitアクセス用ポインタ
 pRGBarray  = ^TRGBarray;
 TRGBarray  = array[0..0] of TRGBTriple; //None リテラル

type
 //24bitアクセス用ダブルポインタ
 PPBits = ^TPBits;
 TPBits = array[0..0] of pRGBarray; //None リテラル

//汎用プロシージャ
procedure Set24bit(Src,Dest :TBitmap);
function  Set255(Value : integer) : BYTE;

implementation


/////////
procedure Set24bit(Src,Dest :Tbitmap);
begin
  Src.PixelFormat :=pf24bit;
  Dest.PixelFormat:=pf24bit;
  Dest.Width:=Src.Width;
  Dest.Height:=Src.Height;
end;

/////////
function Set255(Value:Integer):Byte;
begin
 if Value>=255 then
  Result:=255
 else if Value<=0 then
 Result:=0
 else Result:=Value;
end;
end.

[合成(グレースケール加算)の関数]

//-----------------------------------------------------------------------------
//■関数     EffectBlend_YIQ_Adding
//■用途     画像の合成(グレースケール加算)
//■引数     hBMP1,hBMP2 ...転送元のビットマップのハンドル
//■戻り値
//           新しいビットマップのハンドル
//※合成する画像は2枚とも同一サイズを使用する
//-----------------------------------------------------------------------------
function EffectBlend_YIQ_Adding(hBMP1,hBMP2:HBitmap):HBitmap;
var
  SrcBitmap1,SrcBitmap2,DestBitmap : TBitmap;
  Row, Col : Integer;
  SrcRow1,SrcRow2,DestRow : pRGBArray;
  YIQ1,YIQ2:Integer;
  wrk : Byte;
begin
  SrcBitmap1 := TBitmap.Create;
  SrcBitmap2 := TBitmap.Create;
  DestBitmap := TBitmap.Create;
  SrcBitmap1.Handle := hBMP1;
  SrcBitmap2.Handle := hBMP2;

  Set24bit(SrcBitmap1,DestBitmap);
  Set24bit(SrcBitmap1,SrcBitmap2);

  try
      for Row := 0 to SrcBitmap1.Height - 1 do
      begin
        SrcRow1 := SrcBitmap1.Scanline[Row];
        SrcRow2 := SrcBitmap2.Scanline[Row];
        DestRow := DestBitmap.Scanline[Row];
        for Col := 0 to SrcBitmap1.Width - 1 do
        begin
            // NTSC系加重平均法を用いてグレースケール化
            YIQ1 := Set255(Round(SrcRow1[Col].rgbtRed   * 0.289+
                                 SrcRow1[Col].rgbtGreen * 0.586+
                                 SrcRow1[Col].rgbtBlue  * 0.114 ));

            YIQ2 := Set255(Round(SrcRow2[Col].rgbtRed   * 0.289+
                                 SrcRow2[Col].rgbtGreen * 0.586+
                                 SrcRow2[Col].rgbtBlue  * 0.114 ));

            wrk := Set255(YIQ1+YIQ2);
            DestRow[Col].rgbtRed  := wrk;
            DestRow[Col].rgbtGreen:= wrk;
            DestRow[Col].rgbtBlue := wrk;
        end;
      end;

      Result:=DestBitmap.ReleaseHandle;
  except
    Result:=SrcBitmap1.ReleaseHandle;
  end;

  SrcBitmap1.free;
  SrcBitmap2.free;
  DestBitmap.free;
end;

[関数の呼び出し]

procedure TForm1.Button1Click(Sender: TObject);
begin
    Image1.Picture.Bitmap.Handle :=
      EffectBlend_YIQ_Adding(Image1.Picture.Bitmap.ReleaseHandle,
                             Image2.Picture.Bitmap.ReleaseHandle );
end;





関連記事



公開日:2015年02月24日
記事NO:00291