A
download filterConvolution.pas
Language: Delphi
Copyright: (C) 2004 Durand Emmanuel (C) 2004 Burgel Eric
LOC: 270
Project Info
Filters
Server: SourceForge
Type: cvs
...ilters\Filters1\Src\Delphi\
   ...ractfilterNeighbor3.pas
   Chronometer.pas
   divers.pas
   filter.pas
   filterAdjust.pas
   filterArithmeticAdd.pas
   ...ithmeticConstantAdd.pas
   ...ArithmeticSubstract.pas
   filterBlobBalance.pas
   filterBlobExplorer.pas
   filterBlobGrouping.pas
   ...erBlobRepositioning.pas
   ...rBlobRepositioning2.pas
   filterBlur.pas
   filterCanny.pas
   filterContour.pas
   filterContrastExplorer.pas
   filterConvolution.pas
   filterCoocurenceMatrix.pas
   filterCopy.pas
   filterCorrelation.pas
   filterCutter.pas
   filterDistancesMap.pas
   filterExplorer.pas
   ...nisotropicDiffusion.pas
   ...GranularityExplorer.pas
   filterHistogram.pas
   ...erHistogramContrast.pas
   filterImageCreator.pas
   filterImageLoader.pas
   filterImageSaver.pas
   filterIntegration.pas
   filterInvert.pas
   filterLocalDeviation.pas
   filterLogPolar.pas
   filterMedian.pas
   filterMorphology.pas
   ...onMaximaSuppression.pas
   filterNormalize.pas
   filterOnOffCell.pas
   filterProjectionLine.pas
   filterPyramid.pas
   filterRescaleIntensity.pas
   filterResize.pas
   filterRotation.pas
   filterSigmoid.pas
   filterSmoothBilateral.pas
   filterSobel.pas
   filterSPV.pas
   filterStackProcessor.pas
   filterStackSmasher.pas
   ...erStandardDeviation.pas
   filterSUSAN.pas
   filterThresholdBinary.pas
   filterVectorHistogram.pas
   filterWavelets.pas
   filterWaves.pas
   fmask.pas
   fparameters.pas
   image.pas
   imageIO.pas
   imageIOVideo.pas
   lacModel.pas
   polygonalyzation.pas
   wrapper_itk.pas
   wrapper_opencv.pas

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
unit filterConvolution;
(* ***** BEGIN LICENSE BLOCK *****
 * Copyright (C) 2004 Durand Emmanuel
 * Copyright (C) 2004 Burgel Eric
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact :
 *   filters@edurand.com
 *   filters@burgel.com
 *
 * ***** END LICENSE BLOCK ***** *)

{
 eburgel (filters@burgel.com)
}

interface

uses
  filter, fparameters, image;

Type
  TConvolutionEnum = (convNone, convLaplace, convHipass, convFindEdges, convSharpen, convEdgeEnhance, convEmboss, convSoften, convBlur, convSobelHorizontal, convSobelVertical) ;
const
  TConvolutionEnumStr : Array[convNone..convSobelVertical] of String = ('None', 'Laplace', 'Hipass', 'FindEdges', 'Sharpen', 'EdgeEnhance', 'Emboss', 'Soften', 'Blur', 'SobelHorizontal', 'SobelVertical') ;
Type
  TFilterConvolution = class(TFilter)
  public
    constructor Create; override;
    procedure Run(); override;
    procedure setConvType(aConvType: TConvolutionEnum);
    procedure setParameterString( const aName, aValue:String ); override;
  private
    paramImageIn, paramImageOut : TParameterImage;
    paramImageConvolution : TParameterImage;
    paramConvType : TParameterString ;
    paramZ : TParameterInteger ;
    convType : TConvolutionEnum ;
    procedure _run();
  end;

implementation

Type
  PIntArray = ^IntArray;
  IntArray  = array[0..0] of Integer;

function Set255(Clr : integer) : integer;
asm
  MOV  EAX,Clr  // store value in EAX register (32-bit register)
  CMP  EAX,254  // compare it to 254
  JG   @SETHI   // if greater than 254 then go set to 255 (max value)
  CMP  EAX,1    // if less than 255, compare to 1
  JL   @SETLO   // if less than 1 go set to 0 (min value)
  RET           // otherwise it doesn't change, just exit
@SETHI:         // Set value to 255
  MOV  EAX,255  // Move 255 into the EAX register
  RET           // Exit (result value is the EAX register value)
@SETLO:         // Set value to 0
  MOV  EAX,0    // Move 0 into EAX register
end;            // Result is in EAX


