1
1
//! # Create Mask
2
- //! Reference: [opencv/samples/cpp/create_mask.cpp](https://github.com/opencv/opencv/blob/4.x/samples/cpp/create_mask.cpp)
3
-
4
- use opencv:: {
5
- core:: { bitwise_and, find_file, CommandLineParser , Point , Scalar , CV_8UC1 , CV_8UC3 } ,
6
- highgui:: { self , imshow} ,
7
- imgcodecs:: { imread, IMREAD_COLOR } ,
8
- imgproc,
9
- prelude:: * ,
10
- } ;
11
-
12
- use std:: {
13
- env, process,
14
- sync:: {
15
- atomic:: { self , AtomicBool } ,
16
- Arc , Mutex ,
17
- } ,
18
- } ;
2
+ //! Reference: [opencv/samples/cpp/create_mask.cpp](https://github.com/opencv/opencv/blob/4.9.0/samples/cpp/create_mask.cpp)
3
+
4
+ use std:: sync:: atomic:: { AtomicBool , Ordering } ;
5
+ use std:: sync:: { Arc , Mutex } ;
6
+ use std:: { env, process} ;
7
+
8
+ use opencv:: core:: { bitwise_and, find_file, CommandLineParser , Point , Scalar , Vec3b } ;
9
+ use opencv:: highgui:: imshow;
10
+ use opencv:: imgcodecs:: { imread, IMREAD_COLOR } ;
11
+ use opencv:: prelude:: * ;
12
+ use opencv:: { highgui, imgproc, not_opencv_branch_4, opencv_branch_4, Result } ;
13
+
14
+ opencv_branch_4 ! {
15
+ use opencv:: imgproc:: LINE_8 ;
16
+ }
17
+ not_opencv_branch_4 ! {
18
+ use opencv:: core:: LINE_8 ;
19
+ }
19
20
20
21
const SOURCE_WINDOW : & str = "Source image" ;
21
22
@@ -29,13 +30,13 @@ enum DrawingState {
29
30
Resetting ,
30
31
}
31
32
32
- fn main ( ) {
33
+ fn main ( ) -> Result < ( ) > {
33
34
let args: Vec < String > = env:: args ( ) . collect ( ) ;
34
35
let ( argc, argv) = ( args. len ( ) as i32 , args. iter ( ) . map ( |s| s. as_str ( ) ) . collect :: < Vec < & str > > ( ) ) ;
35
36
36
- let mut parser = CommandLineParser :: new ( argc, & argv, "{@input | lena.jpg | input image}" ) . unwrap ( ) ;
37
- parser. about ( "This program demonstrates using mouse events\n " ) . unwrap ( ) ;
38
- parser. print_message ( ) . unwrap ( ) ;
37
+ let mut parser = CommandLineParser :: new ( argc, & argv, "{@input | lena.jpg | input image}" ) ? ;
38
+ parser. about ( "This program demonstrates using mouse events\n " ) ? ;
39
+ parser. print_message ( ) ? ;
39
40
println ! (
40
41
"\n \t left mouse button - set a point to create mask shape\n \
41
42
\t right mouse button - create mask from points\n \
@@ -52,13 +53,13 @@ fn main() {
52
53
. unwrap_or_else ( |_| panic ! ( "Cannot find input_image: {}" , input_image) ) ;
53
54
54
55
let [ src, mut next_frame, mut mask, mut final_img] : [ Mat ; 4 ] ;
55
- src = imread ( & input_image_path, IMREAD_COLOR ) . unwrap ( ) ;
56
+ src = imread ( & input_image_path, IMREAD_COLOR ) ? ;
56
57
if src. empty ( ) {
57
58
eprintln ! ( "Error opening image: {}" , input_image) ;
58
59
process:: exit ( -1 ) ;
59
60
}
60
61
61
- highgui:: named_window ( SOURCE_WINDOW , highgui:: WINDOW_AUTOSIZE ) . unwrap ( ) ;
62
+ highgui:: named_window ( SOURCE_WINDOW , highgui:: WINDOW_AUTOSIZE ) ? ;
62
63
let mouse_event_data = ( highgui:: MouseEventTypes :: EVENT_MOUSEWHEEL , 0 , 0 , 0 ) ;
63
64
let ( mouse_event_data, should_handle_mouse_event) = ( Arc :: new ( Mutex :: new ( mouse_event_data) ) , Arc :: new ( AtomicBool :: new ( false ) ) ) ;
64
65
@@ -71,28 +72,28 @@ fn main() {
71
72
if let Ok ( mut mouse_data) = mouse_data. lock ( ) {
72
73
* mouse_data = ( mouse_event_from_i32 ( event) , x, y, flags) ;
73
74
}
74
- should_handle_mouse_event. store ( true , atomic :: Ordering :: Relaxed ) ;
75
+ should_handle_mouse_event. store ( true , Ordering :: Relaxed ) ;
75
76
}
76
77
} ;
77
78
highgui:: set_mouse_callback ( SOURCE_WINDOW , Some ( Box :: new ( mouse_event_dispatcher) ) ) . expect ( "Cannot set mouse callback" ) ;
78
79
79
- highgui :: imshow ( SOURCE_WINDOW , & src) . unwrap ( ) ;
80
+ imshow ( SOURCE_WINDOW , & src) ? ;
80
81
81
82
let ( mut marker_points, mut drawing_state) = ( Vec :: < Point > :: new ( ) , DrawingState :: Init ) ;
82
83
83
- next_frame = Mat :: zeros_size ( src. size ( ) . unwrap ( ) , CV_8UC3 ) . unwrap ( ) . to_mat ( ) . unwrap ( ) ;
84
+ next_frame = Mat :: zeros_size ( src. size ( ) ? , Vec3b :: opencv_type ( ) ) ? . to_mat ( ) ? ;
84
85
85
86
loop {
86
87
// Press Esc to exit
87
- if highgui:: wait_key ( 10 ) . unwrap ( ) == 27 {
88
- break ;
88
+ if highgui:: wait_key ( 10 ) ? == 27 {
89
+ break Ok ( ( ) ) ;
89
90
}
90
91
91
92
let ( mouse_event, x, y, _) = {
92
- if !should_handle_mouse_event. load ( atomic :: Ordering :: Relaxed ) {
93
+ if !should_handle_mouse_event. load ( Ordering :: Relaxed ) {
93
94
continue ;
94
95
} else {
95
- should_handle_mouse_event. store ( false , atomic :: Ordering :: Relaxed ) ;
96
+ should_handle_mouse_event. store ( false , Ordering :: Relaxed ) ;
96
97
97
98
if let Ok ( mouse_event_data) = mouse_event_data. lock ( ) {
98
99
* mouse_event_data
@@ -102,7 +103,7 @@ fn main() {
102
103
}
103
104
} ;
104
105
105
- drawing_state = self :: state_transform ( drawing_state, mouse_event) ;
106
+ drawing_state = state_transform ( drawing_state, mouse_event) ;
106
107
107
108
match drawing_state {
108
109
DrawingState :: Init | DrawingState :: DrawingMarkerPointFinished => { /* do nothing */ }
@@ -112,16 +113,7 @@ fn main() {
112
113
}
113
114
114
115
let point = Point :: new ( x, y) ;
115
- imgproc:: circle (
116
- & mut next_frame,
117
- point,
118
- 2 ,
119
- Scalar :: new ( 0. , 0. , 255. , 0. ) ,
120
- -1 ,
121
- imgproc:: LINE_8 ,
122
- 0 ,
123
- )
124
- . unwrap ( ) ;
116
+ imgproc:: circle ( & mut next_frame, point, 2 , Scalar :: new ( 0. , 0. , 255. , 0. ) , -1 , LINE_8 , 0 ) ?;
125
117
marker_points. push ( point) ;
126
118
127
119
if marker_points. len ( ) > 1 {
@@ -131,60 +123,50 @@ fn main() {
131
123
point,
132
124
Scalar :: new ( 0. , 0. , 255. , 0. ) ,
133
125
2 ,
134
- imgproc :: LINE_8 ,
126
+ LINE_8 ,
135
127
0 ,
136
- )
137
- . unwrap ( ) ;
128
+ ) ?;
138
129
}
139
130
140
- imshow ( SOURCE_WINDOW , & next_frame) . unwrap ( ) ;
131
+ imshow ( SOURCE_WINDOW , & next_frame) ? ;
141
132
}
142
133
DrawingState :: DrawingMask => {
143
134
if !marker_points. is_empty ( ) {
144
135
next_frame = src. clone ( ) ;
145
136
146
137
imgproc:: polylines (
147
138
& mut next_frame,
148
- & Mat :: from_slice ( marker_points. as_slice ( ) ) . unwrap ( ) ,
139
+ & Mat :: from_slice ( marker_points. as_slice ( ) ) ? ,
149
140
true ,
150
141
Scalar :: new ( 0. , 0. , 0. , 0. ) ,
151
142
2 ,
152
- imgproc :: LINE_8 ,
143
+ LINE_8 ,
153
144
0 ,
154
- )
155
- . unwrap ( ) ;
145
+ ) ?;
156
146
157
- imshow ( SOURCE_WINDOW , & next_frame) . unwrap ( ) ;
147
+ imshow ( SOURCE_WINDOW , & next_frame) ? ;
158
148
}
159
149
}
160
150
DrawingState :: DrawingMaskFinished => {
161
151
if !marker_points. is_empty ( ) {
162
- final_img = Mat :: zeros_size ( src. size ( ) . unwrap ( ) , CV_8UC3 ) . unwrap ( ) . to_mat ( ) . unwrap ( ) ;
163
- mask = Mat :: zeros_size ( src. size ( ) . unwrap ( ) , CV_8UC1 ) . unwrap ( ) . to_mat ( ) . unwrap ( ) ;
164
-
165
- imgproc:: fill_poly (
166
- & mut mask,
167
- & Mat :: from_slice ( marker_points. as_slice ( ) ) . unwrap ( ) ,
168
- Scalar :: new ( 255. , 255. , 255. , 255. ) ,
169
- imgproc:: LINE_8 ,
170
- 0 ,
171
- Point :: default ( ) ,
172
- )
173
- . unwrap ( ) ;
152
+ final_img = Mat :: zeros_size ( src. size ( ) ?, Vec3b :: opencv_type ( ) ) ?. to_mat ( ) ?;
153
+ mask = Mat :: zeros_size ( src. size ( ) ?, u8:: opencv_type ( ) ) ?. to_mat ( ) ?;
154
+
155
+ imgproc:: fill_poly_def ( & mut mask, & Mat :: from_slice ( marker_points. as_slice ( ) ) ?, Scalar :: all ( 255. ) ) ?;
174
156
175
- bitwise_and ( & src, & src, & mut final_img, & mask) . unwrap ( ) ;
157
+ bitwise_and ( & src, & src, & mut final_img, & mask) ? ;
176
158
177
- imshow ( "Mask" , & mask) . unwrap ( ) ;
178
- imshow ( "Result" , & final_img) . unwrap ( ) ;
179
- imshow ( SOURCE_WINDOW , & next_frame) . unwrap ( ) ;
159
+ imshow ( "Mask" , & mask) ? ;
160
+ imshow ( "Result" , & final_img) ? ;
161
+ imshow ( SOURCE_WINDOW , & next_frame) ? ;
180
162
}
181
163
}
182
164
DrawingState :: Resetting => {
183
165
if !marker_points. is_empty ( ) {
184
166
marker_points. clear ( ) ;
185
167
next_frame = src. clone ( ) ;
186
168
187
- imshow ( SOURCE_WINDOW , & next_frame) . unwrap ( ) ;
169
+ imshow ( SOURCE_WINDOW , & next_frame) ? ;
188
170
}
189
171
}
190
172
}
@@ -196,9 +178,9 @@ fn main() {
196
178
/// # Panics
197
179
///
198
180
/// Panics if the argument less than 0 or greater than 11.
199
- fn mouse_event_from_i32 ( value : i32 ) -> opencv :: highgui:: MouseEventTypes {
200
- ( value. gt ( & ( opencv :: highgui:: MouseEventTypes :: EVENT_MOUSEHWHEEL as i32 /* 11 */ ) )
201
- || ( value. lt ( & ( opencv :: highgui:: MouseEventTypes :: EVENT_MOUSEMOVE as i32 /* 0 */ ) ) ) )
181
+ fn mouse_event_from_i32 ( value : i32 ) -> highgui:: MouseEventTypes {
182
+ ( value. gt ( & ( highgui:: MouseEventTypes :: EVENT_MOUSEHWHEEL as i32 /* 11 */ ) )
183
+ || ( value. lt ( & ( highgui:: MouseEventTypes :: EVENT_MOUSEMOVE as i32 /* 0 */ ) ) ) )
202
184
. then ( || panic ! ( "Invalid cv::highgui::MouseEventTypes value: {}" , value) ) ;
203
185
204
186
// Safe because of the previous check
0 commit comments