I was trying to write code that applies a mask for three rectangulars regions within my image. The goal is to make a vector that contains these regions so that I can blur them later.
Here's my code :
fn main() {
let img = image::open(
"C:/Users/hp/Desktop/Multiprocessor real-time scheduling/Project2/data/aLIEz.jpg",
)
.unwrap();
let mut gray_image = img.to_luma8();
struct Rectangles {
line1: Vec<(u32, u32, u32, u32)>,
line2: Vec<(u32, u32, u32, u32)>,
line3: Vec<(u32, u32, u32, u32)>,
}
let rectangles1 = Rectangles {
line1: vec![(250, 325, 350, 415)],
line2: vec![(225, 500, 350, 615)],
line3: vec![(50, 825, 185, 980)],
};
let img_parts: Vec<_> = rectangles1
.line1
.iter()
.map(|(start_i, start_j, stop_i, stop_j)| {
gray_image.sub_image(*start_i, *start_j, stop_i - start_i, stop_j - start_j)
})
.collect();
gray_image.save("C:/Users/hp/Desktop/Multiprocessor real-time scheduling/Project2/data_output/test12.png").unwrap();
}
Please find the error below :
error: captured variable cannot escape `FnMut` closure body
--> src\main.rs:45:47
|
17 | let mut gray_image = img.to_luma8();
| -------------- variable defined here
...
45 | .map(|(start_i, start_j, stop_i, stop_j)| gray_image.sub_image(*start_i, *start_j, stop_i-start_i, stop_j-start_j)).collect();
| - ----------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| | |
| | returns a reference to a captured variable which escapes the closure body
| | variable captured here
| inferred to be a `FnMut` closure
|
= note: `FnMut` closures only have access to their captured variables while they are executing...
= note: ...therefore, they cannot allow references to captured variables to escape