procedure TFilterConvolution.setParameterString( const aName, aValue:String ); 
begin
  inherited;
  if(aName='convType') then begin
    if paramConvType.Value = 'None' Then setconvType(convNone)
    else if paramConvType.Value = 'Laplace' Then setconvType(convLaplace)
    else if paramConvType.Value = 'Hipass' Then setconvType(convHipass)
    else if paramConvType.Value = 'FindEdges' Then setconvType(convFindEdges)
    else if paramConvType.Value = 'Sharpen' Then setconvType(convSharpen)
    else if paramConvType.Value = 'EdgeEnhance' Then setconvType(convEdgeEnhance)
    else if paramConvType.Value = 'Emboss' Then setconvType(convEmboss)
    else if paramConvType.Value = 'Soften' Then setconvType(convSoften)
    else if paramConvType.Value = 'Blur' Then setconvType(convBlur)
    else if paramConvType.Value = 'SobelHorizontal' Then setconvType(convSobelHorizontal)
    else if paramConvType.Value = 'SobelVertical' Then setconvType(convSobelVertical) ;
  end;
end;

constructor TFilterConvolution.Create;
begin
  inherited;
  paramImageIn  := addParameterImage('inImage','Image to be filtered');
  paramImageOut := addParameterImage('outImage','Filtered image');
  paramConvType := addParameterString('convType', 'Convolution type', 'None');
  paramImageConvolution := addParameterImage('imageConv','BW image for convolution weigth');
  paramZ := addParameterInteger('z','global divisor',1, $10000, 128);
end;

procedure TFilterConvolution.Run;
begin
  if (paramImageIn.Image<>nil) and (paramImageOut.Image<>nil) then begin
    _run();
  end;
end;

procedure TFilterConvolution._run;
var
  pSrc, pLoopSrc, PDest, pConv, pLoopConv : PColor32Array;
  pLoopDest : PIntArray ;
  w,h : Word ;
  x,y, max, maxX : Cardinal ;

  convX, convY : word ;
  maxConvX, maxConvY : word ;
  weight : Integer ;
  value : Integer ;
  z : Integer ;

begin
  {inherited;}
  pSrc:=paramImageIn.Image.Bits;
  pDest:=paramImageOut.Image.Bits;

  h:=paramImageIn.Image.Height ;
  w:=paramImageIn.Image.Width  ;

  max := h*w ;
  maxX := w-2 ;

  pConv := paramImageConvolution.Image.Bits ;
  maxConvX := paramImageConvolution.Image.Width ;
  maxConvY := paramImageConvolution.Image.Height ;

  FillChar(pDest^, (max-1)*4, 0);

  pLoopConv := pConv ;
  for convY := 0 to maxConvY-1 do Begin
    for convX := 0 to maxConvX -1 do Begin
      weight := (Byte(pLoopConv[0]) and $FF) - 128;
      if weight <> 0 Then Begin
        for y:= 1 to h-2 do Begin
          pLoopSrc := pSrc ;
          inc (pLoopSrc, convY*w+convX) ;
          inc (pLoopSrc, (y-1)*w);

          pLoopDest := PIntArray(pDest) ;
          inc(pLoopDest, y*w+1) ;
          for x:= 1 to maxX do Begin
            inc(pLoopDest[0], (Intensity(pLoopSrc[0]))*weight) ;
            inc(pLoopSrc) ;
            inc(pLoopDest) ;
          End ;

        End ;
      End ;
      inc(pLoopConv) ;
    End ;
  End ;

  // normalization
  z := paramZ.Value ;
  pLoopDest := PIntArray(pDest) ;
  for x:=0 to max-1 do Begin
    value := (pLoopDest^[0]) * z;
    value := value div 128;
    value := Set255(value) ;
    PColor32Array(pLoopDest)^[0] := value*$10101 ;
    inc(pLoopDest) ;
  End ;
end;

