-
Notifications
You must be signed in to change notification settings - Fork 29
/
Quick.ImageFX.OpenCV.pas
1478 lines (1324 loc) · 39 KB
/
Quick.ImageFX.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{ ***************************************************************************
Copyright (c) 2016-2018 Kike P�rez
Unit : Quick.ImageFX.OpenCV
Description : Image manipulation with OpenCV
Author : Kike P�rez
Version : 4.0
Created : 10/04/2016
Modified : 11/08/2018
This file is part of QuickImageFX: https://github.com/exilon/QuickImageFX
Third-party libraries used:
Delphi-OpenCV from Laex (https://github.com/Laex/Delphi-OpenCV)
CCR-Exif from Chris Rolliston (https://code.google.com/archive/p/ccr-exif)
***************************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*************************************************************************** }
unit Quick.ImageFX.OpenCV;
interface
uses
{$IFDEF MSWINDOWS}
Winapi.Windows,
Vcl.ImgList,
Vcl.Controls,
System.SysUtils,
Winapi.ShellAPI,
Vcl.Graphics,
Vcl.Imaging.pngimage,
Vcl.Imaging.jpeg,
Vcl.Imaging.GIFImg,
{$ENDIF}
System.UITypes,
System.Classes,
System.Math,
ocv.utils,
ocv.highgui_c,
ocv.core_c,
ocv.core.types_c,
ocv.imgproc_c,
ocv.imgproc.types_c,
CCR.Exif,
Quick.ImageFX,
Quick.ImageFX.Types,
Quick.Base64;
const
MaxPixelCountA = MaxInt Div SizeOf (TRGBQuad);
cmRGB: TA4CVChar = 'RGB'#0;
cmBGR: TA4CVChar = 'BGR'#0;
cmGRAY: TA4CVChar = 'GRAY';
type
TOCVParam = array of Integer;
POCVParam = ^TOCVParam;
TOCVParams = array[0..5] of TOCVParam;
TImageFXOpenCV = class(TImageFX,IImageFX)
private
fOCVImage : pIplImage;
procedure DoScanlines(ScanlineMode : TScanlineMode);
function ResizeImage(w, h : Integer; ResizeOptions : TResizeOptions) : IImageFX;
function AsPAnsiChar(const str : string): PAnsiChar;
procedure SaveToStreamWithoutCompression(stream : TStream; imgFormat : TImageFormat = ifJPG);
protected
function GetPixel(const x, y: Integer): TPixelInfo;
procedure SetPixel(const x, y: Integer; const P: TPixelInfo);
public
property Pixel[const x, y: Integer]: TPixelInfo read GetPixel write SetPixel;
constructor Create; overload; override;
constructor Create(const fromfile : string); overload; override;
destructor Destroy; override;
function NewBitmap(w, h : Integer) : IImageFX; overload;
function NewBitmap(w, h: Integer; Depth : Integer; nChannels : Integer): IImageFX; overload;
function LoadFromFile(const fromfile : string; CheckIfFileExists : Boolean = False) : IImageFX;
function LoadFromFile2(const fromfile : string; CheckIfFileExists : Boolean = False) : IImageFX;
function LoadFromStream(stream : TStream) : IImageFX;
function LoadFromString(const str : string) : IImageFX;
function LoadFromImageList(imgList : TImageList; ImageIndex : Integer) : IImageFX;
function LoadFromIcon(Icon : TIcon) : IImageFX;
function LoadFromFileIcon(const FileName : string; IconIndex : Word) : IImageFX;
function LoadFromFileExtension(const aFilename : string; LargeIcon : Boolean) : IImageFX;
function LoadFromResource(const ResourceName : string) : IImageFX;
function LoadFromImage(image : pIplImage) : IImageFX;
procedure GetResolution(var x,y : Integer); overload;
function AspectRatio : Double;
function GetPixelImage(const x, y: Integer; image : pIplImage): TPixelInfo;
procedure SetPixelImage(const x, y: Integer; const P: TPixelInfo; image : pIplImage);
function IsEmpty : Boolean;
function IsGray : Boolean;
procedure Assign(Graphic : TGraphic);
function Clear(pcolor : TColor = clNone) : IImageFX;
function Resize(w, h : Integer) : IImageFX; overload;
function Resize(w, h : Integer; ResizeMode : TResizeMode; ResizeFlags : TResizeFlags = []; ResampleMode : TResamplerMode = rsLinear) : IImageFX; overload;
function Draw(Graphic : TGraphic; x, y : Integer; alpha : Double = 1) : IImageFX; overload;
function Draw(stream : TStream; x, y : Integer; alpha : Double = 1) : IImageFX; overload;
function Draw(overlay : pIplImage; x, y : Integer; alpha : Double = 1) : IImageFX; overload;
function DrawCentered(Graphic : TGraphic; alpha : Double = 1) : IImageFX; overload;
function DrawCentered(stream: TStream; alpha : Double = 1) : IImageFX; overload;
function Rotate90 : IImageFX;
function Rotate180 : IImageFX;
function Rotate270 : IImageFX;
function RotateBy(RoundAngle : Integer) : IImageFX;
function RotateAngle(RotAngle : Single) : IImageFX;
function FlipX : IImageFX;
function FlipY : IImageFX;
function GrayScale : IImageFX;
function ScanlineH : IImageFX;
function ScanlineV : IImageFX;
function Lighten(StrenghtPercent : Integer = 30) : IImageFX;
function Darken(StrenghtPercent : Integer = 30) : IImageFX;
function Tint(mColor : TColor) : IImageFX;
function TintAdd(R, G , B : Integer) : IImageFX;
function TintBlue : IImageFX;
function TintRed : IImageFX;
function TintGreen : IImageFX;
function Solarize : IImageFX;
function Rounded(RoundLevel : Integer = 27) : IImageFX;
function AntiAliasing : IImageFX;
function SetAlpha(Alpha : Byte) : IImageFX;
procedure SaveToPNG(const outfile : string); override;
procedure SaveToJPG(const outfile : string); override;
procedure SaveToBMP(const outfile : string); override;
procedure SaveToGIF(const outfile : string); override;
function AsImage : pIplImage;
function AsBitmap : TBitmap;
function AsPNG : TPngImage;
function AsJPG : TJpegImage;
function AsGIF : TGifImage;
function AsString(imgFormat : TImageFormat = ifJPG) : string;
function CloneImage : pIplImage;
function Clone : IImageFX;
procedure SaveToStream(stream : TStream; imgFormat : TImageFormat = ifJPG);
class function ColorToScalar(PColor : TColor = clWhite) : TCvScalar;
end;
implementation
constructor TImageFXOpenCV.Create;
begin
inherited Create;
fOCVImage := nil;
end;
constructor TImageFXOpenCV.Create(const fromfile: string);
begin
Create;
LoadFromFile(fromfile);
end;
destructor TImageFXOpenCV.Destroy;
begin
if Assigned(fOCVImage) then cvReleaseImage(fOCVImage);
inherited;
end;
{function TImageFX.LoadFromFile(fromfile: string) : TImageFX;
var
fs: TFileStream;
FirstBytes: AnsiString;
Graphic: TGraphic;
bmp : TBitmap;
begin
Result := Self;
if not FileExists(fromfile) then
begin
fLastResult := arFileNotExist;
Exit;
end;
Graphic := nil;
fs := TFileStream.Create(fromfile, fmOpenRead);
try
SetLength(FirstBytes, 8);
fs.Read(FirstBytes[1], 8);
if Copy(FirstBytes, 1, 2) = 'BM' then
begin
Graphic := TBitmap.Create;
end else
if FirstBytes = #137'PNG'#13#10#26#10 then
begin
Graphic := TPngImage.Create;
end else
if Copy(FirstBytes, 1, 3) = 'GIF' then
begin
Graphic := TGIFImage.Create;
end else
if Copy(FirstBytes, 1, 2) = #$FF#$D8 then
begin
Graphic := TJPEGImage.Create;
end;
if Assigned(Graphic) then
begin
try
fs.Seek(0, soFromBeginning);
Graphic.LoadFromStream(fs);
bmp := TBitmap.Create;
try
InitBitmap(bmp);
bmp.Assign(Graphic);
fBitmap.Assign(bmp);
fLastResult := arOk;
finally
bmp.Free;
end;
except
end;
Graphic.Free;
end;
finally
fs.Free;
end;
end;}
function TImageFXOpenCV.LoadFromFile(const fromfile: string; CheckIfFileExists : Boolean = False) : IImageFX;
var
fs : TFileStream;
begin
Result := Self;
if (CheckIfFileExists) and (not FileExists(fromfile)) then
begin
LastResult := arFileNotExist;
raise Exception.Create(Format('File "%s" not found',[fromfile]));
end;
if fOCVImage <> nil then cvReleaseImage(fOCVImage);
//loads file into OCVImage
fs := TFileStream.Create(fromfile,fmShareDenyWrite);
try
Self.LoadFromStream(fs);
finally
fs.Free;
end;
//fOCVImage := cvLoadImage(AsPAnsiChar(fromfile),CV_LOAD_IMAGE_UNCHANGED);
//cvShowImage('Loaded Image',fOCVImage);
if fOCVImage = nil then
begin
LastResult := arFileNotExist;
raise Exception.Create(Format('Error loading image "%s"',[fromfile]));
end
else LastResult := arOk;
end;
function TImageFXOpenCV.LoadFromFile2(const fromfile: string; CheckIfFileExists : Boolean = False) : IImageFX;
begin
Result := Self;
if (CheckIfFileExists) and (not FileExists(fromfile)) then
begin
LastResult := arFileNotExist;
raise Exception.Create(Format('File "%s" not found',[fromfile]));
end;
if fOCVImage <> nil then cvReleaseImage(fOCVImage);
//loads file into OCVImage
fOCVImage := cvLoadImage(AsPAnsiChar(fromfile),CV_LOAD_IMAGE_UNCHANGED);
//cvShowImage('Loaded Image',fOCVImage);
if fOCVImage = nil then
begin
LastResult := arFileNotExist;
raise Exception.Create(Format('Error loading image "%s"',[fromfile]));
end
else LastResult := arOk;
end;
function TImageFXOpenCV.LoadFromStream(stream: TStream) : IImageFX;
var
Buffer : TBytes;
Size : Int64;
mat : pCvMat;
ExifData : TExifData;
begin
Result := Self;
ExifData := nil;
if (not Assigned(stream)) or (stream.Size = 0) then
begin
LastResult := arZeroBytes;
raise Exception.Create('Stream is empty!');
end;
if fOCVImage <> nil then cvReleaseImage(fOCVImage);
stream.Seek(0,soBeginning);
Size := stream.Size;
SetLength(Buffer, Size);
stream.ReadBuffer(Pointer(Buffer)^,Size);
mat := cvCreateMat(1,Size,CV_64F);
//fallo
mat.data.ptr := PByte(Buffer);
try
fOCVImage := cvDecodeImage(mat,CV_LOAD_IMAGE_UNCHANGED);
if fOCVImage = nil then raise EInvalidGraphic.Create('Unknow Graphic format')
finally
cvReleaseMat(mat);
end;
if ExifRotation then ProcessExifRotation(stream);
LastResult := arOk;
end;
function TImageFXOpenCV.LoadFromString(const str: string) : IImageFX;
var
stream : TStringStream;
begin
Result := Self;
if str = '' then
begin
LastResult := arZeroBytes;
Exit;
end;
stream := TStringStream.Create(Base64Decode(str));
try
LoadFromStream(stream);
LastResult := arOk;
finally
stream.Free;
end;
end;
function TImageFXOpenCV.LoadFromFileIcon(const FileName : string; IconIndex : Word) : IImageFX;
var
Icon : TIcon;
begin
Result := Self;
Icon := TIcon.Create;
try
Icon.Handle := ExtractAssociatedIcon(hInstance,pChar(FileName),IconIndex);
Icon.Transparent := True;
//fBitmap.Assign(Icon);
finally
Icon.Free;
end;
end;
function TImageFXOpenCV.LoadFromResource(const ResourceName : string) : IImageFX;
var
icon : TIcon;
//GPBitmap : TGPBitmap;
begin
Result := Self;
icon:=TIcon.Create;
try
icon.LoadFromResourceName(HInstance,ResourceName);
icon.Transparent:=True;
Self.LoadFromIcon(icon);
finally
icon.Free;
end;
end;
function TImageFXOpenCV.LoadFromImageList(imgList : TImageList; ImageIndex : Integer) : IImageFX;
var
icon : TIcon;
begin
Result := Self;
icon := TIcon.Create;
try
imgList.GetIcon(ImageIndex,icon);
icon.Transparent := True;
Self.LoadFromIcon(icon);
LastResult := arOk;
finally
icon.Free;
end;
end;
function TImageFXOpenCV.LoadFromIcon(Icon : TIcon) : IImageFX;
var
ms : TMemoryStream;
bmp : TBitmap;
begin
Result := Self;
ms := TMemoryStream.Create;
try
bmp := TBitmap.Create;
try
InitBitmap(bmp);
bmp.Assign(Icon);
bmp.SaveToStream(ms);
finally
bmp.Free;
end;
Self.LoadFromStream(ms);
finally
ms.Free;
end;
end;
function TImageFXOpenCV.LoadFromFileExtension(const aFilename : string; LargeIcon : Boolean) : IImageFX;
var
icon : TIcon;
aInfo : TSHFileInfo;
begin
Result := Self;
LastResult := arUnknowFmtType;
if GetFileInfo(ExtractFileExt(aFilename),aInfo,LargeIcon) then
begin
icon := TIcon.Create;
try
Icon.Handle := AInfo.hIcon;
Icon.Transparent := True;
Self.LoadFromIcon(Icon);
LastResult := arOk;
finally
icon.Free;
DestroyIcon(aInfo.hIcon);
end;
end;
end;
function TImageFXOpenCV.LoadFromImage(image : pIplImage) : IImageFX;
begin
Result := Self;
fOCVImage := image;
end;
function TImageFXOpenCV.AspectRatio : Double;
begin
if fOCVImage <> nil then Result := fOCVImage.width / fOCVImage.Height
else Result := 0;
end;
procedure TImageFXOpenCV.GetResolution(var x,y : Integer);
begin
if Assigned(fOCVImage) then
begin
x := fOCVImage^.Width;
y := fOCVImage^.Height;
LastResult := arOk;
end
else
begin
x := -1;
y := -1;
LastResult := arCorruptedData;
end;
end;
function TImageFXOpenCV.IsEmpty: Boolean;
begin
Result := fOCVImage = nil;
end;
function TImageFXOpenCV.IsGray: Boolean;
begin
if Assigned(fOCVImage) then Result := fOCVImage^.nChannels = 1
else Result := False;
end;
function TImageFXOpenCV.Clear(pcolor : TColor = clNone) : IImageFX;
begin
Result := Self;
cvRectangle(fOCVImage,cvPoint(0,0),cvPoint(fOCVImage^.width,fOCVImage^.height),ColorToScalar(pcolor),CV_FILLED);
LastResult := arOk;
end;
function TImageFXOpenCV.ResizeImage(w, h : Integer; ResizeOptions : TResizeOptions) : IImageFX;
var
ImgAux : pIplImage;
srcRect,
tgtRect : TRect;
crop : Integer;
srcRatio : Double;
nw, nh : Integer;
Resampler : Integer;
begin
Result := Self;
if (not Assigned(fOCVImage)) or ((fOCVImage.Width * fOCVImage.Height) = 0) then
begin
LastResult := arZeroBytes;
Exit;
end;
//if any value is 0, calculates proportionaly
if (w * h) = 0 then
begin
//scales max w or h
if ResizeOptions.ResizeMode = rmScale then
begin
if (h = 0) and (fOCVImage.Height > fOCVImage.Width) then
begin
h := w;
w := 0;
end;
end
else ResizeOptions.ResizeMode := rmFitToBounds;
begin
if w > h then
begin
nh := (w * fOCVImage.Height) div fOCVImage.Width;
h := nh;
end
else
begin
nw := (h * fOCVImage.Width) div fOCVImage.Height;
w := nw;
end;
end;
end;
case ResizeOptions.ResizeMode of
rmScale: //recalculate width or height target size to preserve original aspect ratio
begin
if fOCVImage.Width > fOCVImage.Height then
begin
nh := (w * fOCVImage.Height) div fOCVImage.Width;
h := nh;
nw := w;
end
else
begin
nw := (h * fOCVImage.Width) div fOCVImage.Height;
w := nw;
nh := h;
end;
srcRect := Rect(0,0,fOCVImage.Width,fOCVImage.Height);
end;
rmCropToFill: //preserve target aspect ratio cropping original image to fill whole size
begin
nw := w;
nh := h;
crop := Round(h / w * fOCVImage.Width);
if crop < fOCVImage.Height then
begin
//target image is wider, so crop top and bottom
srcRect.Left := 0;
srcRect.Top := (fOCVImage.Height - crop) div 2;
srcRect.Width := fOCVImage.Width;
srcRect.Height := crop;
end
else
begin
//target image is narrower, so crop left and right
crop := Round(w / h * fOCVImage.Height);
srcRect.Left := (fOCVImage.Width - crop) div 2;
srcRect.Top := 0;
srcRect.Width := crop;
srcRect.Height := fOCVImage.Height;
end;
end;
rmFitToBounds: //resize image to fit max bounds of target size
begin
srcRatio := fOCVImage.Width / fOCVImage.Height;
if fOCVImage.Width > fOCVImage.Height then
begin
nw := w;
nh := Round(w / srcRatio);
if nh > h then //too big
begin
nh := h;
nw := Round(h * srcRatio);
end;
end
else
begin
nh := h;
nw := Round(h * srcRatio);
if nw > w then //too big
begin
nw := w;
nh := Round(w / srcRatio);
end;
end;
srcRect := Rect(0,0,fOCVImage.Width,fOCVImage.Height);
end;
else
begin
nw := w;
nh := h;
srcRect := Rect(0,0,fOCVImage.Width,fOCVImage.Height);
end;
end;
//if image is smaller no upsizes
if ResizeOptions.NoMagnify then
begin
if (fOCVImage.Width < nw) or (fOCVImage.Height < nh) then
begin
//if FitToBounds then preserve original size
if ResizeOptions.ResizeMode = rmFitToBounds then
begin
nw := fOCVImage.Width;
nh := fOCVImage.Height;
end
else
begin
//all cases no resizes, but CropToFill needs to grow to fill target size
if ResizeOptions.ResizeMode <> rmCropToFill then
begin
if not ResizeOptions.SkipSmaller then
begin
LastResult := arAlreadyOptim;
nw := srcRect.Width;
nh := srcRect.Height;
w := nw;
h := nh;
end
else Exit;
end;
end;
end;
end;
if ResizeOptions.Center then
begin
tgtRect.Top := (h - nh) div 2;
tgtRect.Left := (w - nw) div 2;
tgtRect.Width := nw;
tgtRect.Height := nh;
end
else tgtRect := Rect(0,0,nw,nh);
//selects resampler algorithm
case ResizeOptions.ResamplerMode of
rsAuto :
begin
if (w < fOCVImage.Width ) or (h < fOCVImage.Height) then Resampler := CV_INTER_AREA
else Resampler := CV_INTER_LINEAR;
end;
rsNearest : Resampler := CV_INTER_NN;
rsOCVArea : Resampler := CV_INTER_AREA;
rsLinear : Resampler := CV_INTER_LINEAR;
rsOCVCubic : Resampler := CV_INTER_CUBIC;
rsOCVLanczos4 : Resampler := CV_INTER_LANCZOS4;
else Resampler := CV_INTER_LINEAR;
end;
ImgAux := cvCreateImage(CvSize(w,h),fOCVImage^.depth,fOCVImage^.nChannels);
//fills outside zone borders with a color
if (ResizeOptions.FillBorders) and ((tgtRect.Width < w) or (tgtRect.Height < h)) then
begin
cvRectangle(ImgAux,cvPoint(0,0),cvPoint(w,h),ColorToScalar(ResizeOptions.BorderColor),CV_FILLED);
end;
//set source and target regions
cvSetImageROI(fOCVImage, CvRect(srcRect.Left,srcRect.Top,srcRect.Width,srcRect.Height));
cvSetImageROI(ImgAux, CvRect(tgtRect.Left,tgtRect.Top,tgtRect.Width,tgtRect.Height));
//resize
try
cvResize(fOCVImage,ImgAux,Resampler);
cvResetImageROI(fOCVImage);
cvResetImageROI(ImgAux);
cvReleaseImage(fOCVImage);
fOCVImage := ImgAux;
LastResult := arOk;
except
LastResult := arCorruptedData;
if ImgAux <> nil then cvReleaseImage(ImgAux);
end;
end;
function TImageFXOpenCV.Resize(w, h : Integer) : IImageFX;
begin
Result := ResizeImage(w,h,ResizeOptions);
end;
function TImageFXOpenCV.Resize(w, h : Integer; ResizeMode : TResizeMode; ResizeFlags : TResizeFlags = []; ResampleMode : TResamplerMode = rsLinear) : IImageFX;
var
lResizeOptions : TResizeOptions;
begin
lResizeOptions := TResizeOptions.Create;
try
if (rfNoMagnify in ResizeFlags) then lResizeOptions.NoMagnify := True else lResizeOptions.NoMagnify := False;
if (rfCenter in ResizeFlags) then lResizeOptions.Center := True else lResizeOptions.Center := False;
if (rfFillBorders in ResizeFlags) then lResizeOptions.FillBorders := True else lResizeOptions.FillBorders := False;
Result := ResizeImage(w,h,lResizeOptions);
finally
lResizeOptions.Free;
end;
end;
procedure TImageFXOpenCV.Assign(Graphic : TGraphic);
var
ms : TMemoryStream;
begin
ms := TMemoryStream.Create;
try
LoadFromStream(ms);
finally
ms.Free;
end;
end;
function TImageFXOpenCV.DrawCentered(Graphic : TGraphic; alpha : Double = 1) : IImageFX;
begin
Result := Draw(Graphic,(fOCVImage.Width - Graphic.Width) div 2, (fOCVImage.Height - Graphic.Height) div 2,alpha);
end;
function TImageFXOpenCV.DrawCentered(stream: TStream; alpha : Double = 1) : IImageFX;
begin
Result := Draw(stream,-1,-1,alpha);
end;
function TImageFXOpenCV.Draw(Graphic : TGraphic; x: Integer; y: Integer; alpha: Double = 1) : IImageFX;
var
stream : TStream;
begin
stream := TStringStream.Create;
try
Graphic.SaveToStream(stream);
Result := Draw(stream,x,y,alpha);
finally
stream.Free;
end;
end;
function TImageFXOpenCV.Draw(stream: TStream; x: Integer; y: Integer; alpha: Double = 1) : IImageFX;
var
overlay : pIplImage;
mat : pCvMat;
Buffer : TBytes;
Size : Int64;
begin
//get overlay image
stream.Seek(0,soBeginning);
Size := stream.Size;
SetLength(Buffer, Size);
stream.ReadBuffer(Pointer(Buffer)^,Size);
mat := cvCreateMat(1,Size,CV_64F);
try
//fallo
mat.data.ptr := PByte(Buffer);
overlay := cvDecodeImage(mat,CV_LOAD_IMAGE_UNCHANGED);
finally
cvReleaseMat(mat);
end;
SetLength(Buffer,0);
//needs x or y center image
if x = -1 then x := (fOCVImage.Width - overlay.Width) div 2;
if y = -1 then y := (fOCVImage.Height - overlay.Height) div 2;
Result := Draw(overlay,x,y,alpha);
end;
function TImageFXOpenCV.Draw(overlay : pIplImage; x, y : Integer; alpha : Double = 1) : IImageFX;
var
i, j, k: Integer;
opacity: Double;
overlayPx, srcPx: Byte;
overlayRect : TCvRect;
srcHasAlpha : Boolean;
begin
Result := Self;
//overlay := cvLoadImage('D:\logo2.png',CV_LOAD_IMAGE_UNCHANGED);
try
//if overlay is bigger than underlay, gets only part of the overlay
if (overlay^.width > fOCVImage^.width) or (overlay^.height > fOCVImage^.height) then
begin
overlayRect := cvRect(0,0,fOCVImage^.width,fOCVImage^.height);
if overlay^.width > fOCVImage^.width then
begin
x := 0;
overlayRect := cvRect(0,0,fOCVImage^.width,overlayRect.height);
end;
if overlay^.height > fOCVImage^.height then
begin
y := 0;
overlayRect := cvRect(0,0,overlayRect.width,fOCVImage^.height);
end;
cvSetImageROI(overlay,overlayRect);
end;
if fOCVImage^.nChannels > 3 then srcHasAlpha := True
else srcHasAlpha := False;
//overlay images
for i := 0 to overlay^.width - 1 do
begin
for j := 0 to overlay^.height - 1 do
begin
for k := 0 to fOCVImage^.nChannels - 1 do
begin
opacity := (overlay^.imageData[j * overlay^.widthStep + i * overlay^.nChannels + 3]) / 255;
opacity := opacity * alpha;
overlayPx := overlay^.imageData[j * overlay^.widthStep + i * overlay^.nChannels + k];
if srcHasAlpha then
begin
srcPx := fOCVImage^.imageData[(y + j) * fOCVImage^.widthStep + (i + x) * overlay^.nChannels + k];
fOCVImage^.imageData[(j + y) * fOCVImage^.widthStep + (i + x) * overlay^.nChannels + k] := Trunc(srcPx * (1 - opacity) + overlayPx * opacity);
end
else if opacity > 0 then
begin
srcPx := fOCVImage^.imageData[(y + j) * fOCVImage^.widthStep + (i + x) * fOCVImage^.nChannels + k];
fOCVImage^.imagedata[(j + y) * fOCVImage^.widthStep + (i + x) * fOCVImage^.nchannels + k] := Trunc(srcPx * (1 - opacity) + overlayPx * opacity);
end;
end;
end;
end;
finally
if overlay.roi <> nil then cvResetImageROI(overlay);
cvReleaseImage(overlay);
end;
end;
function TImageFXOpenCV.NewBitmap(w, h : Integer) : IImageFX;
begin
NewBitmap(w,h,IPL_DEPTH_8U,4);
end;
function TImageFXOpenCV.NewBitmap(w, h: Integer; Depth : Integer; nChannels : Integer): IImageFX;
begin
Result := Self;
if fOCVImage <> nil then cvReleaseImage(fOCVImage);
fOCVImage := cvCreateImage(CvSize(w,h),Depth,nChannels);
end;
function TImageFXOpenCV.Rotate90 : IImageFX;
begin
Result := Self;
LastResult := arRotateError;
Self.RotateBy(90);
LastResult := arOk;
end;
function TImageFXOpenCV.Rotate180 : IImageFX;
begin
Result := Self;
LastResult := arRotateError;
cvFlip(fOCVImage,fOCVImage,-1);
LastResult := arOk;
end;
function TImageFXOpenCV.Rotate270 : IImageFX;
begin
Result := Self;
LastResult := arRotateError;
Self.RotateBy(270);
LastResult := arOk;
end;
function TImageFXOpenCV.RotateBy(RoundAngle : Integer) : IImageFX;
var
center : TCvPoint2D32f;
mat : pCvMat;
ImgAux : pIplImage;
w : Single;
h : Single;
t : Single;
newWidth : Integer;
newHeight : Integer;
begin
Result := Self;
LastResult := arRotateError;
w := fOCVImage^.width;
h := fOCVImage^.height;
t := (RoundAngle * -1) * Pi / 180;
if RoundAngle > 90 then
begin
center.x := w / 2;
center.y := center.x;
end
else
begin
center.x := h / 2;
center.y := center.x;
end;
//calculate rotated bounding box
newWidth := Round(sin(t) * h + cos(t) * w);
newHeight := Round(sin(t) * w + cos(t) * h);
if newWidth < 0 then newWidth := newWidth * -1;
if newHeight < 0 then newHeight := newHeight * -1;
ImgAux := cvCreateImage(CvSize(newWidth,newHeight),IPL_DEPTH_8U,fOCVImage^.nChannels);
mat := cvCreateMat(2,3,CV_32FC1);
try
mat := cv2DRotationMatrix(center,RoundAngle * -1,1.0,mat);
cvWarpAffine(fOCVImage,ImgAux,mat,CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS,cvScalarAll(0));
cvReleaseImage(fOCVImage);
fOCVImage := ImgAux;
finally
cvReleaseMat(mat);
end;
LastResult := arOk;
end;
function TImageFXOpenCV.RotateAngle(RotAngle: Single) : IImageFX;
var
center : TCvPoint2D32f;
ImgAux : pIplImage;
mat : pCvMat;
w : Integer;
h : Integer;
t : single;
newWidth : Integer;
newHeight : Integer;
begin
Result := Self;
w := fOCVImage^.width;
h := fOCVImage^.height;
t := RotAngle * Pi / 180;
if RotAngle > 90 then
begin
center.x := w div 2;
center.y := center.x;
end
else
begin
center.x := h div 2;
center.y := center.x;
end;
//calculate rotated bounding box
newWidth := Round(sin(t) * h + cos(t) * w);
newHeight := Round(sin(t) * w + cos(t) * h);
if newWidth < 0 then newWidth := newWidth * -1;
if newHeight < 0 then newHeight := newHeight * -1;
ImgAux := cvCreateImage(CvSize(newWidth,newHeight),IPL_DEPTH_8U,fOCVImage^.nChannels);
mat := cvCreateMat(2,3,CV_32FC1);
try
LastResult := arRotateError;
mat := cv2DRotationMatrix(center,RotAngle * -1, 1.0,mat);
cvWarpAffine(fOCVImage,ImgAux,mat,CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS,cvScalarAll(0));
cvReleaseImage(fOCVImage);
fOCVImage := ImgAux;
finally
cvReleaseMat(mat);
end;
LastResult := arOk;
end;
function TImageFXOpenCV.FlipX : IImageFX;
begin
Result := Self;
LastResult := arRotateError;
cvFlip(fOCVImage,fOCVImage,1);
LastResult := arOk;
end;
function TImageFXOpenCV.FlipY : IImageFX;
begin
Result := Self;
LastResult := arRotateError;
cvFlip(fOCVImage,fOCVImage,0);
LastResult := arOk;
end;
function TImageFXOpenCV.GrayScale : IImageFX;
var
ImgAux : pIplImage;
begin
Result := Self;
LastResult := arColorizeError;
ImgAux := cvCreateImage(cvGetSize(fOCVImage), fOCVImage.depth, 1);
//cvCvtColor(fOCVImage,ImgAux,CV_BGRA2GRAY);
if fOCVImage.channelSeq = cmBGR then cvConvertImage(fOCVImage,ImgAux,CV_BGR2GRAY)
else cvConvertImage(fOCVImage,ImgAux,CV_BGRA2GRAY);
cvReleaseImage(fOCVImage);
fOCVImage := ImgAux;
LastResult := arOk;
end;
function TImageFXOpenCV.Lighten(StrenghtPercent : Integer = 30) : IImageFX;
begin