-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathmod.rs
1954 lines (1885 loc) · 61.8 KB
/
mod.rs
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
extern crate libc;
use self::libc::{c_char, c_double, c_float, c_int, c_uint};
use crate::array::Array;
use crate::defines::{AfError, BorderType, CannyThresholdType, ColorSpace, Connectivity};
use crate::defines::{DiffusionEq, FluxFn, InterpType, MomentType, YCCStd};
use crate::defines::{InverseDeconvAlgo, IterativeDeconvAlgo};
use crate::error::HANDLE_ERROR;
use crate::util::{AfArray, DimT, MutAfArray};
use crate::util::{ConfidenceCCInput, DeconvInput};
use crate::util::{EdgeComputable, GrayRGBConvertible, MomentsComputable, RealFloating};
use crate::util::{FloatingPoint, HasAfEnum, ImageFilterType, ImageNativeType, RealNumber};
use std::ffi::CString;
// unused functions from image.h header
// af_load_image_memory
// af_save_image_memory
// af_delete_image_memory
#[allow(dead_code)]
extern "C" {
fn af_cast(out: MutAfArray, arr: AfArray, aftype: c_uint) -> c_int;
fn af_gradient(dx: MutAfArray, dy: MutAfArray, arr: AfArray) -> c_int;
fn af_load_image(out: MutAfArray, filename: *const c_char, iscolor: c_int) -> c_int;
fn af_save_image(filename: *const c_char, input: AfArray) -> c_int;
fn af_load_image_native(out: MutAfArray, filename: *const c_char) -> c_int;
fn af_save_image_native(filename: *const c_char, input: AfArray) -> c_int;
fn af_resize(
out: MutAfArray,
input: AfArray,
odim0: DimT,
odim1: DimT,
method: c_uint,
) -> c_int;
fn af_transform(
out: MutAfArray,
input: AfArray,
trans: AfArray,
odim0: DimT,
odim1: DimT,
method: c_uint,
is_inverse: c_int,
) -> c_int;
fn af_rotate(
out: MutAfArray,
input: AfArray,
theta: c_float,
crop: c_int,
method: c_uint,
) -> c_int;
fn af_translate(
out: MutAfArray,
input: AfArray,
trans0: c_float,
trans1: c_float,
odim0: DimT,
odim1: DimT,
method: c_uint,
) -> c_int;
fn af_scale(
out: MutAfArray,
input: AfArray,
scale0: c_float,
scale1: c_float,
odim0: DimT,
odim1: DimT,
method: c_uint,
) -> c_int;
fn af_skew(
out: MutAfArray,
input: AfArray,
skew0: c_float,
skew1: c_float,
odim0: DimT,
odim1: DimT,
method: c_uint,
is_inverse: c_int,
) -> c_int;
fn af_histogram(
out: MutAfArray,
input: AfArray,
nbins: c_uint,
minval: c_double,
maxval: c_double,
) -> c_int;
fn af_dilate(out: MutAfArray, input: AfArray, mask: AfArray) -> c_int;
fn af_dilate3(out: MutAfArray, input: AfArray, mask: AfArray) -> c_int;
fn af_erode(out: MutAfArray, input: AfArray, mask: AfArray) -> c_int;
fn af_erode3(out: MutAfArray, input: AfArray, mask: AfArray) -> c_int;
fn af_regions(out: MutAfArray, input: AfArray, conn: c_uint, aftype: c_uint) -> c_int;
fn af_sobel_operator(dx: MutAfArray, dy: MutAfArray, i: AfArray, ksize: c_uint) -> c_int;
fn af_rgb2gray(out: MutAfArray, input: AfArray, r: c_float, g: c_float, b: c_float) -> c_int;
fn af_gray2rgb(out: MutAfArray, input: AfArray, r: c_float, g: c_float, b: c_float) -> c_int;
fn af_hist_equal(out: MutAfArray, input: AfArray, hist: AfArray) -> c_int;
fn af_hsv2rgb(out: MutAfArray, input: AfArray) -> c_int;
fn af_rgb2hsv(out: MutAfArray, input: AfArray) -> c_int;
fn af_bilateral(
out: MutAfArray,
input: AfArray,
sp_sig: c_float,
ch_sig: c_float,
iscolor: c_int,
) -> c_int;
fn af_mean_shift(
out: MutAfArray,
input: AfArray,
sp_sig: c_float,
ch_sig: c_float,
iter: c_uint,
iscolor: c_int,
) -> c_int;
fn af_medfilt(out: MutAfArray, input: AfArray, wlen: DimT, wwid: DimT, etype: c_uint) -> c_int;
fn af_medfilt1(out: MutAfArray, input: AfArray, wlen: DimT, etype: c_uint) -> c_int;
fn af_minfilt(out: MutAfArray, input: AfArray, wlen: DimT, wwid: DimT, etype: c_uint) -> c_int;
fn af_maxfilt(out: MutAfArray, input: AfArray, wlen: DimT, wwid: DimT, etype: c_uint) -> c_int;
fn af_gaussian_kernel(
out: MutAfArray,
rows: c_int,
cols: c_int,
sigma_r: c_double,
sigma_c: c_double,
) -> c_int;
fn af_color_space(out: MutAfArray, input: AfArray, tospace: c_uint, fromspace: c_uint)
-> c_int;
fn af_unwrap(
out: MutAfArray,
input: AfArray,
wx: DimT,
wy: DimT,
sx: DimT,
sy: DimT,
px: DimT,
py: DimT,
is_column: c_int,
) -> c_int;
fn af_wrap(
out: MutAfArray,
input: AfArray,
ox: DimT,
oy: DimT,
wx: DimT,
wy: DimT,
sx: DimT,
sy: DimT,
px: DimT,
py: DimT,
is_column: c_int,
) -> c_int;
fn af_sat(out: MutAfArray, input: AfArray) -> c_int;
fn af_ycbcr2rgb(out: MutAfArray, input: AfArray, stnd: c_int) -> c_int;
fn af_rgb2ycbcr(out: MutAfArray, input: AfArray, stnd: c_int) -> c_int;
fn af_is_image_io_available(out: *mut c_int) -> c_int;
fn af_transform_coordinates(out: MutAfArray, tf: AfArray, d0: c_float, d1: c_float) -> c_int;
fn af_moments(out: MutAfArray, input: AfArray, moment: c_int) -> c_int;
fn af_moments_all(out: *mut c_double, input: AfArray, moment: c_int) -> c_int;
fn af_canny(
out: MutAfArray,
input: AfArray,
thres_type: c_int,
low: c_float,
high: c_float,
swindow: c_uint,
is_fast: c_int,
) -> c_int;
fn af_anisotropic_diffusion(
out: MutAfArray,
input: AfArray,
dt: c_float,
K: c_float,
iters: c_uint,
fftype: c_int,
diff_kind: c_int,
) -> c_int;
fn af_confidence_cc(
out: MutAfArray,
input: AfArray,
seedx: AfArray,
seedy: AfArray,
radius: c_uint,
multiplier: c_uint,
iterations: c_int,
seg_val: c_double,
) -> c_int;
fn af_iterative_deconv(
out: MutAfArray,
input: AfArray,
ker: AfArray,
iterations: c_uint,
rfactor: c_float,
algo: c_int,
) -> c_int;
fn af_inverse_deconv(
out: MutAfArray,
input: AfArray,
ker: AfArray,
gamma: c_float,
algo: c_int,
) -> c_int;
}
/// Calculate the gradients
///
/// The gradients along the first and second dimensions are calculated simultaneously.
///
/// # Parameters
///
/// - `input` is the input Array
///
/// # Return Values
///
/// A tuple of Arrays.
///
/// The first Array is `dx` which is the gradient along the 1st dimension.
///
/// The second Array is `dy` which is the gradient along the 2nd dimension.
#[allow(unused_mut)]
pub fn gradient<T>(input: &Array<T>) -> (Array<T>, Array<T>)
where
T: HasAfEnum + FloatingPoint,
{
let mut dx: i64 = 0;
let mut dy: i64 = 0;
unsafe {
let err_val = af_gradient(
&mut dx as MutAfArray,
&mut dy as MutAfArray,
input.get() as AfArray,
);
HANDLE_ERROR(AfError::from(err_val));
}
(dx.into(), dy.into())
}
/// Load Image into Array
///
/// Only, Images with 8/16/32 bits per channel can be loaded using this function.
///
/// # Parameters
///
/// - `filename` is aboslute path of the image to be loaded.
/// - `is_color` indicates if the image file at given path is color or gray scale.
///
/// # Return Arrays
///
/// An Array with pixel values loaded from the image
#[allow(unused_mut)]
#[allow(clippy::match_wild_err_arm)]
pub fn load_image<T>(filename: String, is_color: bool) -> Array<T>
where
T: HasAfEnum + RealNumber,
{
let cstr_param = match CString::new(filename) {
Ok(cstr) => cstr,
Err(_) => panic!("CString creation from input filename failed"),
};
let trgt_type = T::get_af_dtype();
let mut img: i64 = 0;
unsafe {
let mut temp: i64 = 0;
let err1 = af_load_image(
&mut temp as MutAfArray,
cstr_param.as_ptr(),
is_color as c_int,
);
HANDLE_ERROR(AfError::from(err1));
let err2 = af_cast(&mut img as MutAfArray, temp as AfArray, trgt_type as c_uint);
HANDLE_ERROR(AfError::from(err2));
}
img.into()
}
/// Load Image into Array in it's native type
///
/// This load image function allows you to load images as U8, U16 or F32
/// depending on the type of input image as shown by the table below.
///
/// Bits per Color (Gray/RGB/RGBA Bits Per Pixel) | Array Type | Range
/// -----------------------------------------------|-------------|---------------
/// 8 ( 8/24/32 BPP) | u8 | 0 - 255
/// 16 (16/48/64 BPP) | u16 | 0 - 65535
/// 32 (32/96/128 BPP) | f32 | 0 - 1
///
/// # Parameters
///
/// - `filename` is name of file to be loaded
///
/// # Return Arrays
///
/// An Array with pixel values loaded from the image
#[allow(unused_mut)]
#[allow(clippy::match_wild_err_arm)]
pub fn load_image_native<T>(filename: String) -> Array<T>
where
T: HasAfEnum + ImageNativeType,
{
let cstr_param = match CString::new(filename) {
Ok(cstr) => cstr,
Err(_) => panic!("CString creation from input filename failed"),
};
let trgt_type = T::get_af_dtype();
let mut img: i64 = 0;
unsafe {
let mut temp: i64 = 0;
let err1 = af_load_image_native(&mut temp as MutAfArray, cstr_param.as_ptr());
HANDLE_ERROR(AfError::from(err1));
let err2 = af_cast(&mut img as MutAfArray, temp as AfArray, trgt_type as c_uint);
HANDLE_ERROR(AfError::from(err2));
}
img.into()
}
/// Save an Array to an image file
///
/// # Parameters
///
/// - `filename` is the abolute path(includes filename) at which input Array is going to be saved
/// - `input` is the Array to be stored into the image file
#[allow(unused_mut)]
#[allow(clippy::match_wild_err_arm)]
pub fn save_image<T>(filename: String, input: &Array<T>)
where
T: HasAfEnum + RealNumber,
{
let cstr_param = match CString::new(filename) {
Ok(cstr) => cstr,
Err(_) => panic!("CString creation from input filename failed"),
};
unsafe {
let err_val = af_save_image(cstr_param.as_ptr(), input.get() as AfArray);
HANDLE_ERROR(AfError::from(err_val));
}
}
/// Save an Array without modifications to an image file
///
/// This function only accepts U8, U16, F32 arrays. These arrays are saved to images without any modifications. You must also note that note all image type support 16 or 32 bit images. The best options for 16 bit images are PNG, PPM and TIFF. The best option for 32 bit images is TIFF. These allow lossless storage.
///
/// The images stored have the following properties:
///
/// Array Type | Bits per Color (Gray/RGB/RGBA Bits Per Pixel) | Range
/// -------------|-----------------------------------------------|---------------
/// U8 | 8 ( 8/24/32 BPP) | 0 - 255
/// U16 | 16 (16/48/64 BPP) | 0 - 65535
/// F32 | 32 (32/96/128 BPP) | 0 - 1
///
/// # Parameters
///
/// - `filename` is name of file to be saved
/// - `input` is the Array to be saved. Should be U8 for saving 8-bit image, U16 for 16-bit image, and F32 for 32-bit image.
#[allow(unused_mut)]
#[allow(clippy::match_wild_err_arm)]
pub fn save_image_native<T>(filename: String, input: &Array<T>)
where
T: HasAfEnum + ImageNativeType,
{
let cstr_param = match CString::new(filename) {
Ok(cstr) => cstr,
Err(_) => panic!("CString creation from input filename failed"),
};
unsafe {
let err_val = af_save_image_native(cstr_param.as_ptr(), input.get() as AfArray);
HANDLE_ERROR(AfError::from(err_val));
}
}
/// Resize an Image
///
/// Resizing an input image can be done using either NEAREST or BILINEAR interpolations.
/// Nearest interpolation will pick the nearest value to the location, whereas bilinear
/// interpolation will do a weighted interpolation for calculate the new size.
///
/// This function does not differentiate between images and data. As long as the array is defined
/// and the output dimensions are not 0, it will resize any type or size of array.
///
/// # Parameters
///
/// - `input` is the image to be resized
/// - `odim0` is the output height
/// - `odim1` is the output width
/// - `method` indicates which interpolation method to use for resizing. It uses enum
/// [InterpType](./enum.InterpType.html) to identify the interpolation method.
///
/// # Return Values
///
/// Resized Array
#[allow(unused_mut)]
pub fn resize<T: HasAfEnum>(
input: &Array<T>,
odim0: i64,
odim1: i64,
method: InterpType,
) -> Array<T> {
let mut temp: i64 = 0;
unsafe {
let err_val = af_resize(
&mut temp as MutAfArray,
input.get() as AfArray,
odim0 as DimT,
odim1 as DimT,
method as c_uint,
);
HANDLE_ERROR(AfError::from(err_val));
}
temp.into()
}
/// Transform(Affine) an Image
///
/// The transform function uses an affine transform matrix to tranform an input image into a new
/// one. The transform matrix tf is a 3x2 matrix of type float. The matrix operation is applied to each
/// location (x, y) that is then transformed to (x', y') of the new array. Hence the transformation
/// is an element-wise operation.
///
/// The operation is as below: tf = [r00 r10 r01 r11 t0 t1]
///
/// x' = x * r00 + y * r01 + t0; y' = x * r10 + y * r11 + t1;
///
/// Interpolation types of NEAREST, LINEAR, BILINEAR and CUBIC are allowed. Affine transforms can be used for various purposes. [translate](./fn.translate.html), [scale](./fn.scale.html) and [skew](./fn.skew.html) are
/// specializations of the transform function.
///
/// This function can also handle batch operations.
///
/// # Parameters
///
/// - `input` is the image to be resized
/// - `trans` is the transformation matrix to be used for image transformation
/// - `odim0` is the output height
/// - `odim1` is the output width
/// - `method` indicates which interpolation method to use for resizing. It uses enum
/// [InterpType](./enum.InterpType.html) to identify the interpolation method.
/// - `is_inverse` indicates if to apply inverse/forward transform
///
/// # Return Values
///
/// Transformed Array
#[allow(unused_mut)]
pub fn transform<T: HasAfEnum>(
input: &Array<T>,
trans: &Array<f32>,
odim0: i64,
odim1: i64,
method: InterpType,
is_inverse: bool,
) -> Array<T> {
let mut temp: i64 = 0;
unsafe {
let err_val = af_transform(
&mut temp as MutAfArray,
input.get() as AfArray,
trans.get() as AfArray,
odim0 as DimT,
odim1 as DimT,
method as c_uint,
is_inverse as c_int,
);
HANDLE_ERROR(AfError::from(err_val));
}
temp.into()
}
/// Rotate an Image
///
/// Rotating an input image can be done using either NEAREST or BILINEAR interpolations.
/// Nearest interpolation will pick the nearest value to the location, whereas bilinear
/// interpolation will do a weighted interpolation for calculate the new size.
///
/// This function does not differentiate between images and data. As long as the array is defined,
/// it will rotate any type or size of array.
///
/// The crop option allows you to choose whether to resize the image. If crop is set to false, ie.
/// the entire rotated image will be a part of the array and the new array size will be greater
/// than or equal to the input array size. If crop is set to true, then the new array size is same
/// as the input array size and the data that falls outside the boundaries of the array is
/// discarded.
///
/// Any location of the rotated array that does not map to a location of the input array is set to
/// 0.
///
/// # Parameters
///
/// - `input` is the input image
/// - `theta` is the amount of angle (in radians) image should be rotated
/// - `crop` indicates if the rotated image has to be cropped to original size
/// - `method` indicates which interpolation method to use for rotating the image. It uses enum
/// [InterpType](./enum.InterpType.html) to identify the interpolation method.
///
/// # Return Values
///
/// Rotated Array
#[allow(unused_mut)]
pub fn rotate<T: HasAfEnum>(
input: &Array<T>,
theta: f64,
crop: bool,
method: InterpType,
) -> Array<T> {
let mut temp: i64 = 0;
unsafe {
let err_val = af_rotate(
&mut temp as MutAfArray,
input.get() as AfArray,
theta as c_float,
crop as c_int,
method as c_uint,
);
HANDLE_ERROR(AfError::from(err_val));
}
temp.into()
}
/// Translate an Image
///
/// Translating an image is moving it along 1st and 2nd dimensions by trans0 and trans1. Positive
/// values of these will move the data towards negative x and negative y whereas negative values of
/// these will move the positive right and positive down. See the example below for more.
///
/// To specify an output dimension, use the odim0 and odim1 for dim0 and dim1 respectively. The
/// size of 2rd and 3rd dimension is same as input. If odim0 and odim1 and not defined, then the
/// output dimensions are same as the input dimensions and the data out of bounds will be
/// discarded.
///
/// All new values that do not map to a location of the input array are set to 0.
///
/// Translate is a special case of the [transform](./fn.transform.html) function.
///
/// # Parameters
///
/// - `input` is input image
/// - `trans0` is amount by which the first dimension is translated
/// - `trans1` is amount by which the second dimension is translated
/// - `odim0` is the first output dimension
/// - `odim1` is the second output dimension
/// - `method` is the interpolation type (Nearest by default)
///
/// # Return Values
///
/// Translated Image(Array).
#[allow(unused_mut)]
pub fn translate<T: HasAfEnum>(
input: &Array<T>,
trans0: f32,
trans1: f32,
odim0: i64,
odim1: i64,
method: InterpType,
) -> Array<T> {
let mut temp: i64 = 0;
unsafe {
let err_val = af_translate(
&mut temp as MutAfArray,
input.get() as AfArray,
trans0 as c_float,
trans1 as c_float,
odim0 as DimT,
odim1 as DimT,
method as c_uint,
);
HANDLE_ERROR(AfError::from(err_val));
}
temp.into()
}
/// Scale an Image
///
/// Scale is the same functionality as [resize](./fn.resize.html) except that the scale function uses the transform kernels. The other difference is that scale does not set boundary values to be the boundary of the input array. Instead these are set to 0.
///
/// Scale is a special case of the [transform](./fn.transform.html) function.
///
/// # Parameters
///
/// - `input` is input image
/// - `trans0` is amount by which the first dimension is translated
/// - `trans1` is amount by which the second dimension is translated
/// - `odim0` is the first output dimension
/// - `odim1` is the second output dimension
/// - `method` is the interpolation type (Nearest by default)
///
/// # Return Values
///
/// Translated Image(Array).
#[allow(unused_mut)]
pub fn scale<T: HasAfEnum>(
input: &Array<T>,
scale0: f32,
scale1: f32,
odim0: i64,
odim1: i64,
method: InterpType,
) -> Array<T> {
let mut temp: i64 = 0;
unsafe {
let err_val = af_scale(
&mut temp as MutAfArray,
input.get() as AfArray,
scale0 as c_float,
scale1 as c_float,
odim0 as DimT,
odim1 as DimT,
method as c_uint,
);
HANDLE_ERROR(AfError::from(err_val));
}
temp.into()
}
/// Skew an image
///
/// Skew function skews the input array along dim0 by skew0 and along dim1 by skew1. The skew
/// areguments are in radians. Skewing the data means the data remains parallel along 1 dimensions
/// but the other dimensions gets moved along based on the angle. If both skew0 and skew1 are
/// specified, then the data will be skewed along both directions. Explicit output dimensions
/// can be specified using odim0 and odim1. All new values that do not map to a location of the input array are set to 0.
///
/// Skew is a special case of the [transform](./fn.transform.html) function.
///
/// # Parameters
///
/// - `input` is the image to be skewed
/// - `skew0` is the factor by which data is skewed along first dimension
/// - `skew1` is the factor by which data is skewed along second dimension
/// - `odim0` is the output length along first dimension
/// - `odim1` is the output length along second dimension
/// - `method` indicates which interpolation method to use for rotating the image. It uses enum
/// [InterpType](./enum.InterpType.html) to identify the interpolation method.
/// - `is_inverse` indicates if to apply inverse/forward transform
///
/// # Return Values
///
/// Skewed Image
#[allow(unused_mut)]
pub fn skew<T: HasAfEnum>(
input: &Array<T>,
skew0: f32,
skew1: f32,
odim0: i64,
odim1: i64,
method: InterpType,
is_inverse: bool,
) -> Array<T> {
let mut temp: i64 = 0;
unsafe {
let err_val = af_skew(
&mut temp as MutAfArray,
input.get() as AfArray,
skew0 as c_float,
skew1 as c_float,
odim0 as DimT,
odim1 as DimT,
method as c_uint,
is_inverse as c_int,
);
HANDLE_ERROR(AfError::from(err_val));
}
temp.into()
}
/// Compute Histogram of an Array
///
/// A histogram is a representation of the distribution of given data. This representation is
/// essentially a graph consisting of the data range or domain on one axis and frequency of
/// occurence on the other axis. All the data in the domain is counted in the appropriate bin. The
/// total number of elements belonging to each bin is known as the bin's frequency.
///
/// The regular histogram function creates bins of equal size between the minimum and maximum of
/// the input data (min and max are calculated internally). The histogram min-max function takes
/// input parameters minimum and maximum, and divides the bins into equal sizes within the range
/// specified by min and max parameters. All values less than min in the data range are placed in
/// the first (min) bin and all values greater than max will be placed in the last (max) bin.
///
/// # Parameters
///
/// - `input` is the Array whose histogram has to be computed
/// - `nbins` is the number bins the input data has to be categorized into.
/// - `minval` is the minimum value of bin ordering
/// - `maxval` is the maximum value of bin ordering
///
/// # Return Values
///
/// Histogram of input Array
#[allow(unused_mut)]
pub fn histogram<T>(input: &Array<T>, nbins: u32, minval: f64, maxval: f64) -> Array<u32>
where
T: HasAfEnum + RealNumber,
{
let mut temp: i64 = 0;
unsafe {
let err_val = af_histogram(
&mut temp as MutAfArray,
input.get() as AfArray,
nbins as c_uint,
minval as c_double,
maxval as c_double,
);
HANDLE_ERROR(AfError::from(err_val));
}
temp.into()
}
/// Dilate an Image
///
/// The dilation function takes two pieces of data as inputs. The first is the input image to be
/// morphed, and the second is the mask indicating the neighborhood around each pixel to match.
///
/// In dilation, for each pixel, the mask is centered at the pixel. If the center pixel of the mask
/// matches the corresponding pixel on the image, then the mask is accepted. If the center pixels
/// do not matches, then the mask is ignored and no changes are made.
///
/// For further reference, see [here](https://en.wikipedia.org/wiki/Dilation_(morphology)).
///
/// # Parameters
///
/// - `input` is the input image
/// - `mask` is the morphological operation mask
///
/// # Return Values
///
/// Dilated Image(Array)
#[allow(unused_mut)]
pub fn dilate<T>(input: &Array<T>, mask: &Array<T>) -> Array<T>
where
T: HasAfEnum + ImageFilterType,
{
let mut temp: i64 = 0;
unsafe {
let err_val = af_dilate(
&mut temp as MutAfArray,
input.get() as AfArray,
mask.get() as AfArray,
);
HANDLE_ERROR(AfError::from(err_val));
}
temp.into()
}
/// Erode an Image
///
/// The erosion function is a morphological transformation on an image that requires two inputs.
/// The first is the image to be morphed, and the second is the mask indicating neighborhood that
/// must be white in order to preserve each pixel.
///
/// In erode, for each pixel, the mask is centered at the pixel. If each pixel of the mask matches
/// the corresponding pixel on the image, then no change is made. If there is at least one
/// mismatch, then pixels are changed to the background color (black).
///
/// For further reference, see [here](https://en.wikipedia.org/wiki/Erosion_(morphology)).
///
/// # Parameters
///
/// - `input` is the input image
/// - `mask` is the morphological operation mask
///
/// # Return Values
///
/// Eroded Image(Array)
#[allow(unused_mut)]
pub fn erode<T>(input: &Array<T>, mask: &Array<T>) -> Array<T>
where
T: HasAfEnum + ImageFilterType,
{
let mut temp: i64 = 0;
unsafe {
let err_val = af_erode(
&mut temp as MutAfArray,
input.get() as AfArray,
mask.get() as AfArray,
);
HANDLE_ERROR(AfError::from(err_val));
}
temp.into()
}
/// Dilate a Volume
///
/// Dilation for a volume is similar to the way dilation works on an image. Only difference is that
/// the masking operation is performed on a volume instead of a rectangular region.
///
/// # Parameters
///
/// - `input` is the input volume
/// - `mask` is the morphological operation mask
///
/// # Return Values
///
/// Dilated Volume(Array)
#[allow(unused_mut)]
pub fn dilate3<T>(input: &Array<T>, mask: &Array<T>) -> Array<T>
where
T: HasAfEnum + ImageFilterType,
{
let mut temp: i64 = 0;
unsafe {
let err_val = af_dilate3(
&mut temp as MutAfArray,
input.get() as AfArray,
mask.get() as AfArray,
);
HANDLE_ERROR(AfError::from(err_val));
}
temp.into()
}
/// Erode a Volume
///
/// Erosion for a volume is similar to the way erosion works on an image. Only difference is that
/// the masking operation is performed on a volume instead of a rectangular region.
///
/// # Parameters
///
/// - `input` is the input volume
/// - `mask` is the morphological operation mask
///
/// # Return Values
///
/// Eroded Volume(Array)
#[allow(unused_mut)]
pub fn erode3<T>(input: &Array<T>, mask: &Array<T>) -> Array<T>
where
T: HasAfEnum + ImageFilterType,
{
let mut temp: i64 = 0;
unsafe {
let err_val = af_erode3(
&mut temp as MutAfArray,
input.get() as AfArray,
mask.get() as AfArray,
);
HANDLE_ERROR(AfError::from(err_val));
}
temp.into()
}
/// Bilateral Filter.
///
/// A bilateral filter is a edge-preserving filter that reduces noise in an image. The intensity of
/// each pixel is replaced by a weighted average of the intensities of nearby pixels. The weights
/// follow a Gaussian distribution and depend on the distance as well as the color distance.
///
/// The bilateral filter requires the size of the filter (in pixels) and the upper bound on color
/// values, N, where pixel values range from 0–N inclusively.
///
/// # Parameters
///
/// - `input` array is the input image
/// - `spatial_sigma` is the spatial variance parameter that decides the filter window
/// - `chromatic_sigma` is the chromatic variance parameter
/// - `iscolor` indicates if the input is color image or grayscale
///
/// # Return Values
///
/// Filtered Image - Array
#[allow(unused_mut)]
pub fn bilateral<T>(
input: &Array<T>,
spatial_sigma: f32,
chromatic_sigma: f32,
iscolor: bool,
) -> Array<T::AbsOutType>
where
T: HasAfEnum + ImageFilterType,
T::AbsOutType: HasAfEnum,
{
let mut temp: i64 = 0;
unsafe {
let err_val = af_bilateral(
&mut temp as MutAfArray,
input.get() as AfArray,
spatial_sigma as c_float,
chromatic_sigma as c_float,
iscolor as c_int,
);
HANDLE_ERROR(AfError::from(err_val));
}
temp.into()
}
/// Meanshift Filter.
///
/// A meanshift filter is an edge-preserving smoothing filter commonly used in object tracking and
/// image segmentation.
///
/// This filter replaces each pixel in the image with the mean of the values within a given given
/// color and spatial radius. The meanshift filter is an iterative algorithm that continues until a
/// maxium number of iterations is met or until the value of the means no longer changes.
///
/// # Parameters
///
/// - `input` array is the input image
/// - `spatial_sigma` is the spatial variance parameter that decides the filter window
/// - `chromatic_sigma` is the chromatic variance parameter
/// - `iter` is the number of iterations filter operation is performed
/// - `iscolor` indicates if the input is color image or grayscale
///
/// # Return Values
///
/// Filtered Image - Array
#[allow(unused_mut)]
pub fn mean_shift<T>(
input: &Array<T>,
spatial_sigma: f32,
chromatic_sigma: f32,
iter: u32,
iscolor: bool,
) -> Array<T>
where
T: HasAfEnum + RealNumber,
{
let mut temp: i64 = 0;
unsafe {
let err_val = af_mean_shift(
&mut temp as MutAfArray,
input.get() as AfArray,
spatial_sigma as c_float,
chromatic_sigma as c_float,
iter as c_uint,
iscolor as c_int,
);
HANDLE_ERROR(AfError::from(err_val));
}
temp.into()
}
macro_rules! filt_func_def {
($doc_str: expr, $fn_name: ident, $ffi_name: ident) => {
#[doc=$doc_str]
///
///# Parameters
///
/// - `input` is the input image(Array)
/// - `wlen` is the horizontal length of the filter
/// - `hlen` is the vertical length of the filter
/// - `etype` is enum of type [BorderType](./enum.BorderType.html)
///
///# Return Values
///
/// An Array with filtered image data.
#[allow(unused_mut)]
pub fn $fn_name<T>(input: &Array<T>, wlen: u64, wwid: u64, etype: BorderType) -> Array<T>
where
T: HasAfEnum + ImageFilterType,
{
let mut temp: i64 = 0;
unsafe {
let err_val = $ffi_name(
&mut temp as MutAfArray,
input.get() as AfArray,
wlen as DimT,
wwid as DimT,
etype as c_uint,
);
HANDLE_ERROR(AfError::from(err_val));
}
temp.into()
}
};
}
filt_func_def!("Median filter", medfilt, af_medfilt);
filt_func_def!(
"Box filter with minimum as box operation",
minfilt,
af_minfilt
);
filt_func_def!(
"Box filter with maximum as box operation",
maxfilt,
af_maxfilt
);
/// Creates a Gaussian Kernel.
///
/// This function creates a kernel of a specified size that contains a Gaussian distribution. This
/// distribution is normalized to one. This is most commonly used when performing a Gaussian blur
/// on an image. The function takes two sets of arguments, the size of the kernel (width and height
/// in pixels) and the sigma parameters (for row and column) which effect the distribution of the
/// weights in the y and x directions, respectively.
///
/// Changing sigma causes the weights in each direction to vary. Sigma is calculated internally as
/// (0.25 * rows + 0.75) for rows and similarly for columns.