Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
107 lines (89 loc) · 3.17 KB

File metadata and controls

107 lines (89 loc) · 3.17 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package opencvtutorial;
/**
* @file opencvtutorial.SobelDemo.java
* @brief Sample code using Sobel and/or Scharr OpenCV functions to make a simple Edge Detector
*/
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.scijava.nativelib.NativeLoader;
class SobelDemoRun {
static {
try {
// Load the native OpenCV library
NativeLoader.loadLibrary(Core.NATIVE_LIBRARY_NAME);
} catch (Exception e) {
}
}
public void run(String[] args) {
// ! [declare_variables]
// First we declare the variables we are going to use
Mat src, src_gray = new Mat();
Mat grad = new Mat();
String window_name = "Sobel Demo - Simple Edge Detector";
int scale = 1;
int delta = 0;
int ddepth = CvType.CV_16S;
// ! [declare_variables]
// ! [load]
// As usual we load our source image (src)
// Check number of arguments
if (args.length == 0) {
System.out.println("Not enough parameters!");
System.out.println("Program Arguments: [image_path]");
System.exit(-1);
}
// Load the image
src = Imgcodecs.imread(args[0]);
// Check if image is loaded fine
if (src.empty()) {
System.out.println("Error opening image: " + args[0]);
System.exit(-1);
}
// ! [load]
// ! [reduce_noise]
// Remove noise by blurring with a Gaussian filter ( kernel size = 3 )
Imgproc.GaussianBlur(src, src, new Size(3, 3), 0, 0, Core.BORDER_DEFAULT);
// ! [reduce_noise]
// ! [convert_to_gray]
// Convert the image to grayscale
Imgproc.cvtColor(src, src_gray, Imgproc.COLOR_RGB2GRAY);
// ! [convert_to_gray]
// ! [sobel]
/// Generate grad_x and grad_y
Mat grad_x = new Mat(), grad_y = new Mat();
Mat abs_grad_x = new Mat(), abs_grad_y = new Mat();
/// Gradient X
// Imgproc.Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta,
/// Core.BORDER_DEFAULT );
Imgproc.Sobel(src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, Core.BORDER_DEFAULT);
/// Gradient Y
// Imgproc.Scharr( src_gray, grad_y, ddepth, 0, 1, scale, delta,
/// Core.BORDER_DEFAULT );
Imgproc.Sobel(src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, Core.BORDER_DEFAULT);
// ! [sobel]
// ![convert]
// converting back to CV_8U
Core.convertScaleAbs(grad_x, abs_grad_x);
Core.convertScaleAbs(grad_y, abs_grad_y);
// ![convert]
// ! [add_weighted]
/// Total Gradient (approximate)
Core.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad);
// ! [add_weighted]
// ! [display]
HighGui.imshow(window_name, grad);
HighGui.waitKey(0);
// ! [display]
System.exit(0);
}
}
public class SobelDemo {
public static void main(String[] args) {
new SobelDemoRun().run(args);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.