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

interface
uses
  filter, fparameters, image, SysUtils;

type
  TFilterGranularityExplorer = class(TFilter)
  public
    constructor Create; override;
    destructor Destroy; override;
    procedure Run(); override;
    procedure setParameterImage( const aName: String; const aImage: PBitmap32); override;
  private
    parameterPrecision : TParameterInteger;
    parameterSensibility : TParameterInteger;
    parameterImageIn, parameterImageOut : TParameterImage;
    imageDiffHorizontal, imageDiffVertical : PBitmap32;
    procedure destroyImages;
    procedure _run();
    procedure _runLong();
    procedure calculateDiffHorizontal();
    procedure calculateDiffVertical();
  end;

implementation
uses
  imageIO, Math;

constructor TFilterGranularityExplorer.Create;
begin
  inherited;
  parameterImageIn:=addParameterImage('inImage','explication...');
  parameterImageOut:=addParameterImage('outImage','explication...');
  parameterPrecision:=addParameterInteger('precision','',0,10,2);
  parameterSensibility:=addParameterInteger('sensibility','',1,15,10);
end;

destructor TFilterGranularityExplorer.Destroy;
begin
  inherited;
  destroyImages;
end;

procedure TFilterGranularityExplorer.destroyImages;
begin
  image.freeImage(imageDiffHorizontal);
  image.freeImage(imageDiffVertical);
end;

procedure TFilterGranularityExplorer.Run();
begin
  if (parameterImageIn.Image<>nil) and (parameterImageOut.Image<>nil) then begin
    _run();
  end;
end;


procedure TFilterGranularityExplorer.setParameterImage( const aName: String; const aImage: PBitmap32);
var
  createImages : boolean;
  w, h : Integer;
begin
  inherited;
  if aName='inImage' then begin
    // we create images with 1 pixel removed
    w:=parameterImageIn.Image.Width-1;
    h:=parameterImageIn.Image.Height-1;
    createImages:=true;
    if (imageDiffHorizontal<>nil) then begin
      if (imageDiffHorizontal.Width=w) and
         (imageDiffHorizontal.Height=h) then begin
         createImages:=false;
      end;
    end;
    if createImages=true then begin
      destroyImages;
      imageDiffHorizontal:=createImage(w,parameterImageIn.Image.Height);
      imageDiffVertical:=createImage(parameterImageIn.Image.Width,h);
    end else begin
      eraseImage(imageDiffHorizontal);
      eraseImage(imageDiffVertical);
    end;
  end;
end;

// Create a image containing all vertical + horizontal difference.
// Example for a precision of 3 (=3*3):
//  diff( row 10, col 100) =
//   (diff horizontal)
//    (src( row 9, col 100) - src( row 9, col 99)) +
//    (src( row 9, col 101) - src( row 9, col 100)) +
//    (src( row 10, col 100) - src( row 10, col 99)) +
//    (src( row 10, col 101) - src( row 10, col 100)) +
//    (src( row 11, col 100) - src( row 11, col 99)) +
//    (src( row 11, col 101) - src( row 11, col 100)) +
//   (diff vertical)
//    (src( row 10, col 99) - src( row 9, col 99)) +
//    (src( row 11, col 99) - src( row 10, col 99)) +
//    (src( row 10, col 100) - src( row 9, col 100)) +
//    (src( row 11, col 100) - src( row 10, col 100)) +
//    (src( row 10, col 101) - src( row 9, col 101)) +
//    (src( row 11, col 101) - src( row 10, col 101)) +
procedure TFilterGranularityExplorer._run();
var
  PDest : PColor32Array;
  w,h :Integer;
  value : Integer;
  diff : Integer;
  tContraste : Integer;
  tContrasteHorizontal, tContrasteVertical : Integer;
  neighbor :Integer;
  imageSrcRow, imageSrcCol : Integer;
  imageSrcRowmax, imageSrcColmax : Integer;
  imageSrcNeighborRow, imageSrcNeighborCol : Integer;
  imageSrcNeighborRowmin, imageSrcNeighborColmin : Integer;
  imageSrcNeighborRowmax, imageSrcNeighborColmax : Integer;
  pSrcDiffHorizontal, pSrcDiffVertical : PColor32Array;
  imageSrcDiffHorizontalRowmax, imageSrcDiffHorizontalColmax : Integer;
  imageSrcDiffVerticalRowmax, imageSrcDiffVerticalColmax : Integer;
