Advertisement
WarPie90

Simba - Align & Stack

Mar 14th, 2024
1,174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.46 KB | None | 0 0
  1. program new;
  2. {$I SRL/osr.simba}
  3. {$R-}
  4.  
  5. type
  6.   TMufImage  = TMufasaBitmap;
  7.   TMufImages = array of TMufImage;
  8.  
  9. function StackMedian(lst: TMufImages): TMufImage;
  10. var
  11.   x,y,i: Int32;
  12.   R,G,B: TIntegerArray;
  13.   color: TRGB32;
  14. begin
  15.   result.Init();
  16.   result.SetSize(lst[0].GetWidth(), lst[0].GetHeight());
  17.   SetLength(R, Length(lst));
  18.   SetLength(G, Length(lst));
  19.   SetLength(B, Length(lst));
  20.  
  21.   for y:=0 to result.GetHeight()-1 do
  22.     for x:=0 to result.GetWidth()-1 do
  23.     begin
  24.       for i:=0 to High(lst) do
  25.       begin
  26.         color := TRGB32(lst[i].GetPixel(x,y));
  27.         R[i] := color.R;
  28.         G[i] := color.G;
  29.         B[i] := color.B;
  30.       end;
  31.       color.R := R.Sorted()[length(R) div 2];
  32.       color.G := G.Sorted()[length(R) div 2];
  33.       color.B := B.Sorted()[length(R) div 2];
  34.  
  35.       result.SetPixel(x,y, TColor(color));
  36.     end;
  37. end;
  38.  
  39. procedure TMufImage.EnhanceColor(f: Single);
  40. var
  41.   x,y: Int32;
  42.   H,S,L: Extended;
  43. begin
  44.   for y:=0 to self.GetHeight()-1 do
  45.     for x:=0 to self.GetWidth()-1 do
  46.     begin
  47.       ColorToHSL(self.GetPixel(x,y),H,S,L);
  48.       Self.SetPixel(x,y, HSLToColor(H,Min(100,Max(0,S*f)),L));
  49.     end;
  50. end;
  51.  
  52. procedure TMufImage.LightContrast(f: Single);
  53. var
  54.   x,y: Int32;
  55.   H,S,L: Extended;
  56. begin
  57.   for y:=0 to self.GetHeight()-1 do
  58.     for x:=0 to self.GetWidth()-1 do
  59.     begin
  60.       ColorToHSL(self.GetPixel(x,y),H,S,L);
  61.       Self.SetPixel(x,y, HSLToColor(H,S,Min(100,Max(0,L*f))));
  62.     end;
  63. end;
  64.  
  65. procedure TMufImage.AdjustPop(f: Single; MidPt: Single=0.5);
  66. var
  67.   x,y: Int32;
  68.   H,S,L: Extended;
  69.   newS, newL: Extended;
  70. begin
  71.   for y:=0 to self.GetHeight()-1 do
  72.     for x:=0 to self.GetWidth()-1 do
  73.     begin
  74.       ColorToHSL(self.GetPixel(x,y),H,S,L);
  75.  
  76.       newS := Min(100,Max(0,(MidPt+(S/100-MidPt)*f)*100));
  77.       newL := (Min(100,Max(0,(MidPt+(L/100-MidPt)*f)*100)) + L) / 2;
  78.  
  79.       Self.SetPixel(x,y, HSLToColor(H, newS, newL));
  80.     end;
  81. end;
  82.  
  83. function StackMean(lst: TMufImages): TMufImage;
  84. var
  85.   x,y,i: Int32;
  86.   R,G,B: Int32;
  87.   color: TRGB32;
  88.  
  89. begin
  90.   result.Init();
  91.   result.SetSize(lst[0].GetWidth(), lst[0].GetHeight());
  92.  
  93.   for y:=0 to result.GetHeight()-1 do
  94.     for x:=0 to result.GetWidth()-1 do
  95.     begin
  96.       r := 0;
  97.       g := 0;
  98.       b := 0;
  99.       for i:=0 to High(lst) do
  100.       begin
  101.         color := TRGB32(lst[i].GetPixel(x,y));
  102.         R += color.R;
  103.         G += color.G;
  104.         B += color.B;
  105.       end;
  106.       R /= Length(lst);
  107.       G /= Length(lst);
  108.       B /= Length(lst);
  109.       color := [B,G,R];
  110.       result.SetPixel(x,y, TColor(color));
  111.     end;
  112. end;
  113.  
  114. procedure TMufasaBitmap.Offset(ax,ay: Int32);
  115. var w,h,x,y: Int32;
  116. begin
  117.   w := Self.getWidth()-1;
  118.   h := Self.getHeight()-1;
  119.  
  120.   for y:=h downto 0 do
  121.     for x:=w downto 0 do //boundchecking internaly in simba
  122.       self.SetPixel(x+ax,y+ay, self.GetPixel(x,y));
  123. end;
  124.  
  125. procedure Alignment(base:TMufImage; lst: TMufImages; Area:Int32=150);
  126.   function VerifyIntegrity(patch: TMufImage): Boolean;
  127.   var x,y: Int32;
  128.   begin
  129.     Result := patch.PeakBrightness() > 20;
  130.   end;
  131. var
  132.   locations: TPointArray;
  133.   patch,tmp: TMufImage;
  134.   i,x,y,w,h: Int32;
  135.   p: TPoint;
  136. begin
  137.   w := base.getWidth()-1;
  138.   h := base.getHeight()-1;
  139.   for patch in lst do
  140.   begin
  141.     locations := [];
  142.     for y:=0 to h with area do
  143.       for x:=0 to w with area do
  144.       begin
  145.         tmp := patch.Copy(x,y, Min(x+area, w), Min(y+area, h));
  146.         if VerifyIntegrity(tmp) then
  147.           locations += base.MatchTemplate(tmp, TM_CCOEFF_NORMED).ArgMax() + Point(-x,-y);
  148.  
  149.         tmp.Free();
  150.       end;
  151.  
  152.     p := locations.Median();
  153.     WriteLn('Offsetting by: ', p);
  154.     patch.Offset(p.x, p.y);
  155.   end;
  156. end;
  157.  
  158.  
  159.  
  160. var
  161.   patch,res,bmp: TMufImage;
  162.   i: Int32;
  163.   tmp,idx: string;
  164.   TSA: TStringArray;
  165.   mat: TSingleMatrix;
  166.   at,rel: TPoint;
  167.  
  168.   lst: TMufImages;
  169. begin
  170.   bmp.Init();
  171.   bmp.LoadFromFile('C:\Users\Eier\Desktop\Ny mappe (4)\crop\a_0000_392A0001.CR2.png');
  172.  
  173.   TSA := GetFiles('C:\Users\Eier\Desktop\Ny mappe (4)\crop\','png');
  174.  
  175.   WriteLn('Loading images...');
  176.   for tmp in TSA do
  177.   begin
  178.     patch.Init();
  179.     patch.LoadFromFile('C:\Users\Eier\Desktop\Ny mappe (4)\crop\'+tmp);
  180.  
  181.     lst += patch;
  182.     WriteLn(tmp);
  183.   end;
  184.  
  185.   WriteLn('Applying alignment...');
  186.   Alignment(bmp, lst);
  187.  
  188.   WriteLn('Stacking...');
  189.   res := StackMean(lst);
  190.  
  191.   res.EnhanceColor(1.8);
  192.   res.AdjustPop(1.6);
  193.   res.LightContrast(1.3);
  194.   res.Debug();
  195.  
  196.   res.SaveToFile('autostack.png');
  197.   res.Free();
  198. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement