download filterHistogramContrast.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
unit filterHistogramContrast;
(* ***** 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, filterblur, image, SysUtils, Windows,
  filterHistogram, filterStandardDeviation;

type

  THistogramContrastAnalyseROI = class
  public
    diff : array[0..255] of Integer;
    diffMax, diffIntegralMax : Integer;
    diffIntegral, diffIntegralNormed : Integer;
    constructor Create;
    destructor Destroy; override;
    procedure setImage(aImage:PBitmap32);
    procedure setPrecision(aPrecision:Integer);
    procedure process(aROI:TRect);
  private
    filterHistogram1, filterHistogram2 : TFilterHistogram;
    histogramme1, histogramme2 : TParameterArraySingles;
    t : TParameterArrayIntegers ;
  end;

  TFilterHistogramContrast = class(TFilter)
  public
    constructor Create; override;
    destructor Destroy; override;
    procedure Run(); override;
    procedure setParameterImage( const aName: String; const aImage: PBitmap32); override;
    procedure setParameterInteger( const aName: String; const aValue:Int64); override;
  private
    analyseROI : THistogramContrastAnalyseROI;
    parameterImageIn : TParameterImage;
    outputParameterImageOuts : TParameterImages;
    imageOuts : ArrayOfPBitmap32;
    parameterMode : TParameterString;
    parameterGridSpacingWidth, parameterGridSpacingHeight : TParameterInteger;
    parameterPrecision : TParameterInteger;
    parameterRectangleWidth,parameterRectangleHeight : TParameterInteger;
    parameterBlurIteration : TParameterInteger;
    filterBlur : TFilterBlur ;
    imageIn : PBitmap32;
    imageBlurOut : PBitmap32;
    filterStandardDeviation : TFilterStandardDeviation;
    imageStandardDeviationOut : PBitmap32;
    procedure destroyImageOuts();
    procedure createImageOut0(aImageIn:PBitmap32);
    procedure runNormal;
    procedure runShowdetails;
    procedure _run();
  end;

implementation
uses
  imageIO, Math;

constructor TFilterHistogramContrast.Create;
begin
  inherited;
  parameterImageIn:=addParameterImage('inImage','explication...');
  outputParameterImageOuts:=addOutputParameterImages('outImages','0:normal/1:show_details');
  parameterMode:=addParameterString('mode','NORMAL/SHOW_DETAILS','NORMAL');
  parameterGridSpacingWidth:=addParameterInteger('gridSpacingWidth','',1,100,10);
  parameterGridSpacingHeight:=addParameterInteger('gridSpacingHeight','',1,100,10);
  parameterPrecision:=addParameterInteger('precision','',1,15,5);
  parameterRectangleWidth:=addParameterInteger('rectangleWidth','',2,100,20);
  parameterRectangleHeight:=addParameterInteger('rectangleHeight','',2,100,60);
  analyseROI:=THistogramContrastAnalyseROI.Create;
  parameterBlurIteration:=addParameterInteger('blurIteration','explication...',0,10,3);
  filterBlur:=TFilterBlur.Create ;
  filterStandardDeviation:=TFilterStandardDeviation.Create;
  SetLength(imageOuts,2);
end;

destructor TFilterHistogramContrast.Destroy;
begin
  inherited;
  destroyImageOuts();
  analyseROI.Free;
  image.freeImage(imageBlurOut);
  filterBlur.Free;
  filterStandardDeviation.Free;
end;

procedure TFilterHistogramContrast.destroyImageOuts();
var
  i : Integer;
begin
  for i:=Low(imageOuts) to High(imageOuts) do begin
    image.freeImage(imageOuts[i]);
  end;
end;

procedure TFilterHistogramContrast.setParameterImage( const aName: String; const aImage: PBitmap32);
begin
  inherited;
  if aName='inImage' then begin
    image.freeImage(imageBlurOut);
    imageBlurOut:=createImageFromImage(aImage);
    analyseROI.setImage(imageBlurOut);
    createImageOut0(aImage);
  end;
end;

procedure TFilterHistogramContrast.createImageOut0(aImageIn:PBitmap32);
var
  imageOutWidth, imageOutHeight : Integer;
begin
  if aImageIn<>nil then begin
    imageOutWidth:=aImageIn.Width div parameterGridSpacingWidth.Value;
    imageOutHeight:=aImageIn.Height div parameterGridSpacingHeight.Value;
    if (imageOuts[0]<>nil) then begin
      if (imageOuts[0].Width<>imageOutWidth) or
         (imageOuts[0].Height<>imageOutHeight) then begin
        image.freeImage(imageOuts[0]);
        imageOuts[0]:=createImage(imageOutWidth,imageOutHeight);
        setOutputParameterImages('outImages',imageOuts);
        image.freeImage(imageStandardDeviationOut);
        imageStandardDeviationOut:=createImageFromImage(imageOuts[0]);
      end;
    end else begin
      imageOuts[0]:=createImage(imageOutWidth,imageOutHeight);
      setOutputParameterImages('outImages',imageOuts);
      image.freeImage(imageStandardDeviationOut);
      imageStandardDeviationOut:=createImageFromImage(imageOuts[0]);
    end;
  end;
end;

procedure TFilterHistogramContrast.setParameterInteger( const aName: String; const aValue: Int64);
begin
  inherited;
  if aName='precision' then begin
    analyseROI.setPrecision(aValue);
  end else if (aName='gridSpacingWidth') or (aName='gridSpacingHeight') then begin
    createImageOut0(parameterImageIn.Image);
  end;
end;

procedure TFilterHistogramContrast.Run();
begin
  imageIn:=parameterImageIn.Image;
  if (imageIn<>nil) then begin
    _run();
  end;
end;

procedure TFilterHistogramContrast._run();
var
  i : Integer;
begin
  // blur
  if parameterBlurIteration.Value>0 then begin
    filterBlur.setParameterImage('inImage',imageIn);
    filterBlur.setParameterImage('outImage',imageBlurOut);
    i:=parameterBlurIteration.Value;
    while i>0 do begin
      filterBlur.Run;
      Dec(i);
      if imageIn<>parameterImageIn.Image then image.freeImage(imageIn);
      if i>0 then begin
        imageIn:=createImageFromImage(imageBlurOut);
        filterBlur.setParameterImage('inImage',imageIn);
      end;
    end;
  end;
  if parameterMode.Value='NORMAL' then begin
    runNormal;
    filterStandardDeviation.setParameterImage('inImage',imageOuts[0]);
    filterStandardDeviation.setParameterImage('outImage',imageStandardDeviationOut);
    filterStandardDeviation.Run;
    copyImageToImage(imageStandardDeviationOut,imageOuts[0]);
  end else begin
    runShowdetails;
  end;
end;

procedure TFilterHistogramContrast.runNormal;
var
  rectangleWidth, rectangleHeight : Integer;
  rectangleWidthDiv2, rectangleHeightDiv2 : Integer;
  roi : TRect;
  imageIn, imageOut : PBitmap32 ;
  w,h :Integer;
  trow, tcol : Integer;
  imageInRow, imageInCol : Integer;
  trowmax, tcolmax : Integer;
  value : Integer;
  gridSpacingWidth, gridSpacingHeight : Integer;
begin
  imageIn:=parameterImageIn.Image;
  eraseImage(imageOuts[0]);

  imageOut:=imageOuts[0];
  rectangleWidth:=parameterRectangleWidth.Value;
  rectangleHeight:=parameterRectangleHeight.Value;
  rectangleWidthDiv2:=rectangleWidth div 2;
  rectangleHeightDiv2:=rectangleHeight div 2;
  h:=imageOut.Height;
  w:=imageOut.Width;
  gridSpacingWidth:=parameterGridSpacingWidth.Value;
  gridSpacingHeight:=parameterGridSpacingHeight.Value;

  trowmax:=h-1;
  tcolmax:=w-1;
  for trow:=0 to trowmax do begin
    imageInRow:=trow * gridSpacingHeight;
    roi.Top:=imageInRow-rectangleHeightDiv2;
    roi.Bottom:=imageInRow+rectangleHeightDiv2;
    for tcol:=0 to tcolmax do begin
      imageInCol:=tcol * gridSpacingWidth;
      roi.Left:=imageInCol-rectangleWidthDiv2;
      roi.Right:=imageInCol+rectangleWidthDiv2;
      if (roi.Left>=0) and (roi.Right<=imageIn.Width) and (roi.Top>=0) and (roi.Bottom<=imageIn.Height) then begin
        // analyse
        analyseROI.process(roi);
        value:=analyseROI.diffIntegralNormed;
        // set gray value with Color32 for optimization
        image.setPixel(imageOut,tcol,trow,Color32(value,value,value));
        //image.setPixel(imageOut,tcol,trow,clRed32);
      end;
    end;
  end;

end;

procedure TFilterHistogramContrast.runShowdetails;
var
  i,j : Integer;
  imageOut : PBitmap32;
begin
  // analyse the current ROI
  analyseROI.process(RegionOfInterest);
  // create imageOut
  image.freeImage(imageOuts[1]);
  imageOuts[1]:=createImage(256,analyseROI.diffMax+1);
  setOutputParameterImages('outImages',imageOuts);
  imageOut:=outputParameterImageOuts.Images[1];
  // show diff
  for i:=0 to 255 do begin
    for j:=analyseROI.diff[i] downto 0 do begin
      image.setPixel(imageOut,i,analyseROI.diffMax-j,clRed32);
    end;
  end;
  // show diff integral
  for j:=0 to imageOut.Width-1 do begin
    image.setPixel(imageOut,j,imageOut.Height-analyseROI.diffIntegralNormed,clGreen32);
  end;
end;

constructor THistogramContrastAnalyseROI.Create;
begin
  inherited;
  filterHistogram1:=TFilterHistogram.Create;
  filterHistogram1.setParameterString('normalize','TRUE');
  histogramme1:=filterHistogram1.getOutputParameterArraySingles('histogram');
  filterHistogram2:=TFilterHistogram.Create;
  filterHistogram2.setParameterString('normalize','TRUE');
  histogramme2:=filterHistogram2.getOutputParameterArraySingles('histogram');
end;

destructor THistogramContrastAnalyseROI.Destroy;
begin
  inherited;
  filterHistogram1.Free;
  filterHistogram2.Free;
end;

procedure THistogramContrastAnalyseROI.setImage(aImage: PBitmap32);
begin
  filterHistogram1.setParameterImage('inImage',aImage);
  filterHistogram2.setParameterImage('inImage',aImage);
end;

procedure THistogramContrastAnalyseROI.setPrecision(aPrecision: Integer);
begin
  filterHistogram1.setParameterInteger('precision',aPrecision);
  filterHistogram2.setParameterInteger('precision',aPrecision);
end;

procedure THistogramContrastAnalyseROI.process(aROI:TRect);
var
  roiWidthDiv2 : Integer;
  roi : TRect;
  i : Integer;
begin
  roiWidthDiv2:=(aROI.Right-aROI.Left) div 2;

  roi.Left:=aROI.Left;
  roi.Right:=aROI.Left+roiWidthDiv2;
  roi.Top:=aROI.Top;
  roi.Bottom:=aROI.Bottom;
  filterHistogram1.setRegionOfInterest(roi);
  filterHistogram1.Run;

  roi.Left:=aROI.Left+roiWidthDiv2+1;
  roi.Right:=aROI.Right;
  roi.Top:=aROI.Top;
  roi.Bottom:=aROI.Bottom;
  filterHistogram2.setRegionOfInterest(roi);
  filterHistogram2.Run;

  // calcul diff
  FillChar(diff,SizeOf(diff),0);
  for i:=0 to 255 do begin
    diff[i]:=Abs(Round(histogramme2.Singles[i]-histogramme1.Singles[i]));
  end;

  // calcul des diff max
  //diffMax:=((roi.Right-roi.Left)*(roi.Bottom-roi.Top)) div 2;
  diffMax:=255;
  diffIntegralMax:=diffMax*20;

  // calcul diff integral
  diffIntegral:=0;
  for i:=0 to 255 do begin
    Inc(diffIntegral,diff[i]);
  end;
  diffIntegralNormed:=(diffIntegral * diffMax) div diffIntegralMax;
  if diffIntegralNormed>diffMax then diffIntegralNormed:=diffMax;

end;

end.

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