Error occurred when converting the image to Array3: ShapeError/IncompatibleShape: incompatible shapes
AAlieZZ opened this issue · comments
Importing Image with image-rs
use std::path::Path;
use image::{io::Reader, imageops};
let img = Reader::open(path)?.decode()?;
let resized_img = imageops::resize(&img, 512, 512, imageops::FilterType::Lanczos3);
let mut data: Vec<u8> = resized_img.into_raw();
Add more images.
for i in 0..255 {
let img = Reader::open(path)?.decode().unwrap();
data.extend(img.to_bytes());
}
to Array3
let inp_data: Array3<f32> = Array3::from_shape_vec((256, 512, 512), data)
.expect("Error converting data to 3D array")
.map(|x| *x as f32/256.0);
error
thread 'main' panicked at 'Error converting data to 3D array: ShapeError/IncompatibleShape: incompatible shapes'
I don't think I filled in the "shape" parameter incorrectly :(
What is the length of of data
? Are you sure that each pixel is a single byte instead of e.g. four for RGB32?
What is the length of of
data
? Are you sure that each pixel is a single byte instead of e.g. four for RGB32?
They're grayscale image, each pixel is typically represented by a single byte of data.
So what is the length of data
just be you call from_shape_vec
?
So what is the length of
data
just be you callfrom_shape_vec
?
...
for i in 0..255 {
let img = Reader::open(path)?.decode().unwrap();
data.extend(img.to_bytes());
}
println!("Data length is {}", data.len());
...
cargo run
Data length is 202375168
I don't understand how the data length can be 202 375 168. You load a 512x512 image 256 times, and you add it in bytes
256 * 512 * 512 * 4 = 268 435 456
With your number
(202375168 / 512) / 512 = 772
772 / 4 = 193
At least one of your number is wrong
I'd suggest checking the intermediate images added to the data array, e.g.
...
for i in 0..255 {
let img = Reader::open(path)?.decode().unwrap();
let bytes = img.to_bytes();
assert_eq(bytes.len(), 512 * 512);
data.extend(bytes);
}
...
Maybe you are just missing a resize operation in the loop?
(If I understand your approach correctly, you would actually expect data
to end up with length 256 * 512 * 512 == 67108864
for single-byte-per-pixel gray scale images.)
I'm also puzzled as to why this number was obtained.
let img = Reader::open(path)?.decode()?;
let resized_img = imageops::resize(&img, 512, 512, imageops::FilterType::Lanczos3);
let data: Vec<u8> = resized_img.into_raw();
println!("{}", data.len());
The length of the individual image turned out to be 1048576
instead of 512 * 512 == 262144
.
The length of the individual image turned out to be 1048576 instead of 512 * 512 == 262144.
1048576 = 512 * 512 * 4, so
Are you sure that each pixel is a single byte instead of e.g. four for RGB32?
as suggested above.
Oops! I got confused. Thanks for your help!