Lazarus

Programming => Graphics and Multimedia => Graphics => Topic started by: barakli on August 06, 2014, 10:27:06 pm

Title: [Solved] same example but it gives different values
Post by: barakli on August 06, 2014, 10:27:06 pm
i have two same examples. But two examples have different gray scale image. why it is different?

first example code is:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  IntfImg1, IntfImg2: TLazIntfImage;
  px, py: Integer;
  Row1, Row2: PRGBTripleArray;
  abitmap:TBitmap;
begin
  abitmap:=TBitmap.Create;
  abitmap.LoadFromFile('bb.bmp');
  IntfImg1:=abitmap.CreateIntfImage;
  IntfImg2:=TLazIntfImage.Create(0,0);
  IntfImg2.LoadFromBitmap(aBitmap.Handle,aBitmap.MaskHandle);

   for py:=0 to IntfImg1.Height-1 do begin
     Row1 := IntfImg1.GetDataLineStart(py); //like Delphi TBitMap.ScanLine
     Row2 := IntfImg2.GetDataLineStart(py); //like Delphi TBitMap.ScanLine
     for px:=0 to IntfImg1.Width-1 do begin
       Row2^[px].rgbtRed    :=  round((Row1^[px].rgbtRed+Row1^[px].rgbtGreen+Row1^[px].rgbtBlue)/3);
       Row2^[px].rgbtGreen  :=  round((Row1^[px].rgbtRed+Row1^[px].rgbtGreen+Row1^[px].rgbtBlue)/3);
       Row2^[px].rgbtBlue   :=  round((Row1^[px].rgbtRed+Row1^[px].rgbtGreen+Row1^[px].rgbtBlue)/3);
     end;
   end;

   Image1.Stretch:=True;
   Image1.Picture.Bitmap.LoadFromIntfImage(IntfImg2);
end;

second code is:
Code: [Select]
procedure TForm1.Button2Click(Sender: TObject);
  var
  IntfImg1, IntfImg2: TLazIntfImage;
  px, py: Integer;
  Row1, Row2: PRGBTripleArray;
  abitmap:TBitmap;
  CurColor: TFPColor;
begin
   abitmap:=TBitmap.Create;
   abitmap.LoadFromFile('bb.bmp');
   IntfImg1:=abitmap.CreateIntfImage;
   IntfImg2:=TLazIntfImage.Create(0,0);
   IntfImg2.LoadFromBitmap(aBitmap.Handle,aBitmap.MaskHandle);

    for py:=0 to IntfImg1.Height-1 do begin
      Row1 := IntfImg1.GetDataLineStart(py); //like Delphi TBitMap.ScanLine
      Row2 := IntfImg2.GetDataLineStart(py); //like Delphi TBitMap.ScanLine
      for px:=0 to IntfImg1.Width-1 do begin
        CurColor:=IntfImg1.Colors[px,py];
        CurColor.red    :=  round((CurColor.red+CurColor.green+CurColor.blue)/3);
        CurColor.green  :=  round((CurColor.red+CurColor.green+CurColor.blue)/3);
        CurColor.blue   :=  round((CurColor.red+CurColor.green+CurColor.blue)/3);
        IntfImg2.Colors[px,py]:=CurColor;
      end;
    end;

    Image1.Stretch:=True;
    Image1.Picture.Bitmap.LoadFromIntfImage(IntfImg2);
end;

first example result:       http://www.opencvtr.net/buglikod/1.png
second example result:  http://www.opencvtr.net/buglikod/2.png
Title: Re: same example but it gives different values
Post by: engkin on August 06, 2014, 11:21:57 pm
Welcome to FPC/Lazarus.

This mistake should teach you two things:
1-Do not repeat the same calculation.
2-Repeating the same calculations is wrong. The second case:
Quote
        CurColor:=IntfImg1.Colors[px,py];
        CurColor.red    :=  round((CurColor.red+CurColor.green+CurColor.blue)/3);
        CurColor.green  :=  round((CurColor.red+CurColor.green+CurColor.blue)/3);
        CurColor.blue   :=  round((CurColor.red+CurColor.green+CurColor.blue)/3);
Legend:
Original value, New Value.

Do you see the mistake?
Title: Re: same example but it gives different values
Post by: Graeme on August 07, 2014, 12:17:29 am
Do you see the mistake?
At least the second one makes for a nice "sepia effect" ;)
Title: Re: same example but it gives different values
Post by: engkin on August 07, 2014, 04:56:35 am
At least the second one makes for a nice "sepia effect" ;)
That's true, it is nice.
Title: Re: same example but it gives different values
Post by: barakli on August 07, 2014, 08:22:18 pm
Thank you very much to engkin and  i love this mistake. in God, I've done this error.
TinyPortal © 2005-2018