So I take an image and then do some operations over it and then try to save the image back to matrix, here is the code for the same,
let mut final_im_buf = DynamicImage::new_luma8(width as u32, height as u32).to_luma();
for (y, x, pixel) in final_im_buf.enumerate_pixels_mut(){
*pixel = image::Luma([mat[x as usize][y as usize]]);
}
final_im_buf.save("final_redaction.png").unwrap();
The problem is even even though the final result has exactly the same changes as the initial one, the final extracted image turns out sort of tilted. Is there a way to fix it? I was wondering is such a thing happening because I am using a rectangular image (dimension being 1058 x 323).
Note that width here is 1058 and height here is 323
Should I use maybe a square matrix, like sort of reduce the size of image by compressing or something?
I am not sure if I understand your answer. My first point of operation is I have an array, I have to convert it to a matrix form. This I try to do here,
let mut mat = vec![vec![0u8;width as usize];height as usize];
let mut flag = 0;
for i in 0..(height-1){
for j in 0..(width-1){
mat[i as usize][j as usize] = redacted_demo_vec[flag];
flag = flag + 1;
}
}
But, I think I am doing something wrong earlier. So in order to take an image in and then store the black and white pixel values in an array of u8, I do something like this,
let img = open("sample.png").unwrap();
let mut gray = img.to_luma8();
gray.save("grayscale.png").unwrap();
imageops::dither(&mut gray, &BiLevel);
gray.save("grayscale_bicolor.png").unwrap();
let img_arr = gray.as_bytes(); //converting to array of u8
Am I doing it correctly? I am sorry, I am new to working with images and trying to best of my knowledge to work it out as of now. Please let me know, if the functions I am using are in correct format?
There might be an off-by-one error here since a Range is not inclusive of the upper bound. I could see it making a tilted image with the way you read from redacted_demo_vec. Maybe try:
And that's why it's more recommended to use iterator over direct indexing when possible. It's both idiomatic, convenient, less error prone, and potentially faster as it usually guarantees to skip bound check. Try this instead.
Note that I used .for_each() instead of the for loop because I think it's visually better on code to use it after the long iterator chain. The default way to consume iterator would be the for loop.
EDIT: I missed the mat is a newly allocated zeroed vector which allows better construction method.
let mat: Vec<_> = redacted_demo_vec
.chunks_exact(width)
.take(height)
.map(|s| s.to_vec())
.collect();