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