begin
  h:=parameterImageIn.Image.Height;
  w:=parameterImageIn.Image.Width;
  neighbor:=parameterPrecision.Value;

  // We start by calculate the difference in horizontal and vertical
  // The result is in imageDiffHorizontal and imageDiffVertical
  calculateDiffHorizontal();
  calculateDiffVertical();

  imageSrcRowmax:=h-1;
  imageSrcColmax:=w-1;
  pSrcDiffHorizontal:=imageDiffHorizontal.Bits;
  imageSrcDiffHorizontalRowmax:=imageDiffHorizontal.Height-1;
  imageSrcDiffHorizontalColmax:=imageDiffHorizontal.Width-1;
  pSrcDiffVertical:=imageDiffVertical.Bits;
  imageSrcDiffVerticalRowmax:=imageDiffVertical.Height-1;
  imageSrcDiffVerticalColmax:=imageDiffVertical.Width-1;
  for imageSrcRow:=0 to imageSrcRowmax do begin
    pDest:=parameterImageOut.Image.Bits;
    Inc(pDest,imageSrcRow*w);
    for imageSrcCol:=0 to imageSrcColmax do begin
      // contrast horizontal and contrast vertical
      tContrasteHorizontal:=0;
      tContrasteVertical:=0;
      imageSrcNeighborRowmin:=imageSrcRow-neighbor+1; if imageSrcNeighborRowmin<0 then imageSrcNeighborRowmin:=0;
      imageSrcNeighborColmin:=imageSrcCol-neighbor+1; if imageSrcNeighborColmin<0 then imageSrcNeighborColmin:=0;
      imageSrcNeighborRowmax:=imageSrcRow+neighbor;
      if imageSrcNeighborRowmax>imageSrcDiffHorizontalRowmax then imageSrcNeighborRowmax:=imageSrcDiffHorizontalRowmax;
      if imageSrcNeighborRowmax>imageSrcDiffVerticalRowmax then imageSrcNeighborRowmax:=imageSrcDiffVerticalRowmax;
      imageSrcNeighborColmax:=imageSrcCol+neighbor;
      if imageSrcNeighborColmax>imageSrcDiffHorizontalColmax then imageSrcNeighborColmax:=imageSrcDiffHorizontalColmax;
      if imageSrcNeighborColmax>imageSrcDiffVerticalColmax then imageSrcNeighborColmax:=imageSrcDiffVerticalColmax;
      for imageSrcNeighborRow:=imageSrcNeighborRowmin to imageSrcNeighborRowmax do begin
        pSrcDiffHorizontal:=imageDiffHorizontal.Bits;
        Inc(pSrcDiffHorizontal,imageSrcNeighborRow*imageDiffHorizontal.Width+imageSrcNeighborColmin);
        pSrcDiffVertical:=imageDiffVertical.Bits;
        Inc(pSrcDiffVertical,imageSrcNeighborRow*imageDiffVertical.Width+imageSrcNeighborColmin);
        for imageSrcNeighborCol:=imageSrcNeighborColmin to imageSrcNeighborColmax do begin
          // horizontal
          value:=pSrcDiffHorizontal^[0];
          Inc(pSrcDiffHorizontal);
          diff:=value and $000000FF;
          Inc(tContrasteHorizontal,diff);
          // vertical
          value:=pSrcDiffVertical^[0];
          Inc(pSrcDiffVertical);
          diff:=value and $000000FF;
          Inc(tContrasteVertical,diff);
        end;
      end;
      // contrast global
      tContraste:=tContrasteHorizontal+tContrasteVertical;
      if tContraste>255 then tContraste:=255;
      // set gray value with Color32 for optimization
      pDest^[0]:= Color32(tContraste,tContraste,tContraste);
      Inc(pDest);
    end;
  end;

end;

procedure TFilterGranularityExplorer._runLong();
var
  pSrc, PDest : PColor32Array;
  w,h :Integer;
  value : Integer;
  tIntensity, tIntensityPreviouse : Integer;
  diff : Integer;
  tContraste : Integer;
  sensibility : Integer;
  neighbor :Integer;
  imageSrcRow, imageSrcCol : Integer;
  imageSrcRowmax, imageSrcColmax : Integer;
  imageSrcNeighborRow, imageSrcNeighborCol : Integer;
  imageSrcNeighborRowmin, imageSrcNeighborColmin : Integer;
  imageSrcNeighborRowmax, imageSrcNeighborColmax : Integer;
