1
- import org .opencv .core .*;
1
+ import org .opencv .core .Core ;
2
+ import org .opencv .core .CvType ;
3
+ import org .opencv .core .Mat ;
4
+ import org .opencv .core .Scalar ;
2
5
import org .opencv .imgcodecs .Imgcodecs ;
3
6
import org .opencv .imgproc .Imgproc ;
4
7
5
8
import javax .swing .*;
6
- import java .awt .Image ;
9
+ import java .awt .* ;
7
10
import java .awt .image .BufferedImage ;
8
11
import java .awt .image .DataBufferByte ;
9
12
10
13
class MatMaskOperationsRun {
11
14
12
- public void run () {
15
+ public void run (String [] args ) {
13
16
14
- //! [laod_image]
15
- Mat I = Imgcodecs .imread ("../data/lena.jpg" );
16
- if (I .empty ())
17
- System .out .println ("Error opening image" );
18
- //! [laod_image]
17
+ String filename = "../data/lena.jpg" ;
19
18
20
- Image img = toBufferedImage ( I );
21
- displayImage ("Input Image" , img , 0 , 200 );
19
+ int img_codec = Imgcodecs .IMREAD_COLOR ;
20
+ if (args .length != 0 ) {
21
+ filename = args [0 ];
22
+ if (args .length >= 2 && args [1 ].equals ("G" ))
23
+ img_codec = Imgcodecs .IMREAD_GRAYSCALE ;
24
+ }
25
+
26
+ Mat src = Imgcodecs .imread (filename , img_codec );
22
27
28
+ if (src .empty ()) {
29
+ System .out .println ("Can't open image [" + filename + "]" );
30
+ System .out .println ("Program Arguments: [image_path -- default ../data/lena.jpg] [G -- grayscale]" );
31
+ System .exit (-1 );
32
+ }
33
+
34
+ Image img = toBufferedImage (src );
35
+ displayImage ("Input" , img , 0 , 200 );
23
36
double t = System .currentTimeMillis ();
24
37
25
- Mat J = sharpen (I , new Mat ());
38
+ Mat dst0 = sharpen (src , new Mat ());
26
39
27
- t = ((double )System .currentTimeMillis () - t )/ 1000 ;
28
- System .out .println ("Hand written function times passed in seconds: " + t );
40
+ t = ((double ) System .currentTimeMillis () - t ) / 1000 ;
41
+ System .out .println ("Hand written function time passed in seconds: " + t );
29
42
30
- Image img2 = toBufferedImage ( J );
31
- displayImage ("Output Image" , img2 , 400 , 400 );
43
+ Image img2 = toBufferedImage (dst0 );
44
+ displayImage ("Output" , img2 , 400 , 400 );
32
45
33
- Mat K = new Mat ();
34
- //![kern]
35
- Mat kern = new Mat ( 3 , 3 , CvType .CV_8S );
46
+ //![kern]
47
+ Mat kern = new Mat (3 , 3 , CvType .CV_8S );
36
48
int row = 0 , col = 0 ;
37
- kern .put (row ,col , 0 , -1 , 0 , -1 , 5 , -1 , 0 , -1 , 0 );
38
- //![kern]
39
-
40
- System .out .println ("kern = \n " + kern .dump ());
49
+ kern .put (row , col , 0 , -1 , 0 , -1 , 5 , -1 , 0 , -1 , 0 );
50
+ //![kern]
41
51
42
52
t = System .currentTimeMillis ();
43
53
44
- //![filter2D]
45
- Imgproc .filter2D (I , K , I .depth (), kern );
46
- //![filter2D]
54
+ Mat dst1 = new Mat ();
55
+ //![filter2D]
56
+ Imgproc .filter2D (src , dst1 , src .depth (), kern );
57
+ //![filter2D]
58
+ t = ((double ) System .currentTimeMillis () - t ) / 1000 ;
59
+ System .out .println ("Built-in filter2D time passed in seconds: " + t );
47
60
48
- t = ((double )System .currentTimeMillis () - t )/1000 ;
49
- System .out .println ("Built-in filter2D time passed in seconds: " + t );
50
-
51
- Image img3 = toBufferedImage ( J );
52
- displayImage ("filter2D Output Image" , img3 , 800 , 400 );
61
+ Image img3 = toBufferedImage (dst1 );
62
+ displayImage ("Output" , img3 , 800 , 400 );
53
63
}
54
64
55
65
//! [basic_method]
56
- public static double saturateCastUchar (double x ) {
66
+ public static double saturate (double x ) {
57
67
return x > 255.0 ? 255.0 : (x < 0.0 ? 0.0 : x );
58
68
}
59
69
60
- public Mat sharpen (Mat myImage , Mat Result )
61
- {
62
- //! [8_bit]
70
+ public Mat sharpen (Mat myImage , Mat Result ) {
71
+ //! [8_bit]
63
72
myImage .convertTo (myImage , CvType .CV_8U );
64
- //! [8_bit]
73
+ //! [8_bit]
65
74
66
- //! [create_channels]
75
+ //! [create_channels]
67
76
int nChannels = myImage .channels ();
68
- Result .create (myImage .size (),myImage .type ());
69
- //! [create_channels]
70
-
71
- //! [basic_method_loop]
72
- for (int j = 1 ; j < myImage .rows ()-1 ; ++j )
73
- {
74
- for (int i = 1 ; i < myImage .cols ()-1 ; ++i )
75
- {
77
+ Result .create (myImage .size (), myImage .type ());
78
+ //! [create_channels]
79
+
80
+ //! [basic_method_loop]
81
+ for (int j = 1 ; j < myImage .rows () - 1 ; ++j ) {
82
+ for (int i = 1 ; i < myImage .cols () - 1 ; ++i ) {
76
83
double sum [] = new double [nChannels ];
77
84
78
- for (int k = 0 ; k < nChannels ; ++k ) {
85
+ for (int k = 0 ; k < nChannels ; ++k ) {
79
86
80
87
double top = -myImage .get (j - 1 , i )[k ];
81
88
double bottom = -myImage .get (j + 1 , i )[k ];
82
89
double center = (5 * myImage .get (j , i )[k ]);
83
- double left = -myImage .get (j , i - 1 )[k ];
84
- double right = -myImage .get (j , i + 1 )[k ];
90
+ double left = -myImage .get (j , i - 1 )[k ];
91
+ double right = -myImage .get (j , i + 1 )[k ];
85
92
86
- sum [k ] = saturateCastUchar (top + bottom + center + left + right );
93
+ sum [k ] = saturate (top + bottom + center + left + right );
87
94
}
88
95
89
96
Result .put (j , i , sum );
90
97
}
91
98
}
92
- //! [basic_method_loop]
99
+ //! [basic_method_loop]
93
100
94
- //! [borders]
101
+ //! [borders]
95
102
Result .row (0 ).setTo (new Scalar (0 ));
96
- Result .row (Result .rows ()- 1 ).setTo (new Scalar (0 ));
103
+ Result .row (Result .rows () - 1 ).setTo (new Scalar (0 ));
97
104
Result .col (0 ).setTo (new Scalar (0 ));
98
- Result .col (Result .cols ()- 1 ).setTo (new Scalar (0 ));
99
- //! [borders]
105
+ Result .col (Result .cols () - 1 ).setTo (new Scalar (0 ));
106
+ //! [borders]
100
107
101
108
return Result ;
102
109
}
103
110
//! [basic_method]
104
111
105
112
public Image toBufferedImage (Mat m ) {
106
113
int type = BufferedImage .TYPE_BYTE_GRAY ;
107
- if ( m .channels () > 1 ) {
114
+ if (m .channels () > 1 ) {
108
115
type = BufferedImage .TYPE_3BYTE_BGR ;
109
116
}
110
- int bufferSize = m .channels ()* m .cols ()* m .rows ();
111
- byte [] b = new byte [bufferSize ];
112
- m .get (0 ,0 , b ); // get all the pixels
113
- BufferedImage image = new BufferedImage (m .cols (),m .rows (), type );
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 );
114
121
final byte [] targetPixels = ((DataBufferByte ) image .getRaster ().getDataBuffer ()).getData ();
115
122
System .arraycopy (b , 0 , targetPixels , 0 , b .length );
116
123
return image ;
117
124
}
118
125
119
- public void displayImage (String title , Image img , int x , int y )
120
- {
121
- ImageIcon icon =new ImageIcon (img );
122
- JFrame frame =new JFrame (title );
123
- JLabel lbl =new JLabel (icon );
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 );
124
130
frame .add (lbl );
125
131
frame .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
126
132
frame .pack ();
@@ -131,9 +137,9 @@ public void displayImage(String title, Image img, int x, int y)
131
137
132
138
public class MatMaskOperations {
133
139
public static void main (String [] args ) {
134
-
135
140
// Load the native library.
136
141
System .loadLibrary (Core .NATIVE_LIBRARY_NAME );
137
- new MatMaskOperationsRun ().run ();
142
+
143
+ new MatMaskOperationsRun ().run (args ); // run code
138
144
}
139
145
}
0 commit comments