Skip to content

Commit 45afd29

Browse files
committed
Tutorial Mask Operations on Matrices
1 parent 954e2f9 commit 45afd29

File tree

3 files changed

+64
-88
lines changed

3 files changed

+64
-88
lines changed

doc/tutorials/core/mat-mask-operations/mat_mask_operations.markdown

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,39 @@ the zero-zero index) on the pixel you want to calculate and sum up the pixel val
2828
the overlapped matrix values. It's the same thing, however in case of large matrices the latter
2929
notation is a lot easier to look over.
3030

31+
Code
32+
----
33+
3134
@add_toggle_cpp
32-
Now let us see how we can make this happen by using the basic pixel access method or by using the
33-
@ref cv::filter2D function.
35+
You can download this source code from [here
36+
](https://raw.githubusercontent.com/opencv/opencv/master/samples/cpp/tutorial_code/core/mat_mask_operations/mat_mask_operations.cpp) or look in the
37+
OpenCV source code libraries sample directory at
38+
`samples/cpp/tutorial_code/core/mat_mask_operations/mat_mask_operations.cpp`.
39+
@include samples/cpp/tutorial_code/core/mat_mask_operations/mat_mask_operations.cpp
3440
@end_toggle
3541

3642
@add_toggle_java
37-
Now let us see how we can make this happen by using the basic pixel access method or by using the
38-
**Imgproc.filter2D()** function.
43+
You can download this source code from [here
44+
](https://raw.githubusercontent.com/opencv/opencv/master/samples/java/tutorial_code/core/mat_mask_operations/MatMaskOperations.java) or look in the
45+
OpenCV source code libraries sample directory at
46+
`samples/java/tutorial_code/core/mat_mask_operations/MatMaskOperations.java`.
47+
@include samples/java/tutorial_code/core/mat_mask_operations/MatMaskOperations.java
3948
@end_toggle
4049

4150
@add_toggle_python
42-
Now let us see how we can make this happen by using the basic pixel access method or by using the
43-
**cv2.filter2D()** function.
51+
You can download this source code from [here
52+
](https://raw.githubusercontent.com/opencv/opencv/master/samples/python/tutorial_code/core/mat_mask_operations/mat_mask_operations.py) or look in the
53+
OpenCV source code libraries sample directory at
54+
`samples/python/tutorial_code/core/mat_mask_operations/mat_mask_operations.py`.
55+
@include samples/python/tutorial_code/core/mat_mask_operations/mat_mask_operations.py
4456
@end_toggle
4557

4658
The Basic Method
4759
----------------
4860

61+
Now let us see how we can make this happen by using the basic pixel access method or by using the
62+
**filter2D()** function.
63+
4964
Here's a function that will do this:
5065
@add_toggle_cpp
5166
@snippet samples/cpp/tutorial_code/core/mat_mask_operations/mat_mask_operations.cpp basic_method
@@ -132,37 +147,38 @@ The filter2D function
132147
Applying such filters are so common in image processing that in OpenCV there exist a function that
133148
will take care of applying the mask (also called a kernel in some places). For this you first need
134149
to define an object that holds the mask:
150+
135151
@add_toggle_cpp
136152
@snippet samples/cpp/tutorial_code/core/mat_mask_operations/mat_mask_operations.cpp kern
137-
138-
Then call the @ref cv::filter2D function specifying the input, the output image and the kernel to
139-
use:
140-
@snippet samples/cpp/tutorial_code/core/mat_mask_operations/mat_mask_operations.cpp filter2D
141-
142-
The function even has a fifth optional argument to specify the center of the kernel, a sixth
143-
for adding an optional value to the filtered pixels before storing them in K and a seventh one
144-
for determining what to do in the regions where the operation is undefined (borders).
145153
@end_toggle
146154

147155
@add_toggle_java
148156
@snippet samples/java/tutorial_code/core/mat_mask_operations/MatMaskOperations.java kern
149-
150-
Then call the **Imgproc.filter2D()** function specifying the input, the output image and the kernel to
151-
use:
152-
@snippet samples/java/tutorial_code/core/mat_mask_operations/MatMaskOperations.java filter2D
153-
The function even has a fifth optional argument to specify the center of the kernel, a sixth
154-
for adding an optional value to the filtered pixels before storing them in K and a seventh one
155-
for determining what to do in the regions where the operation is undefined (borders).
156157
@end_toggle
157158

158159
@add_toggle_python
159160
@snippet samples/python/tutorial_code/core/mat_mask_operations/mat_mask_operations.py kern
161+
@end_toggle
160162

161-
Then call the **cv2.filter2D()** function specifying the input, the output image and the kernell to
163+
Then call the **filter2D()** function specifying the input, the output image and the kernel to
162164
use:
165+
166+
@add_toggle_cpp
167+
@snippet samples/cpp/tutorial_code/core/mat_mask_operations/mat_mask_operations.cpp filter2D
168+
@end_toggle
169+
170+
@add_toggle_java
171+
@snippet samples/java/tutorial_code/core/mat_mask_operations/MatMaskOperations.java filter2D
172+
@end_toggle
173+
174+
@add_toggle_python
163175
@snippet samples/python/tutorial_code/core/mat_mask_operations/mat_mask_operations.py filter2D
164176
@end_toggle
165177

178+
The function even has a fifth optional argument to specify the center of the kernel, a sixth
179+
for adding an optional value to the filtered pixels before storing them in K and a seventh one
180+
for determining what to do in the regions where the operation is undefined (borders).
181+
166182
This function is shorter, less verbose and, because there are some optimizations, it is usually faster
167183
than the *hand-coded method*. For example in my test while the second one took only 13
168184
milliseconds the first took around 31 milliseconds. Quite some difference.
@@ -172,22 +188,7 @@ For example:
172188
![](images/resultMatMaskFilter2D.png)
173189

174190
@add_toggle_cpp
175-
You can download this source code from [here
176-
](https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/core/mat_mask_operations/mat_mask_operations.cpp) or look in the
177-
OpenCV source code libraries sample directory at
178-
`samples/cpp/tutorial_code/core/mat_mask_operations/mat_mask_operations.cpp`.
179-
180191
Check out an instance of running the program on our [YouTube
181192
channel](http://www.youtube.com/watch?v=7PF1tAU9se4) .
182193
@youtube{7PF1tAU9se4}
183194
@end_toggle
184-
185-
@add_toggle_java
186-
You can look in the OpenCV source code libraries sample directory at
187-
`samples/java/tutorial_code/core/mat_mask_operations/MatMaskOperations.java`.
188-
@end_toggle
189-
190-
@add_toggle_python
191-
You can look in the OpenCV source code libraries sample directory at
192-
`samples/python/tutorial_code/core/mat_mask_operations/mat_mask_operations.py`.
193-
@end_toggle

samples/java/tutorial_code/core/mat_mask_operations/MatMaskOperations.java

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@
22
import org.opencv.core.CvType;
33
import org.opencv.core.Mat;
44
import org.opencv.core.Scalar;
5+
import org.opencv.highgui.HighGui;
56
import org.opencv.imgcodecs.Imgcodecs;
67
import org.opencv.imgproc.Imgproc;
78

8-
import javax.swing.*;
9-
import java.awt.*;
10-
import java.awt.image.BufferedImage;
11-
import java.awt.image.DataBufferByte;
12-
139
class MatMaskOperationsRun {
1410

1511
public void run(String[] args) {
@@ -31,17 +27,20 @@ public void run(String[] args) {
3127
System.exit(-1);
3228
}
3329

34-
Image img = toBufferedImage(src);
35-
displayImage("Input", img, 0, 200);
30+
HighGui.namedWindow("Input", HighGui.WINDOW_AUTOSIZE);
31+
HighGui.namedWindow("Output", HighGui.WINDOW_AUTOSIZE);
32+
33+
HighGui.imshow( "Input", src );
3634
double t = System.currentTimeMillis();
3735

3836
Mat dst0 = sharpen(src, new Mat());
3937

4038
t = ((double) System.currentTimeMillis() - t) / 1000;
4139
System.out.println("Hand written function time passed in seconds: " + t);
4240

43-
Image img2 = toBufferedImage(dst0);
44-
displayImage("Output", img2, 400, 400);
41+
HighGui.imshow( "Output", dst0 );
42+
HighGui.moveWindow("Output", 400, 400);
43+
HighGui.waitKey();
4544

4645
//![kern]
4746
Mat kern = new Mat(3, 3, CvType.CV_8S);
@@ -58,8 +57,10 @@ public void run(String[] args) {
5857
t = ((double) System.currentTimeMillis() - t) / 1000;
5958
System.out.println("Built-in filter2D time passed in seconds: " + t);
6059

61-
Image img3 = toBufferedImage(dst1);
62-
displayImage("Output", img3, 800, 400);
60+
HighGui.imshow( "Output", dst1 );
61+
62+
HighGui.waitKey();
63+
System.exit(0);
6364
}
6465

6566
//! [basic_method]
@@ -108,38 +109,12 @@ public Mat sharpen(Mat myImage, Mat Result) {
108109
return Result;
109110
}
110111
//! [basic_method]
111-
112-
public Image toBufferedImage(Mat m) {
113-
int type = BufferedImage.TYPE_BYTE_GRAY;
114-
if (m.channels() > 1) {
115-
type = BufferedImage.TYPE_3BYTE_BGR;
116-
}
117-
int bufferSize = m.channels() * m.cols() * m.rows();
118-
byte[] b = new byte[bufferSize];
119-
m.get(0, 0, b); // get all the pixels
120-
BufferedImage image = new BufferedImage(m.cols(), m.rows(), type);
121-
final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
122-
System.arraycopy(b, 0, targetPixels, 0, b.length);
123-
return image;
124-
}
125-
126-
public void displayImage(String title, Image img, int x, int y) {
127-
ImageIcon icon = new ImageIcon(img);
128-
JFrame frame = new JFrame(title);
129-
JLabel lbl = new JLabel(icon);
130-
frame.add(lbl);
131-
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
132-
frame.pack();
133-
frame.setLocation(x, y);
134-
frame.setVisible(true);
135-
}
136112
}
137113

138114
public class MatMaskOperations {
139115
public static void main(String[] args) {
140116
// Load the native library.
141117
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
142-
143-
new MatMaskOperationsRun().run(args); // run code
118+
new MatMaskOperationsRun().run(args);
144119
}
145120
}

samples/python/tutorial_code/core/mat_mask_operations/mat_mask_operations.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
from __future__ import print_function
12
import sys
23
import time
4+
35
import numpy as np
46
import cv2
57

6-
78
## [basic_method]
89
def is_grayscale(my_image):
910
return len(my_image.shape) < 3
@@ -26,7 +27,6 @@ def sharpen(my_image):
2627
height, width, n_channels = my_image.shape
2728

2829
result = np.zeros(my_image.shape, my_image.dtype)
29-
3030
## [basic_method_loop]
3131
for j in range(1, height - 1):
3232
for i in range(1, width - 1):
@@ -36,17 +36,16 @@ def sharpen(my_image):
3636
result[j, i] = saturated(sum_value)
3737
else:
3838
for k in range(0, n_channels):
39-
sum_value = 5 * my_image[j, i, k] - my_image[j + 1, i, k] - my_image[j - 1, i, k] \
40-
- my_image[j, i + 1, k] - my_image[j, i - 1, k]
39+
sum_value = 5 * my_image[j, i, k] - my_image[j + 1, i, k] \
40+
- my_image[j - 1, i, k] - my_image[j, i + 1, k]\
41+
- my_image[j, i - 1, k]
4142
result[j, i, k] = saturated(sum_value)
4243
## [basic_method_loop]
43-
4444
return result
4545
## [basic_method]
4646

47-
4847
def main(argv):
49-
filename = "../data/lena.jpg"
48+
filename = "../../../../data/lena.jpg"
5049

5150
img_codec = cv2.IMREAD_COLOR
5251
if argv:
@@ -57,8 +56,9 @@ def main(argv):
5756
src = cv2.imread(filename, img_codec)
5857

5958
if src is None:
60-
print "Can't open image [" + filename + "]"
61-
print "Usage:\nmat_mask_operations.py [image_path -- default ../data/lena.jpg] [G -- grayscale]"
59+
print("Can't open image [" + filename + "]")
60+
print("Usage:")
61+
print("mat_mask_operations.py [image_path -- default ../../../../data/lena.jpg] [G -- grayscale]")
6262
return -1
6363

6464
cv2.namedWindow("Input", cv2.WINDOW_AUTOSIZE)
@@ -70,7 +70,7 @@ def main(argv):
7070
dst0 = sharpen(src)
7171

7272
t = (time.time() - t) / 1000
73-
print "Hand written function time passed in seconds: %s" % t
73+
print("Hand written function time passed in seconds: %s" % t)
7474

7575
cv2.imshow("Output", dst0)
7676
cv2.waitKey()
@@ -81,13 +81,13 @@ def main(argv):
8181
[-1, 5, -1],
8282
[0, -1, 0]], np.float32) # kernel should be floating point type
8383
## [kern]
84-
8584
## [filter2D]
86-
dst1 = cv2.filter2D(src, -1, kernel) # ddepth = -1, means destination image has depth same as input image
85+
dst1 = cv2.filter2D(src, -1, kernel)
86+
# ddepth = -1, means destination image has depth same as input image
8787
## [filter2D]
8888

8989
t = (time.time() - t) / 1000
90-
print "Built-in filter2D time passed in seconds: %s" % t
90+
print("Built-in filter2D time passed in seconds: %s" % t)
9191

9292
cv2.imshow("Output", dst1)
9393

0 commit comments

Comments
 (0)