0

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
Ben A. Hilleli
  • 586
  • 1
  • 6
  • 21
specter
  • 11
  • 1
  • 6

1 Answers1

0

sub_image returns a mutable view into the image. You obviously cannot have multiple mutable views into the same image due to borrow checker conflicts. So, use GenericImageView::view for an immutable view instead:

// get the trait into scope
use image::GenericImageView;

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)| {
            // use view instead of sub_image here
            gray_image.view(*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();
}
Aplet123
  • 33,825
  • 1
  • 29
  • 55
  • Thank you, the error no longer apear. I avoided to use view because I want to perform operations in the image part but view is immutable as you said – specter Dec 12 '20 at 15:49
  • Then, either perform the operations in the `map` so you only have one mutable reference at a time, or store the coordinates so you can make subimages one at a time later as needed. – Aplet123 Dec 12 '20 at 15:51
  • I don't see how I can do the bluring in the map. https://docs.rs/imageproc/0.22.0/imageproc/filter/fn.box_filter.html. I need a gray_image reference for that – specter Dec 12 '20 at 16:09