Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program new;
- {$I SRL/osr.simba}
- {$R-}
- type
- TMufImage = TMufasaBitmap;
- TMufImages = array of TMufImage;
- function StackMedian(lst: TMufImages): TMufImage;
- var
- x,y,i: Int32;
- R,G,B: TIntegerArray;
- color: TRGB32;
- begin
- result.Init();
- result.SetSize(lst[0].GetWidth(), lst[0].GetHeight());
- SetLength(R, Length(lst));
- SetLength(G, Length(lst));
- SetLength(B, Length(lst));
- for y:=0 to result.GetHeight()-1 do
- for x:=0 to result.GetWidth()-1 do
- begin
- for i:=0 to High(lst) do
- begin
- color := TRGB32(lst[i].GetPixel(x,y));
- R[i] := color.R;
- G[i] := color.G;
- B[i] := color.B;
- end;
- color.R := R.Sorted()[length(R) div 2];
- color.G := G.Sorted()[length(R) div 2];
- color.B := B.Sorted()[length(R) div 2];
- result.SetPixel(x,y, TColor(color));
- end;
- end;
- procedure TMufImage.EnhanceColor(f: Single);
- var
- x,y: Int32;
- H,S,L: Extended;
- begin
- for y:=0 to self.GetHeight()-1 do
- for x:=0 to self.GetWidth()-1 do
- begin
- ColorToHSL(self.GetPixel(x,y),H,S,L);
- Self.SetPixel(x,y, HSLToColor(H,Min(100,Max(0,S*f)),L));
- end;
- end;
- procedure TMufImage.LightContrast(f: Single);
- var
- x,y: Int32;
- H,S,L: Extended;
- begin
- for y:=0 to self.GetHeight()-1 do
- for x:=0 to self.GetWidth()-1 do
- begin
- ColorToHSL(self.GetPixel(x,y),H,S,L);
- Self.SetPixel(x,y, HSLToColor(H,S,Min(100,Max(0,L*f))));
- end;
- end;
- procedure TMufImage.AdjustPop(f: Single; MidPt: Single=0.5);
- var
- x,y: Int32;
- H,S,L: Extended;
- newS, newL: Extended;
- begin
- for y:=0 to self.GetHeight()-1 do
- for x:=0 to self.GetWidth()-1 do
- begin
- ColorToHSL(self.GetPixel(x,y),H,S,L);
- newS := Min(100,Max(0,(MidPt+(S/100-MidPt)*f)*100));
- newL := (Min(100,Max(0,(MidPt+(L/100-MidPt)*f)*100)) + L) / 2;
- Self.SetPixel(x,y, HSLToColor(H, newS, newL));
- end;
- end;
- function StackMean(lst: TMufImages): TMufImage;
- var
- x,y,i: Int32;
- R,G,B: Int32;
- color: TRGB32;
- begin
- result.Init();
- result.SetSize(lst[0].GetWidth(), lst[0].GetHeight());
- for y:=0 to result.GetHeight()-1 do
- for x:=0 to result.GetWidth()-1 do
- begin
- r := 0;
- g := 0;
- b := 0;
- for i:=0 to High(lst) do
- begin
- color := TRGB32(lst[i].GetPixel(x,y));
- R += color.R;
- G += color.G;
- B += color.B;
- end;
- R /= Length(lst);
- G /= Length(lst);
- B /= Length(lst);
- color := [B,G,R];
- result.SetPixel(x,y, TColor(color));
- end;
- end;
- procedure TMufasaBitmap.Offset(ax,ay: Int32);
- var w,h,x,y: Int32;
- begin
- w := Self.getWidth()-1;
- h := Self.getHeight()-1;
- for y:=h downto 0 do
- for x:=w downto 0 do //boundchecking internaly in simba
- self.SetPixel(x+ax,y+ay, self.GetPixel(x,y));
- end;
- procedure Alignment(base:TMufImage; lst: TMufImages; Area:Int32=150);
- function VerifyIntegrity(patch: TMufImage): Boolean;
- var x,y: Int32;
- begin
- Result := patch.PeakBrightness() > 20;
- end;
- var
- locations: TPointArray;
- patch,tmp: TMufImage;
- i,x,y,w,h: Int32;
- p: TPoint;
- begin
- w := base.getWidth()-1;
- h := base.getHeight()-1;
- for patch in lst do
- begin
- locations := [];
- for y:=0 to h with area do
- for x:=0 to w with area do
- begin
- tmp := patch.Copy(x,y, Min(x+area, w), Min(y+area, h));
- if VerifyIntegrity(tmp) then
- locations += base.MatchTemplate(tmp, TM_CCOEFF_NORMED).ArgMax() + Point(-x,-y);
- tmp.Free();
- end;
- p := locations.Median();
- WriteLn('Offsetting by: ', p);
- patch.Offset(p.x, p.y);
- end;
- end;
- var
- patch,res,bmp: TMufImage;
- i: Int32;
- tmp,idx: string;
- TSA: TStringArray;
- mat: TSingleMatrix;
- at,rel: TPoint;
- lst: TMufImages;
- begin
- bmp.Init();
- bmp.LoadFromFile('C:\Users\Eier\Desktop\Ny mappe (4)\crop\a_0000_392A0001.CR2.png');
- TSA := GetFiles('C:\Users\Eier\Desktop\Ny mappe (4)\crop\','png');
- WriteLn('Loading images...');
- for tmp in TSA do
- begin
- patch.Init();
- patch.LoadFromFile('C:\Users\Eier\Desktop\Ny mappe (4)\crop\'+tmp);
- lst += patch;
- WriteLn(tmp);
- end;
- WriteLn('Applying alignment...');
- Alignment(bmp, lst);
- WriteLn('Stacking...');
- res := StackMean(lst);
- res.EnhanceColor(1.8);
- res.AdjustPop(1.6);
- res.LightContrast(1.3);
- res.Debug();
- res.SaveToFile('autostack.png');
- res.Free();
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement