Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/java/tutorial_code/ImgProc/Smoothing/Smoothing.java
16354 views
1
import org.opencv.core.*;
2
import org.opencv.highgui.HighGui;
3
import org.opencv.imgcodecs.Imgcodecs;
4
import org.opencv.imgproc.Imgproc;
5
6
class SmoothingRun {
7
8
/// Global Variables
9
int DELAY_CAPTION = 1500;
10
int DELAY_BLUR = 100;
11
int MAX_KERNEL_LENGTH = 31;
12
13
Mat src = new Mat(), dst = new Mat();
14
String windowName = "Filter Demo 1";
15
16
public void run(String[] args) {
17
18
String filename = ((args.length > 0) ? args[0] : "../data/lena.jpg");
19
20
src = Imgcodecs.imread(filename, Imgcodecs.IMREAD_COLOR);
21
if( src.empty() ) {
22
System.out.println("Error opening image");
23
System.out.println("Usage: ./Smoothing [image_name -- default ../data/lena.jpg] \n");
24
System.exit(-1);
25
}
26
27
if( displayCaption( "Original Image" ) != 0 ) { System.exit(0); }
28
29
dst = src.clone();
30
if( displayDst( DELAY_CAPTION ) != 0 ) { System.exit(0); }
31
32
/// Applying Homogeneous blur
33
if( displayCaption( "Homogeneous Blur" ) != 0 ) { System.exit(0); }
34
35
//! [blur]
36
for (int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2) {
37
Imgproc.blur(src, dst, new Size(i, i), new Point(-1, -1));
38
displayDst(DELAY_BLUR);
39
}
40
//! [blur]
41
42
/// Applying Gaussian blur
43
if( displayCaption( "Gaussian Blur" ) != 0 ) { System.exit(0); }
44
45
//! [gaussianblur]
46
for (int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2) {
47
Imgproc.GaussianBlur(src, dst, new Size(i, i), 0, 0);
48
displayDst(DELAY_BLUR);
49
}
50
//! [gaussianblur]
51
52
/// Applying Median blur
53
if( displayCaption( "Median Blur" ) != 0 ) { System.exit(0); }
54
55
//! [medianblur]
56
for (int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2) {
57
Imgproc.medianBlur(src, dst, i);
58
displayDst(DELAY_BLUR);
59
}
60
//! [medianblur]
61
62
/// Applying Bilateral Filter
63
if( displayCaption( "Bilateral Blur" ) != 0 ) { System.exit(0); }
64
65
//![bilateralfilter]
66
for (int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2) {
67
Imgproc.bilateralFilter(src, dst, i, i * 2, i / 2);
68
displayDst(DELAY_BLUR);
69
}
70
//![bilateralfilter]
71
72
/// Done
73
displayCaption( "Done!" );
74
75
System.exit(0);
76
}
77
78
int displayCaption(String caption) {
79
dst = Mat.zeros(src.size(), src.type());
80
Imgproc.putText(dst, caption,
81
new Point(src.cols() / 4, src.rows() / 2),
82
Imgproc.FONT_HERSHEY_COMPLEX, 1, new Scalar(255, 255, 255));
83
84
return displayDst(DELAY_CAPTION);
85
}
86
87
int displayDst(int delay) {
88
HighGui.imshow( windowName, dst );
89
int c = HighGui.waitKey( delay );
90
if (c >= 0) { return -1; }
91
return 0;
92
}
93
}
94
95
public class Smoothing {
96
public static void main(String[] args) {
97
// Load the native library.
98
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
99
new SmoothingRun().run(args);
100
}
101
}
102
103