Read image as Array2<Complex<f64>>

Hallo,
I've been trying to write a function to load an image as Array2<Complex> for a while.

#[derive(Clone, Debug)]
pub struct Field {
    pub values: Array2<Complex<f64>>,
}
fn load_image() -> Field {
    
    let img = image::open("file.png").expect("File not found!"); 

    // set every pixel to Complex::new(img_value, 0.0);
   // the Library needs a complex vector
   let mask = ...

    Field {
        values: mask,
    }
}

Maybe give something like this a shot? I'm not sure if my pixel decoding is correct and haven't tested it:

let width: usize = img.width().try_into().unwrap();
let height: usize = img.height().try_into().unwrap();

let mut mask: Array2<Complex64> = Array2::zeros([width, height]);

for (x, y, pixel) in img.pixels() {
    let x: usize = x.try_into().unwrap();
    let y: usize = y.try_into().unwrap();

    // PNG is big endian, right?
    let re: f64 = u32::from_be_bytes(pixel.0).into();

    mask[[x, y]] = Complex64::new(re, 0.0);
}

Playground.

2 Likes

Thank you jofas.

I hav tested it an i get errors I can not solve.

use image::GenericImageView;
use ndarray::{Array2};
use num_complex::{Complex, Complex64};

pub struct Field {
    pub values: Array2<Complex<f64>>,
}

pub fn main() {
    load_image();
}

fn load_image() -> Field {
    let img = image::open("file.png").expect("File not found!");

    let width: usize = img.width().try_into().unwrap();
    let height: usize = img.height().try_into().unwrap();

    let mut mask: Array2<Complex64> = Array2::zeros([width, height]);

    for (x, y, pixel) in img.pixels() {
        let x: usize = x.try_into().unwrap();
        let y: usize = y.try_into().unwrap();
    }

    // PNG is big endian, right?
    let re: f64 = u32::from_be_bytes(pixel.0).into();

    mask[[x, y]] = Complex64::new(re, 0.0);
    Field {
        values: mask,
    }
}

Compiler output:

error[E0425]: cannot find value `pixel` in this scope
  --> src/main.rs:27:38
   |
27 |     let re: f64 = u32::from_be_bytes(pixel.0).into();
   |                                      ^^^^^ not found in this scope

error[E0425]: cannot find value `x` in this scope
  --> src/main.rs:29:11
   |
29 |     mask[[x, y]] = Complex64::new(re, 0.0);
   |           ^
   |
help: the binding `x` is available in a different scope in the same function
  --> src/main.rs:22:13
   |
22 |         let x: usize = x.try_into().unwrap();
   |             ^

error[E0425]: cannot find value `y` in this scope
  --> src/main.rs:29:14
   |
29 |     mask[[x, y]] = Complex64::new(re, 0.0);
   |              ^
   |
help: the binding `y` is available in a different scope in the same function
  --> src/main.rs:23:13
   |
23 |         let y: usize = y.try_into().unwrap();
   |             ^

Can you help me to get further? I have to say that I have some programming experience, but this is my first Rust project.

Thanks!

Sure, it's an easy problem to fix. you just need to add the two lines that give you compile errors into the loop, which you accidentally closed too early:

    for (x, y, pixel) in img.pixels() {
        let x: usize = x.try_into().unwrap();
        let y: usize = y.try_into().unwrap();
-   }

    // PNG is big endian, right?
    let re: f64 = u32::from_be_bytes(pixel.0).into();

    mask[[x, y]] = Complex64::new(re, 0.0);
+   }

Playground.

1 Like

Thank you, my fault.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.