1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| function TBZBitmapSegmentationFilters.ComputeConvolutionGradientY(aMode : TBZDetectEdgeFilterMode) : TBZSingle2DMap;
Var
size, moffset, idx, grad, x, y, i, j, pX, pY : Integer;
KS, KSDiv2: Byte;
KernelValue, K: Single;
Sum, Value : Single;
aMatrix : Array of Single;
begin
Case aMode of
defPrewitt: idx := 17;
defSobel: idx := 21;
defRoberts: idx := 19;
defKirsch: idx := 23;
defScharr: idx := 25;
defRobinson: idx := 27;
defMDif: idx := 29;
defLaplace: idx := 38;
end;
Size := BZConvolutionFilterPresets[idx].MatrixSize;
Case size of
3: aMatrix := BZConvolutionFilterPresets[idx].Matrix._3;
5: aMatrix := BZConvolutionFilterPresets[idx].Matrix._5;
7: aMatrix := BZConvolutionFilterPresets[idx].Matrix._7;
end;
KS := Size - 1;
KSDiv2 := KS Div 2;
Result := TBZSingle2DMap.Create(OwnerBitmap.Width, OwnerBitmap.Height);
For Y := 0 To OwnerBitmap.MaxHeight Do
Begin
For X := 0 To OwnerBitmap.MaxWidth Do
Begin
Sum := 0;
For J := 0 To KS Do
Begin
pY := Clamp(Y + J - KSDiv2, 0, OwnerBitmap.MaxHeight);
moffset := J * Size;
For I := 0 To KS Do
Begin
pX := Clamp(X + I - KSDiv2, 0, OwnerBitmap.MaxWidth);
KernelValue := aMatrix[moffset + I]; // Valeur dans la matrice
Value := OwnerBitmap.GetPixel(pX, pY).Red * _FloatColorRatio;
Value := (Value * KernelValue);
Sum := Sum + Value;
end;
end;
Result.Add(Sum);
end;
end;
end;
procedure TBZBitmapSegmentationFilters.GradientConvolution(aMode : TBZDetectEdgeFilterMode);
Var
x, y : Integer;
GradX, GradY : TBZSingle2DMap;
gX, gY : Single;
c : Integer;
begin
GradX := Self.ComputeConvolutionGradientX(aMode);
GradY := Self.ComputeConvolutionGradientX(aMode);
For y := 0 to OwnerBitmap.MaxHeight do
begin
for x := 0 to OwnerBitmap.MaxWidth do
begin
gX := GradX.Items[x,y];
gY := GradY.Items[x,y];
c := Round(Clamp(System.Sqrt(gX * gX + gY * gY),0,1.0) * 255);
OwnerBitmap.setPixel(x,y, BZColor(c,c,c));
end;
end;
end; |