begin
  h:=parameterImageIn.Image.Height;
  w:=parameterImageIn.Image.Width;
  neighbor:=parameterPrecision.Value;
  sensibility:=parameterSensibility.Max-parameterSensibility.Value+1;
  pSrc:=parameterImageIn.Image.Bits;
  imageSrcRowmax:=h-1;
  imageSrcColmax:=w-1;
  for imageSrcRow:=0 to imageSrcRowmax do begin
    pDest:=parameterImageOut.Image.Bits;
    Inc(pDest,imageSrcRow*w);
    for imageSrcCol:=0 to imageSrcColmax do begin
      tContraste:=0;
      imageSrcNeighborRowmin:=imageSrcRow-neighbor; if imageSrcNeighborRowmin<0 then imageSrcNeighborRowmin:=0;
      imageSrcNeighborColmin:=imageSrcCol-neighbor; if imageSrcNeighborColmin<0 then imageSrcNeighborColmin:=0;
      imageSrcNeighborRowmax:=imageSrcRow+neighbor; if imageSrcNeighborRowmax>imageSrcRowmax then imageSrcNeighborRowmax:=imageSrcRowmax;
      imageSrcNeighborColmax:=imageSrcCol+neighbor; if imageSrcNeighborColmax>imageSrcColmax then imageSrcNeighborColmax:=imageSrcColmax;
      // by row
      for imageSrcNeighborRow:=imageSrcNeighborRowmin to imageSrcNeighborRowmax do begin
        tIntensityPreviouse:=-1;
        for imageSrcNeighborCol:=imageSrcNeighborColmin to imageSrcNeighborColmax do begin
          value:=pSrc[imageSrcNeighborRow*w+imageSrcNeighborCol];
          tIntensity:=Intensity(value);
          if tIntensityPreviouse<>-1 then begin
            diff:=tIntensity-tIntensityPreviouse;
            if diff<0 then diff:=-diff;
            diff:=diff div sensibility;
            Inc(tContraste,diff);
          end;
          tIntensityPreviouse:=tIntensity;
        end;
      end;
      // by col
      for imageSrcNeighborCol:=imageSrcNeighborColmin to imageSrcNeighborColmax do begin
        tIntensityPreviouse:=-1;
        for imageSrcNeighborRow:=imageSrcNeighborRowmin to imageSrcNeighborRowmax do begin
          value:=pSrc[imageSrcNeighborRow*w+imageSrcNeighborCol];
          tIntensity:=Intensity(value);
          if tIntensityPreviouse<>-1 then begin
            diff:=tIntensity-tIntensityPreviouse;
            if diff<0 then diff:=-diff;
            diff:=diff div sensibility;
            Inc(tContraste,diff);
          end;
          tIntensityPreviouse:=tIntensity;
        end;
      end;
      if tContraste>255 then tContraste:=255;
      // set gray value with Color32 for optimization
      pDest^[0]:= Color32(tContraste,tContraste,tContraste);
      Inc(pDest);
    end;
  end;
end;

// We create a image containing all difference between each 2 cols of source image, by row.
// Example :
//   diff(row 10, col 100) = src(row 10, col 100) - src(row 10, col 99)
procedure TFilterGranularityExplorer.calculateDiffHorizontal();
var
  pSrc, PDest : PColor32Array;
  w,h :Integer;
  value : Integer;
  tIntensity, tIntensityPreviouse : Integer;
  diff : Integer;
  sensibility : Integer;
  imageSrcRow, imageSrcCol : Integer;
  imageSrcRowmax, imageSrcColmax : Integer;
begin
  h:=parameterImageIn.Image.Height;
  w:=parameterImageIn.Image.Width;
  sensibility:=parameterSensibility.Max-parameterSensibility.Value+1;
  pSrc:=parameterImageIn.Image.Bits;
  imageSrcRowmax:=h-1;
  imageSrcColmax:=w-1;
  for imageSrcRow:=0 to imageSrcRowmax do begin
    pDest:=imageDiffHorizontal.Bits;
    Inc(pDest,imageSrcRow*imageDiffHorizontal.Width);
    tIntensityPreviouse:=0;
    for imageSrcCol:=0 to imageSrcColmax do begin
      value:=pSrc[imageSrcRow*w+imageSrcCol];
      tIntensity:=Intensity(value);
      if imageSrcCol>0 then begin
        diff:=tIntensity-tIntensityPreviouse;
        if diff<0 then diff:=-diff;
        diff:=diff div sensibility;
        pDest^[0]:= image.Gray32(diff);
        Inc(pDest);
      end;
      tIntensityPreviouse:=tIntensity;
    end;
  end;
end;

// We create a image containing all difference between each 2 rows of source image, by colonne.
// Example :
//   diff(row 10, col 100) = src(row 10, col 100) - src(row 9, col 100)
procedure TFilterGranularityExplorer.calculateDiffVertical();
var
  pSrc, PDest : PColor32Array;
  w,h :Integer;
  value : Integer;
  tIntensity : Integer;
  tIntensityPreviouse : array of Integer;
  diff : Integer;
  sensibility : Integer;
  imageSrcRow, imageSrcCol : Integer;
  imageSrcRowmax, imageSrcColmax : Integer;
begin
  h:=parameterImageIn.Image.Height;
  w:=parameterImageIn.Image.Width;
  sensibility:=parameterSensibility.Max-parameterSensibility.Value+1;
  pSrc:=parameterImageIn.Image.Bits;
  imageSrcRowmax:=h-1;
  imageSrcColmax:=w-1;
  tIntensityPreviouse:=nil;
  SetLength(tIntensityPreviouse,parameterImageIn.Image.Width);
  for imageSrcRow:=0 to imageSrcRowmax do begin

    if imageSrcRow>0 then begin
      pDest:=imageDiffVertical.Bits;
      Inc(pDest,(imageSrcRow-1)*imageDiffVertical.Width);
    end;

    for imageSrcCol:=0 to imageSrcColmax do begin
      value:=pSrc[imageSrcRow*w+imageSrcCol];
      tIntensity:=Intensity(value);

      if imageSrcRow>0 then begin
        diff:=tIntensity-tIntensityPreviouse[imageSrcCol];
        if diff<0 then diff:=-diff;
        diff:=diff div sensibility;
        pDest^[0]:=image.Gray32(diff);
        Inc(pDest);
      end;
      // we save the intensity of this colonne (used in the next row)
      tIntensityPreviouse[imageSrcCol]:=tIntensity;

    end;
  end;
end;

end.

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