download filterDistancesMap.pas
Language: Delphi
Copyright: (C) 2004 Durand Emmanuel (C) 2004 Burgel Eric
LOC: 271
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
unit filterDistancesMap;
(* ***** 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 ***** *)

{
 edurand (filters@edurand.com)
 eburgel (filters@burgel.com)
}


interface
uses
  filter, fparameters, image;

type
  TFilterDistancesMap = class(TFilter)
  public
    constructor Create; override;
    procedure Run(); override;
  private
    parameterImageIn, parameterImageOut : TParameterImage;
    parameterPreProcessInvert : TParameterBoolean ;
    parameterMaxEDM : TParameterInteger ;
    parameterPostProcessScale : TParameterInteger ;
    imgIn, imgOut : PBitmap32 ;
    procedure _run();
    procedure setValue (offset, rowsize : Integer; image16:PColor32Array) ;
    procedure setEdgeValue (offset, rowsize : Integer; image16:PColor32Array ; x, y, xmax, ymax : Integer) ;
  end;

implementation
//uses
  //imageIO;

Const
  one = 41;
  sqrt2 = 58; // ~ 41 * sqrt(2)
  sqrt5 = 92; // ~ 41 * sqrt(5)

constructor TFilterDistancesMap.Create;
begin
  inherited;
  parameterImageIn:=addParameterImage('inImage', 'input image to invert');
  parameterImageOut:=addParameterImage('outImage', 'result image of invertion');
  parameterPreProcessInvert := addParameterBoolean('preprocessInvert', 'invertion of image before processing', false) ; ;
  parameterMaxEDM :=addOutputParameterInteger('maxEDM', 'Maximum EDM in this image');
  parameterPostProcessScale := addParameterInteger('scale', 'scale', 1, 40, 1) ;
end;

procedure TFilterDistancesMap.run () ;
begin
  imgIn   := parameterImageIn.Image ;
  imgOut  := parameterImageOut.Image ;
  if (imgIn<>nil) and (imgOut<>nil) then begin
    _run();
  end;
end;

procedure TFilterDistancesMap._run () ;
Var
  x, y, width, height : Integer ;
  xmax, ymax : Integer ;
  offset, rowsize : Integer ;
  pSrc, image16 : PColor32Array;
  rounded : cardinal ;
  scale : cardinal ;
  v : integer ;
  outColor : TColor32 ;
  maxEDM : integer ;
  preInvert : Boolean ;
Begin
  // showStatus("Generating EDM")
  preInvert := parameterPreProcessInvert.Value ;
  scale := parameterPostProcessScale.Value ;
  width   := scanWidth(imgIn);
  height  := scanHeight(imgIn) ;
  rowsize := width;

  xmax := scanWidth(imgIn)-1 ;
  ymax := scanHeight(imgIn)-1 ;
  for y:=0 to ymax do begin
    pSrc := scanLine(imgIn, y) ;
    image16 := scanLine(imgOut, y) ;
    if (preInvert) then Begin
      for x:=0 to xmax do begin
        //image16^[0] := (pSrc^[0] and $FF) * one;
        image16^[0] := ($FF - Intensity(pSrc^[0])) * one;
        inc(image16) ;
        inc(pSrc) ;
      end ;
    end else Begin
      for x:=0 to xmax do begin
        //image16^[0] := (pSrc^[0] and $FF) * one;
        image16^[0] := Intensity(pSrc^[0]) * one;
        inc(image16) ;
        inc(pSrc) ;
      end ;
    end ;
  end ;

  xmax    := width - 3;
  ymax    := height - 3;

  image16 := scanLine(imgOut, 0) ;
  for y:=0 to height-1 do Begin
    for x:=0 to width-1 do Begin
      offset := x + y * rowsize;
      if (image16[offset] > 0) Then Begin
        if ((x<2) or (x>xmax) or (y<2) or (y>ymax)) Then
          setEdgeValue(offset, rowsize, image16, x, y, xmax, ymax)
        else
          setValue(offset, rowsize, image16);
      end ;
    end ;
  end ;

  for y := height-1 downto 0 do Begin
    for x := width-1 downto 0 do Begin
      offset := x + y * rowsize;
      if (image16[offset] > 0) Then Begin
        if ((x<2) or (x>xmax) or (y<2) or (y>ymax)) then
          setEdgeValue (offset, rowsize, image16, x, y, xmax, ymax)
        else
          setValue (offset, rowsize, image16);
      end ;
    end ;
  end ;

  // Back to 8 bits images
  rounded := one div 2;
  maxEDM := 0;
  for y:=0 to height-1 do Begin
    for x:=0 to width-1 do Begin
      offset := x + y * width;
      v := (image16[offset] + rounded) div one * scale ;
      if (v > 255) then Begin
        outColor := clRed32 ;
        v := 255;
      end else
        outColor := v*$10101 ;
      if (v>maxEDM) then maxEDM := v;
      image16[offset] := outColor;
    end ;
  end ;
  setOutputParameterInteger('maxEDM',maxEDM);

end ; // run)

procedure  TFilterDistancesMap.setValue(offset, rowsize : Integer; image16:PColor32Array) ;
Const
  one = 41;
  sqrt2 = 58; // ~ 41 * sqrt(2)
  sqrt5 = 92; // ~ 41 * sqrt(5)
