Skip to content

Commit 3250f11

Browse files
committed
Tutorial Laplace Operator
1 parent d068e27 commit 3250f11

File tree

5 files changed

+292
-56
lines changed

5 files changed

+292
-56
lines changed

doc/tutorials/imgproc/imgtrans/laplace_operator/laplace_operator.markdown

Lines changed: 117 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
Laplace Operator {#tutorial_laplace_operator}
22
================
33

4+
@prev_tutorial{tutorial_sobel_derivatives}
5+
@next_tutorial{tutorial_canny_detector}
6+
47
Goal
58
----
69

710
In this tutorial you will learn how to:
811

9-
- Use the OpenCV function @ref cv::Laplacian to implement a discrete analog of the *Laplacian
12+
- Use the OpenCV function **Laplacian()** to implement a discrete analog of the *Laplacian
1013
operator*.
1114

1215
Theory
@@ -37,7 +40,7 @@ Theory
3740

3841
\f[Laplace(f) = \dfrac{\partial^{2} f}{\partial x^{2}} + \dfrac{\partial^{2} f}{\partial y^{2}}\f]
3942

40-
-# The Laplacian operator is implemented in OpenCV by the function @ref cv::Laplacian . In fact,
43+
-# The Laplacian operator is implemented in OpenCV by the function **Laplacian()** . In fact,
4144
since the Laplacian uses the gradient of images, it calls internally the *Sobel* operator to
4245
perform its computation.
4346

@@ -50,25 +53,98 @@ Code
5053
- Applies a Laplacian operator to the grayscale image and stores the output image
5154
- Display the result in a window
5255

56+
@add_toggle_cpp
5357
-# The tutorial code's is shown lines below. You can also download it from
54-
[here](https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/ImgTrans/Laplace_Demo.cpp)
58+
[here](https://raw.githubusercontent.com/opencv/opencv/master/samples/cpp/tutorial_code/ImgTrans/Laplace_Demo.cpp)
5559
@include samples/cpp/tutorial_code/ImgTrans/Laplace_Demo.cpp
60+
@end_toggle
61+
62+
@add_toggle_java
63+
-# The tutorial code's is shown lines below. You can also download it from
64+
[here](https://raw.githubusercontent.com/opencv/opencv/master/samples/java/tutorial_code/ImgTrans/LaPlace/LaplaceDemo.java)
65+
@include samples/java/tutorial_code/ImgTrans/LaPlace/LaplaceDemo.java
66+
@end_toggle
67+
68+
@add_toggle_python
69+
-# The tutorial code's is shown lines below. You can also download it from
70+
[here](https://raw.githubusercontent.com/opencv/opencv/master/samples/python/tutorial_code/ImgTrans/LaPlace/laplace_demo.py)
71+
@include samples/python/tutorial_code/ImgTrans/LaPlace/laplace_demo.py
72+
@end_toggle
5673

5774
Explanation
5875
-----------
5976

60-
-# Create some needed variables:
61-
@snippet cpp/tutorial_code/ImgTrans/Laplace_Demo.cpp variables
62-
-# Loads the source image:
63-
@snippet cpp/tutorial_code/ImgTrans/Laplace_Demo.cpp load
64-
-# Apply a Gaussian blur to reduce noise:
65-
@snippet cpp/tutorial_code/ImgTrans/Laplace_Demo.cpp reduce_noise
66-
-# Convert the image to grayscale using @ref cv::cvtColor
67-
@snippet cpp/tutorial_code/ImgTrans/Laplace_Demo.cpp convert_to_gray
68-
-# Apply the Laplacian operator to the grayscale image:
69-
@snippet cpp/tutorial_code/ImgTrans/Laplace_Demo.cpp laplacian
70-
where the arguments are:
77+
#### Declare variables
78+
79+
@add_toggle_cpp
80+
@snippet cpp/tutorial_code/ImgTrans/Laplace_Demo.cpp variables
81+
@end_toggle
82+
83+
@add_toggle_java
84+
@snippet samples/java/tutorial_code/ImgTrans/LaPlace/LaplaceDemo.java variables
85+
@end_toggle
86+
87+
@add_toggle_python
88+
@snippet samples/python/tutorial_code/ImgTrans/LaPlace/laplace_demo.py variables
89+
@end_toggle
90+
91+
#### Load source image
92+
93+
@add_toggle_cpp
94+
@snippet cpp/tutorial_code/ImgTrans/Laplace_Demo.cpp load
95+
@end_toggle
96+
97+
@add_toggle_java
98+
@snippet samples/java/tutorial_code/ImgTrans/LaPlace/LaplaceDemo.java load
99+
@end_toggle
100+
101+
@add_toggle_python
102+
@snippet samples/python/tutorial_code/ImgTrans/LaPlace/laplace_demo.py load
103+
@end_toggle
104+
105+
#### Reduce noise
106+
107+
@add_toggle_cpp
108+
@snippet cpp/tutorial_code/ImgTrans/Laplace_Demo.cpp reduce_noise
109+
@end_toggle
110+
111+
@add_toggle_java
112+
@snippet samples/java/tutorial_code/ImgTrans/LaPlace/LaplaceDemo.java reduce_noise
113+
@end_toggle
114+
115+
@add_toggle_python
116+
@snippet samples/python/tutorial_code/ImgTrans/LaPlace/laplace_demo.py reduce_noise
117+
@end_toggle
71118

119+
#### Grayscale
120+
121+
@add_toggle_cpp
122+
@snippet cpp/tutorial_code/ImgTrans/Laplace_Demo.cpp convert_to_gray
123+
@end_toggle
124+
125+
@add_toggle_java
126+
@snippet samples/java/tutorial_code/ImgTrans/LaPlace/LaplaceDemo.java convert_to_gray
127+
@end_toggle
128+
129+
@add_toggle_python
130+
@snippet samples/python/tutorial_code/ImgTrans/LaPlace/laplace_demo.py convert_to_gray
131+
@end_toggle
132+
133+
#### Laplacian operator
134+
135+
@add_toggle_cpp
136+
@snippet cpp/tutorial_code/ImgTrans/Laplace_Demo.cpp laplacian
137+
@end_toggle
138+
139+
@add_toggle_java
140+
@snippet samples/java/tutorial_code/ImgTrans/LaPlace/LaplaceDemo.java laplacian
141+
@end_toggle
142+
143+
@add_toggle_python
144+
@snippet samples/python/tutorial_code/ImgTrans/LaPlace/laplace_demo.py laplacian
145+
@end_toggle
146+
147+
- The arguments are:
72148
- *src_gray*: The input image.
73149
- *dst*: Destination (output) image
74150
- *ddepth*: Depth of the destination image. Since our input is *CV_8U* we define *ddepth* =
@@ -77,10 +153,33 @@ Explanation
77153
this example.
78154
- *scale*, *delta* and *BORDER_DEFAULT*: We leave them as default values.
79155

80-
-# Convert the output from the Laplacian operator to a *CV_8U* image:
81-
@snippet cpp/tutorial_code/ImgTrans/Laplace_Demo.cpp convert
82-
-# Display the result in a window:
83-
@snippet cpp/tutorial_code/ImgTrans/Laplace_Demo.cpp display
156+
#### Convert output to a *CV_8U* image
157+
158+
@add_toggle_cpp
159+
@snippet cpp/tutorial_code/ImgTrans/Laplace_Demo.cpp convert
160+
@end_toggle
161+
162+
@add_toggle_java
163+
@snippet samples/java/tutorial_code/ImgTrans/LaPlace/LaplaceDemo.java convert
164+
@end_toggle
165+
166+
@add_toggle_python
167+
@snippet samples/python/tutorial_code/ImgTrans/LaPlace/laplace_demo.py convert
168+
@end_toggle
169+
170+
#### Display the result
171+
172+
@add_toggle_cpp
173+
@snippet cpp/tutorial_code/ImgTrans/Laplace_Demo.cpp display
174+
@end_toggle
175+
176+
@add_toggle_java
177+
@snippet samples/java/tutorial_code/ImgTrans/LaPlace/LaplaceDemo.java display
178+
@end_toggle
179+
180+
@add_toggle_python
181+
@snippet samples/python/tutorial_code/ImgTrans/LaPlace/laplace_demo.py display
182+
@end_toggle
84183

85184
Results
86185
-------

doc/tutorials/imgproc/table_of_content_imgproc.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ In this section you will learn about the image processing (manipulation) functio
9999

100100
- @subpage tutorial_laplace_operator
101101

102+
*Languages:* C++, Java, Python
103+
102104
*Compatibility:* \> OpenCV 2.0
103105

104106
*Author:* Ana Huamán

samples/cpp/tutorial_code/ImgTrans/Laplace_Demo.cpp

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,50 +15,53 @@ using namespace cv;
1515
*/
1616
int main( int argc, char** argv )
1717
{
18-
//![variables]
19-
Mat src, src_gray, dst;
20-
int kernel_size = 3;
21-
int scale = 1;
22-
int delta = 0;
23-
int ddepth = CV_16S;
24-
const char* window_name = "Laplace Demo";
25-
//![variables]
18+
//![variables]
19+
// Declare the variables we are going to use
20+
Mat src, src_gray, dst;
21+
int kernel_size = 3;
22+
int scale = 1;
23+
int delta = 0;
24+
int ddepth = CV_16S;
25+
const char* window_name = "Laplace Demo";
26+
//![variables]
2627

27-
//![load]
28-
String imageName("../data/lena.jpg"); // by default
29-
if (argc > 1)
30-
{
31-
imageName = argv[1];
32-
}
33-
src = imread( imageName, IMREAD_COLOR ); // Load an image
28+
//![load]
29+
const char* imageName = argc >=2 ? argv[1] : "../data/lena.jpg";
3430

35-
if( src.empty() )
36-
{ return -1; }
37-
//![load]
31+
src = imread( imageName, IMREAD_COLOR ); // Load an image
3832

39-
//![reduce_noise]
40-
/// Reduce noise by blurring with a Gaussian filter
41-
GaussianBlur( src, src, Size(3,3), 0, 0, BORDER_DEFAULT );
42-
//![reduce_noise]
33+
// Check if image is loaded fine
34+
if(src.empty()){
35+
printf(" Error opening image\n");
36+
printf(" Program Arguments: [image_name -- default ../data/lena.jpg] \n");
37+
return -1;
38+
}
39+
//![load]
4340

44-
//![convert_to_gray]
45-
cvtColor( src, src_gray, COLOR_BGR2GRAY ); // Convert the image to grayscale
46-
//![convert_to_gray]
41+
//![reduce_noise]
42+
// Reduce noise by blurring with a Gaussian filter ( kernel size = 3 )
43+
GaussianBlur( src, src, Size(3, 3), 0, 0, BORDER_DEFAULT );
44+
//![reduce_noise]
4745

48-
/// Apply Laplace function
49-
Mat abs_dst;
50-
//![laplacian]
51-
Laplacian( src_gray, dst, ddepth, kernel_size, scale, delta, BORDER_DEFAULT );
52-
//![laplacian]
46+
//![convert_to_gray]
47+
cvtColor( src, src_gray, COLOR_BGR2GRAY ); // Convert the image to grayscale
48+
//![convert_to_gray]
5349

54-
//![convert]
55-
convertScaleAbs( dst, abs_dst );
56-
//![convert]
50+
/// Apply Laplace function
51+
Mat abs_dst;
52+
//![laplacian]
53+
Laplacian( src_gray, dst, ddepth, kernel_size, scale, delta, BORDER_DEFAULT );
54+
//![laplacian]
5755

58-
//![display]
59-
imshow( window_name, abs_dst );
60-
waitKey(0);
61-
//![display]
56+
//![convert]
57+
// converting back to CV_8U
58+
convertScaleAbs( dst, abs_dst );
59+
//![convert]
6260

63-
return 0;
61+
//![display]
62+
imshow( window_name, abs_dst );
63+
waitKey(0);
64+
//![display]
65+
66+
return 0;
6467
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* @file LaplaceDemo.java
3+
* @brief Sample code showing how to detect edges using the Laplace operator
4+
*/
5+
6+
import org.opencv.core.*;
7+
import org.opencv.highgui.HighGui;
8+
import org.opencv.imgcodecs.Imgcodecs;
9+
import org.opencv.imgproc.Imgproc;
10+
11+
class LaplaceDemoRun {
12+
13+
public void run(String[] args) {
14+
//! [variables]
15+
// Declare the variables we are going to use
16+
Mat src, src_gray = new Mat(), dst = new Mat();
17+
int kernel_size = 3;
18+
int scale = 1;
19+
int delta = 0;
20+
int ddepth = CvType.CV_16S;
21+
String window_name = "Laplace Demo";
22+
//! [variables]
23+
24+
//! [load]
25+
String imageName = ((args.length > 0) ? args[0] : "../data/lena.jpg");
26+
27+
src = Imgcodecs.imread(imageName, Imgcodecs.IMREAD_COLOR); // Load an image
28+
29+
// Check if image is loaded fine
30+
if( src.empty() ) {
31+
System.out.println("Error opening image");
32+
System.out.println("Program Arguments: [image_name -- default ../data/lena.jpg] \n");
33+
System.exit(-1);
34+
}
35+
//! [load]
36+
37+
//! [reduce_noise]
38+
// Reduce noise by blurring with a Gaussian filter ( kernel size = 3 )
39+
Imgproc.GaussianBlur( src, src, new Size(3, 3), 0, 0, Core.BORDER_DEFAULT );
40+
//! [reduce_noise]
41+
42+
//! [convert_to_gray]
43+
// Convert the image to grayscale
44+
Imgproc.cvtColor( src, src_gray, Imgproc.COLOR_RGB2GRAY );
45+
//! [convert_to_gray]
46+
47+
/// Apply Laplace function
48+
Mat abs_dst = new Mat();
49+
//! [laplacian]
50+
Imgproc.Laplacian( src_gray, dst, ddepth, kernel_size, scale, delta, Core.BORDER_DEFAULT );
51+
//! [laplacian]
52+
53+
//! [convert]
54+
// converting back to CV_8U
55+
Core.convertScaleAbs( dst, abs_dst );
56+
//! [convert]
57+
58+
//! [display]
59+
HighGui.imshow( window_name, abs_dst );
60+
HighGui.waitKey(0);
61+
//! [display]
62+
63+
System.exit(0);
64+
}
65+
}
66+
67+
public class LaplaceDemo {
68+
public static void main(String[] args) {
69+
// Load the native library.
70+
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
71+
new LaplaceDemoRun().run(args);
72+
}
73+
}

0 commit comments

Comments
 (0)