procedure TFilterConvolution.setConvType(aConvType: TConvolutionEnum);
Var img : PBitmap32 ;
Begin
  if paramImageConvolution <> nil Then Begin
    //freeBitmap(paramImageConvolution.Image);
    //img := createBitmap(3, 3) ;
    img := paramImageConvolution.Image ;

    convType := aConvType ;
    Case aConvType of

      convNone : begin
        setWeight(img, 0, 0, 0) ;
        setWeight(img, 1, 0, 0) ;
        setWeight(img, 2, 0, 0) ;
        setWeight(img, 0, 1, 0) ;
        setWeight(img, 1, 1, 16) ;
        setWeight(img, 2, 1, 0) ;
        setWeight(img, 0, 2, 0) ;
        setWeight(img, 1, 2, 0) ;
        setWeight(img, 2, 2, 0) ;
        paramZ.Value := 8;

      End ;

      convLaplace : begin
        setWeight(img, 0, 0, -16) ;
        setWeight(img, 1, 0, -16) ;
        setWeight(img, 2, 0, -16) ;
        setWeight(img, 0, 1, -16) ;
        setWeight(img, 1, 1, 127) ;
        setWeight(img, 2, 1, -16) ;
        setWeight(img, 0, 2, -16) ;
        setWeight(img, 1, 2, -16) ;
        setWeight(img, 2, 2, -16) ;
        paramZ.Value := $80;
      End ;

      convHipass : Begin
        setWeight(img, 0, 0, -16) ;
        setWeight(img, 1, 0, -16) ;
        setWeight(img, 2, 0, -16) ;
        setWeight(img, 0, 1, -16) ;
        setWeight(img, 1, 1, 127) ;
        setWeight(img, 2, 1, -16) ;
        setWeight(img, 0, 2, -16) ;
        setWeight(img, 1, 2, -16) ;
        setWeight(img, 2, 2, -15) ;
        paramZ.Value := $80;
      End ;

      convfindEdges : Begin            // Find Edges (top down)
        setWeight(img, 0, 0, 64) ;
        setWeight(img, 1, 0, 64) ;
        setWeight(img, 2, 0, 64) ;
        setWeight(img, 0, 1, 64) ;
        setWeight(img, 1, 1, -128) ;
        setWeight(img, 2, 1, 64) ;
        setWeight(img, 0, 2, -64) ;
        setWeight(img, 1, 2, -64) ;
        setWeight(img, 2, 2, -64) ;
        paramZ.Value := $20;
      End ;

      convSharpen : begin  // Sharpen
        setWeight(img, 0, 0, -4) ;
        setWeight(img, 1, 0, -4) ;
        setWeight(img, 2, 0, -4) ;
        setWeight(img, 0, 1, -4) ;
        setWeight(img, 1, 1, 64) ;
        setWeight(img, 2, 1, -4) ;
        setWeight(img, 0, 2, -4) ;
        setWeight(img, 1, 2, -4) ;
        setWeight(img, 2, 2, -4) ;
        paramZ.Value := $4;
      end;

      convEdgeEnhance: Begin // Edge Enhance
        //ray[0] :=  0; ray[1] := -1; ray[2] :=  0;
        //ray[3] := -1; ray[4] :=  5; ray[5] := -1;
        //ray[6] :=  0; ray[7] := -1; ray[8] :=  0;
        //z := 1;
        setWeight(img, 0, 0, 0) ;
        setWeight(img, 1, 0, -16) ;
        setWeight(img, 2, 0, 0) ;
        setWeight(img, 0, 1, 0) ;
        setWeight(img, 1, 1, 80) ;
        setWeight(img, 2, 1, -16) ;
        setWeight(img, 0, 2, 0) ;
        setWeight(img, 1, 2, -16) ;
        setWeight(img, 2, 2, 0) ;
        paramZ.Value := $4;
      end;

      convEmboss : begin  // Emboss (Sorta)
        //ray[0] :=  1; ray[1] :=  0; ray[2] :=  1;
        //ray[3] :=  0; ray[4] :=  0; ray[5] :=  0;
        //ray[6] :=  1; ray[7] :=  0; ray[8] := -2;
        //z := 1;
        setWeight(img, 0, 0, 1) ;
        setWeight(img, 1, 0, 0) ;
        setWeight(img, 2, 0, 1) ;
        setWeight(img, 0, 1, 0) ;
        setWeight(img, 1, 1, 0) ;
        setWeight(img, 2, 1, 0) ;
        setWeight(img, 0, 2, 1) ;
        setWeight(img, 1, 2, 0) ;
        setWeight(img, 2, 2, -2) ;
        paramZ.Value := $80;
      end;

      convSoften : begin  // Soften
      //ray[0] :=  2; ray[1] :=  2; ray[2] :=  2;
      //ray[3] :=  2; ray[4] :=  0; ray[5] :=  2;
      //ray[6] :=  2; ray[7] :=  2; ray[8] :=  2;
      //z := 16;
        setWeight(img, 0, 0, 2) ;
        setWeight(img, 1, 0, 2) ;
        setWeight(img, 2, 0, 2) ;
        setWeight(img, 0, 1, 2) ;
        setWeight(img, 1, 1, 0) ;
        setWeight(img, 2, 1, 2) ;
        setWeight(img, 0, 2, 2) ;
        setWeight(img, 1, 2, 2) ;
        setWeight(img, 2, 2, 2) ;
        paramZ.Value := $8;
      end;

      convBlur : begin  // Blur
        //ray[0] :=  3; ray[1] :=  3; ray[2] :=  3;
        //ray[3] :=  3; ray[4] :=  8; ray[5] :=  3;
        //ray[6] :=  3; ray[7] :=  3; ray[8] :=  3;
        //z := 32;
        setWeight(img, 0, 0, 3) ;
        setWeight(img, 1, 0, 3) ;
        setWeight(img, 2, 0, 3) ;
        setWeight(img, 0, 1, 3) ;
        setWeight(img, 1, 1, 8) ;
        setWeight(img, 2, 1, 3) ;
        setWeight(img, 0, 2, 3) ;
        setWeight(img, 1, 2, 3) ;
        setWeight(img, 2, 2, 3) ;
        paramZ.Value := $4;
      end;

      // Gy
      convSobelVertical : begin
        setWeight(img, 0, 0, 1*16) ;
        setWeight(img, 1, 0, 2*16) ;
        setWeight(img, 2, 0, 1*16) ;
        setWeight(img, 0, 1, 0*16) ;
        setWeight(img, 1, 1, 0*16) ;
        setWeight(img, 2, 1, 0*16) ;
        setWeight(img, 0, 2, -1*16) ;
        setWeight(img, 1, 2, -2*16) ;
        setWeight(img, 2, 2, -1*16) ;
        paramZ.Value := 8;
      end;

      // Gx
      convSobelHorizontal : begin
        setWeight(img, 0, 0, 1*16) ;
        setWeight(img, 1, 0, 0*16) ;
        setWeight(img, 2, 0, -1*16) ;
        setWeight(img, 0, 1, 2*16) ;
        setWeight(img, 1, 1, 0*16) ;
        setWeight(img, 2, 1, -2*16) ;
        setWeight(img, 0, 2, 1*16) ;
        setWeight(img, 1, 2, 0*16) ;
        setWeight(img, 2, 2, -1*16) ;
        paramZ.Value := 8;
      end;