Var
  v: Integer ;
  r1, r2, r3, r4, r5 : Integer ;
  min : Integer ;
Begin
  r1 := offset - rowsize - rowsize - 2;
  r2 := r1 + rowsize;
  r3 := r2 + rowsize;
  r4 := r3 + rowsize;
  r5 := r4 + rowsize;
  min := 32767;

  v := image16[r2 + 2] + one;
  if (v < min) then min := v;
  v := image16[r3 + 1] + one;
  if (v < min) then min := v;
  v := image16[r3 + 3] + one;
  if (v < min) then min := v;
  v := image16[r4 + 2] + one;
  if (v < min) then min := v;

  v := image16[r2 + 1] + sqrt2;
  if (v < min) then min := v;
  v := image16[r2 + 3] + sqrt2;
  if (v < min) then min := v;
  v := image16[r4 + 1] + sqrt2;
  if (v < min)then min := v;
  v := image16[r4 + 3] + sqrt2;
  if (v < min)then min := v;

  v := image16[r1 + 1] + sqrt5;
  if (v < min) then min := v;
  v := image16[r1 + 3] + sqrt5;
  if (v < min) then min := v;
  v := image16[r2 + 4] + sqrt5;
  if (v < min) then min := v;
  v := image16[r4 + 4] + sqrt5;
  if (v < min) then min := v;

  v := image16[r5 + 3] + sqrt5;
  if (v < min) then min := v;
  v := image16[r5 + 1] + sqrt5;
  if (v < min) then min := v;
  v := image16[r4] + sqrt5;
  if (v < min) then min := v;
  v := image16[r2] + sqrt5;
  if (v < min) then min := v;

  image16[offset] := min;
end ; // setValue()

procedure TFilterDistancesMap.setEdgeValue (offset, rowsize : Integer; image16:PColor32Array ; x, y, xmax, ymax : Integer) ;
Const
  one = 41;
  sqrt2 = 58; // ~ 41 * sqrt(2)
  sqrt5 = 92; // ~ 41 * sqrt(5)
Var
  v: Integer ;
  r1, r2, r3, r4, r5 : Integer ;
  min : Integer ;
  offimage : Integer ;
Begin
  r1 := offset - rowsize - rowsize - 2;
  r2 := r1 + rowsize;
  r3 := r2 + rowsize;
  r4 := r3 + rowsize;
  r5 := r4 + rowsize;
  min := 32767;
  offimage := image16[r3 + 2];

  if (y<2) then
    v := offimage + one
  else
    v := image16[r2 + 2] + one;
  if (v < min) then min := v;

  if (x<2) then
      v := offimage + one
  else
      v := image16[r3 + 1] + one;
  if (v < min) then min := v;

  if (x>xmax) then
      v := offimage + one
  else
      v := image16[r3 + 3] + one;
  if (v < min) then min := v;

  if (y>ymax) then
      v := offimage + one
  else
      v := image16[r4 + 2] + one;
  if (v < min) then min := v;

  if ((x<2) or (y<2)) then
      v := offimage + sqrt2
  else
      v := image16[r2 + 1] + sqrt2;
  if (v < min) then min := v;

  if ((x>xmax) or (y<2)) then
      v := offimage + sqrt2
  else
      v := image16[r2 + 3] + sqrt2;
  if (v < min) then min := v;

  if ((x<2) or (y>ymax)) then
      v := offimage + sqrt2
  else
      v := image16[r4 + 1] + sqrt2;
  if (v < min) then min := v;

  if ((x>xmax) or (y>ymax)) then
      v := offimage + sqrt2
  else
      v := image16[r4 + 3] + sqrt2;
  if (v < min) then min := v;

  if ((x<2) or (y<2)) then
      v := offimage + sqrt5
  else
      v := image16[r1 + 1] + sqrt5;
  if (v < min) then min := v;

  if ((x>xmax) or (y<2)) then
      v := offimage + sqrt5
  else
      v := image16[r1 + 3] + sqrt5;
  if (v < min) then min := v;

  if ((x>xmax) or (y<2)) then
      v := offimage + sqrt5
  else
      v := image16[r2 + 4] + sqrt5;
  if (v < min) then min := v;

  if ((x>xmax) or (y>ymax)) then
      v := offimage + sqrt5
  else
      v := image16[r4 + 4] + sqrt5;
  if (v < min) then min := v;

  if ((x>xmax) or (y>ymax)) then
      v := offimage + sqrt5
  else
      v := image16[r5 + 3] + sqrt5;
  if (v < min) then min := v;

  if ((x<2) or (y>ymax)) then
      v := offimage + sqrt5
  else
      v := image16[r5 + 1] + sqrt5;
  if (v < min) then min := v;

  if ((x<2) or (y>ymax)) then
      v := offimage + sqrt5
  else
      v := image16[r4] + sqrt5;
  if (v < min) then min := v;

  if ((x<2) or (y<2)) then
      v := offimage + sqrt5
  else
      v := image16[r2] + sqrt5;
  if (v < min) then min := v;

  image16[offset] := min;
end ;

end.

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