Skip to content

Commit c4c1e94

Browse files
committed
Tutorial Adding Images
1 parent 89172c0 commit c4c1e94

File tree

5 files changed

+158
-18
lines changed

5 files changed

+158
-18
lines changed

doc/tutorials/core/adding_images/adding_images.markdown

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
Adding (blending) two images using OpenCV {#tutorial_adding_images}
22
=========================================
33

4+
@prev_tutorial{tutorial_mat_operations}
5+
@next_tutorial{tutorial_basic_linear_transform}
6+
47
Goal
58
----
69

710
In this tutorial you will learn:
811

912
- what is *linear blending* and why it is useful;
10-
- how to add two images using @ref cv::addWeighted
13+
- how to add two images using **addWeighted()**
1114

1215
Theory
1316
------
@@ -28,33 +31,83 @@ eh?)
2831
Source Code
2932
-----------
3033

34+
@add_toggle_cpp
3135
Download the source code from
32-
[here](https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/core/AddingImages/AddingImages.cpp).
36+
[here](https://raw.githubusercontent.com/opencv/opencv/master/samples/cpp/tutorial_code/core/AddingImages/AddingImages.cpp).
3337
@include cpp/tutorial_code/core/AddingImages/AddingImages.cpp
38+
@end_toggle
39+
40+
@add_toggle_java
41+
Download the source code from
42+
[here](https://raw.githubusercontent.com/opencv/opencv/master/samples/java/tutorial_code/core/AddingImages/AddingImages.java).
43+
@include java/tutorial_code/core/AddingImages/AddingImages.java
44+
@end_toggle
45+
46+
@add_toggle_python
47+
Download the source code from
48+
[here](https://raw.githubusercontent.com/opencv/opencv/master/samples/python/tutorial_code/core/AddingImages/adding_images.py).
49+
@include python/tutorial_code/core/AddingImages/adding_images.py
50+
@end_toggle
3451

3552
Explanation
3653
-----------
3754

38-
-# Since we are going to perform:
55+
Since we are going to perform:
56+
57+
\f[g(x) = (1 - \alpha)f_{0}(x) + \alpha f_{1}(x)\f]
58+
59+
We need two source images (\f$f_{0}(x)\f$ and \f$f_{1}(x)\f$). So, we load them in the usual way:
60+
@add_toggle_cpp
61+
@snippet cpp/tutorial_code/core/AddingImages/AddingImages.cpp load
62+
@end_toggle
63+
64+
@add_toggle_java
65+
@snippet java/tutorial_code/core/AddingImages/AddingImages.java load
66+
@end_toggle
67+
68+
@add_toggle_python
69+
@snippet python/tutorial_code/core/AddingImages/adding_images.py load
70+
@end_toggle
71+
72+
We used the following images: [LinuxLogo.jpg](https://raw.githubusercontent.com/opencv/opencv/master/samples/data/LinuxLogo.jpg) and [WindowsLogo.jpg](https://raw.githubusercontent.com/opencv/opencv/master/samples/data/WindowsLogo.jpg)
73+
74+
@warning Since we are *adding* *src1* and *src2*, they both have to be of the same size
75+
(width and height) and type.
76+
77+
Now we need to generate the `g(x)` image. For this, the function **addWeighted()** comes quite handy:
78+
79+
@add_toggle_cpp
80+
@snippet cpp/tutorial_code/core/AddingImages/AddingImages.cpp blend_images
81+
@end_toggle
3982

40-
\f[g(x) = (1 - \alpha)f_{0}(x) + \alpha f_{1}(x)\f]
83+
@add_toggle_java
84+
@snippet java/tutorial_code/core/AddingImages/AddingImages.java blend_images
85+
@end_toggle
4186

42-
We need two source images (\f$f_{0}(x)\f$ and \f$f_{1}(x)\f$). So, we load them in the usual way:
43-
@snippet cpp/tutorial_code/core/AddingImages/AddingImages.cpp load
87+
@add_toggle_python
88+
@snippet python/tutorial_code/core/AddingImages/adding_images.py blend_images
89+
Numpy version of above line (but cv2 function is around 2x faster):
90+
\code{.py}
91+
dst = np.uint8(alpha*(img1)+beta*(img2))
92+
\endcode
93+
@end_toggle
4494

45-
**warning**
95+
since **addWeighted()** produces:
96+
\f[dst = \alpha \cdot src1 + \beta \cdot src2 + \gamma\f]
97+
In this case, `gamma` is the argument \f$0.0\f$ in the code above.
4698

47-
Since we are *adding* *src1* and *src2*, they both have to be of the same size (width and
48-
height) and type.
99+
Create windows, show the images and wait for the user to end the program.
100+
@add_toggle_cpp
101+
@snippet cpp/tutorial_code/core/AddingImages/AddingImages.cpp display
102+
@end_toggle
49103

50-
-# Now we need to generate the `g(x)` image. For this, the function @ref cv::addWeighted comes quite handy:
51-
@snippet cpp/tutorial_code/core/AddingImages/AddingImages.cpp blend_images
52-
since @ref cv::addWeighted produces:
53-
\f[dst = \alpha \cdot src1 + \beta \cdot src2 + \gamma\f]
54-
In this case, `gamma` is the argument \f$0.0\f$ in the code above.
104+
@add_toggle_java
105+
@snippet java/tutorial_code/core/AddingImages/AddingImages.java display
106+
@end_toggle
55107

56-
-# Create windows, show the images and wait for the user to end the program.
57-
@snippet cpp/tutorial_code/core/AddingImages/AddingImages.cpp display
108+
@add_toggle_python
109+
@snippet python/tutorial_code/core/AddingImages/adding_images.py display
110+
@end_toggle
58111

59112
Result
60113
------

doc/tutorials/core/table_of_content_core.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ understanding how to manipulate the images on a pixel level.
4040

4141
- @subpage tutorial_adding_images
4242

43+
*Languages:* C++, Java, Python
44+
4345
*Compatibility:* \> OpenCV 2.0
4446

4547
*Author:* Ana Huamán

samples/cpp/tutorial_code/core/AddingImages/AddingImages.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* @brief Simple linear blender ( dst = alpha*src1 + beta*src2 )
44
* @author OpenCV team
55
*/
6-
76
#include "opencv2/imgcodecs.hpp"
87
#include "opencv2/highgui.hpp"
98
#include <iostream>
@@ -24,7 +23,7 @@ int main( void )
2423
/// Ask the user enter alpha
2524
cout << " Simple Linear Blender " << endl;
2625
cout << "-----------------------" << endl;
27-
cout << "* Enter alpha [0-1]: ";
26+
cout << "* Enter alpha [0.0-1.0]: ";
2827
cin >> input;
2928

3029
// We use the alpha provided by the user if it is between 0 and 1
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import org.opencv.core.*;
2+
import org.opencv.highgui.HighGui;
3+
import org.opencv.imgcodecs.Imgcodecs;
4+
5+
import java.util.Locale;
6+
import java.util.Scanner;
7+
8+
class AddingImagesRun{
9+
public void run() {
10+
double alpha = 0.5; double beta; double input;
11+
12+
Mat src1, src2, dst = new Mat();
13+
14+
System.out.println(" Simple Linear Blender ");
15+
System.out.println("-----------------------");
16+
System.out.println("* Enter alpha [0.0-1.0]: ");
17+
Scanner scan = new Scanner( System.in ).useLocale(Locale.US);
18+
input = scan.nextDouble();
19+
20+
if( input >= 0.0 && input <= 1.0 )
21+
alpha = input;
22+
23+
//! [load]
24+
src1 = Imgcodecs.imread("../../images/LinuxLogo.jpg");
25+
src2 = Imgcodecs.imread("../../images/WindowsLogo.jpg");
26+
//! [load]
27+
28+
if( src1.empty() == true ){ System.out.println("Error loading src1"); return;}
29+
if( src2.empty() == true ){ System.out.println("Error loading src2"); return;}
30+
31+
//! [blend_images]
32+
beta = ( 1.0 - alpha );
33+
Core.addWeighted( src1, alpha, src2, beta, 0.0, dst);
34+
//! [blend_images]
35+
36+
//![display]
37+
HighGui.imshow("Linear Blend", dst);
38+
HighGui.waitKey(0);
39+
//![display]
40+
41+
System.exit(0);
42+
}
43+
}
44+
45+
public class AddingImages {
46+
public static void main(String[] args) {
47+
// Load the native library.
48+
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
49+
new AddingImagesRun().run();
50+
}
51+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from __future__ import print_function
2+
import sys
3+
4+
import cv2
5+
6+
alpha = 0.5
7+
8+
print(''' Simple Linear Blender
9+
-----------------------
10+
* Enter alpha [0.0-1.0]: ''')
11+
if sys.version_info >= (3, 0): # If Python 3.x
12+
input_alpha = float(input())
13+
else:
14+
input_alpha = float(raw_input())
15+
if 0 <= alpha <= 1:
16+
alpha = input_alpha
17+
## [load]
18+
src1 = cv2.imread('../../../../data/LinuxLogo.jpg')
19+
src2 = cv2.imread('../../../../data/WindowsLogo.jpg')
20+
## [load]
21+
if src1 is None:
22+
print ("Error loading src1")
23+
exit(-1)
24+
elif src2 is None:
25+
print ("Error loading src2")
26+
exit(-1)
27+
## [blend_images]
28+
beta = (1.0 - alpha)
29+
dst = cv2.addWeighted(src1, alpha, src2, beta, 0.0)
30+
## [blend_images]
31+
## [display]
32+
cv2.imshow('dst', dst)
33+
cv2.waitKey(0)
34+
## [display]
35+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)