(*
    8 : begin  // LAP1
      ray[0] :=  0; ray[1] :=  1; ray[2] :=  0;
      ray[3] :=  1; ray[4] := -4; ray[5] :=  1;
      ray[6] :=  0; ray[7] :=  1; ray[8] :=  0;
      z := 1;
      end;

    9 : begin  // LAP4
      ray[0] :=  1; ray[1] := -2; ray[2] :=  1;
      ray[3] := -2; ray[4] :=  4; ray[5] := -2;
      ray[6] :=  1; ray[7] := -2; ray[8] :=  1;
      z := 1;
      end;

    10 : begin  // HP3
      ray[0] :=  1; ray[1] := -2; ray[2] :=  1;
      ray[3] := -2; ray[4] :=  5; ray[5] := -2;
      ray[6] :=  1; ray[7] := -2; ray[8] :=  1;
      z := 1;
      end;

    11 : begin  // Find Edges (Bottom top )
      ray[0] := -1; ray[1] := -1; ray[2] := -1;
      ray[3] :=  1; ray[4] := -2; ray[5] :=  1;
      ray[6] :=  1; ray[7] :=  1; ray[8] :=  1;
      z := 1;
      end;

    12 : begin  // Sobel hor
      ray[0] :=  1; ray[1] :=  2; ray[2] :=  1;
      ray[3] :=  0; ray[4] :=  0; ray[5] :=  0;
      ray[6] := -1; ray[7] := -2; ray[8] := -1;
      z := 1;
      end;

    13 : begin  // Sobel vert
      ray[0] :=  1; ray[1] :=  0; ray[2] := -1;
      ray[3] :=  2; ray[4] :=  0; ray[5] := -2;
      ray[6] :=  1; ray[7] :=  0; ray[8] := -1;
      z := 1;
      end;

    14 : begin  // Previt hor
      ray[0] :=  1; ray[1] :=  1; ray[2] :=  1;
      ray[3] :=  0; ray[4] :=  0; ray[5] :=  0;
      ray[6] := -1; ray[7] := -1; ray[8] := -1;
      z := 1;
      end;

    15 : begin  // Previt vert
      ray[0] :=  1; ray[1] :=  0; ray[2] := -1;
      ray[3] :=  1; ray[4] :=  0; ray[5] := -1;
      ray[6] :=  1; ray[7] :=  0; ray[8] := -1;
      z := 1;
      end;


    16 : begin // Laplace - Origin
      ray[0] := -1; ray[1] := -1; ray[2] := -1;
      ray[3] := -1; ray[4] :=  7; ray[5] := -1;
      ray[6] := -1; ray[7] := -1; ray[8] := -1;
      z := 1;
      end;

    17 : begin   //Gaussian Blure
      ray[0] :=  1; ray[1] :=  2; ray[2] :=  1;
      ray[3] :=  2; ray[4] :=  4; ray[5] :=  2;
      ray[6] :=  1; ray[7] :=  2; ray[8] :=  1;
      z := 16;
         end;
*)
    End ;
    paramImageConvolution.Image := img ;

  End
End ;


end.

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us