|
1 | 1 | Adding (blending) two images using OpenCV {#tutorial_adding_images}
|
2 | 2 | =========================================
|
3 | 3 |
|
| 4 | +@prev_tutorial{tutorial_mat_operations} |
| 5 | +@next_tutorial{tutorial_basic_linear_transform} |
| 6 | + |
4 | 7 | Goal
|
5 | 8 | ----
|
6 | 9 |
|
7 | 10 | In this tutorial you will learn:
|
8 | 11 |
|
9 | 12 | - 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()** |
11 | 14 |
|
12 | 15 | Theory
|
13 | 16 | ------
|
|
28 | 31 | Source Code
|
29 | 32 | -----------
|
30 | 33 |
|
| 34 | +@add_toggle_cpp |
31 | 35 | 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). |
33 | 37 | @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 |
34 | 51 |
|
35 | 52 | Explanation
|
36 | 53 | -----------
|
37 | 54 |
|
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 |
39 | 82 |
|
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 |
41 | 86 |
|
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 |
44 | 94 |
|
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. |
46 | 98 |
|
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 |
49 | 103 |
|
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 |
55 | 107 |
|
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 |
58 | 111 |
|
59 | 112 | Result
|
60 | 113 | ------
|
|
0 commit comments