我正在学习Rust,并且想执行一些基本的图像操作。我目前正在使用 image::open("path/to/img.jpg").unwrap()
条板箱读取这样的图像:image
。
问题是我希望图像采用特定的2d格式,其中有N=height*weight
行和3列(每种颜色一种)。我找到了 rulinalg
crate ,但无法从rulinalg::matrix::Matrix
对象创建DynamicImage
。我尝试了以下方法:
// Normalize to [0, 1]
let (width, height) = img.dimensions();
let n = width*height;
let tmp = img.as_rgb8().unwrap().to_vec().iter().map(|&e| e as f32 / 255.0).collect::<Vec<f32>>();
let mat = Matrix::new(n, 3, tmp);
所以现在我有了一个n
行和3列的矩阵,但是我不确定它是否是正确的表示形式。那样的话,我的意思是我不确定第一个像素是否包含值mat[[0, 0]], mat[[n, 0]], mat[[n*2, 0]]
。因此,为了对其进行测试,我认为我应该尝试通过使用带有以下代码的
mat
矩阵来重新创建图像:let mut img_buf = image::ImageBuffer::new(width, height);
for i in 0..mat.rows() {
let color0 = (mat[[i, 0]] * 255.0) as u8;
let color1 = (mat[[i, 1]] * 255.0) as u8;
let color2 = (mat[[i, 2]] * 255.0) as u8;
let x = i as u32 /height;
let y = i as u32 - x*height;
let pixel = img_buf.get_pixel_mut(x, y);
// let image = *pixel;
*pixel = image::Rgb([color0, color1, color2]);
}
img_buf.save("./tmp.jpg").unwrap();
但是输出只是噪声(即使颜色结构似乎保持不变)。我尝试了很多事情,但似乎没有任何效果。我还尝试在github上找到类似的“项目”,但是我发现的唯一相关的事情是将过滤器应用于图像,这些过滤器只是从image
crate 调用函数。所需的工作流程
所以,我想要的是以下内容:
rulinalg::matrix::Matrix
)np.log(arr), np.exp(arr), np.sqrt(arr), np.sum(arr, axis=0), np.amax(arr, axis=0)
)原因
我的目标是执行图像分割并减少图像的颜色(通过聚类)。
问题
有人对我如何在使用rust 中实现上述目标有任何想法或指示吗?
最佳答案
遵循@Jmb的评论,输出图像为噪点的原因是因为我弄错了轴。
我只是想发布我所做的事情,以防有人想做类似的事情。请记住,尽管我刚接触 rust ,所以我可能犯了一些错误。
使用rulinalg
如果要使用rulinalg
crate ,则应使用以下命令(0.4.2
版本):
let img = image::open("path/to/img.jpg").unwrap();
let (width, height) = img.dimensions();
let n = (width * height) as usize;
// Normalize image pixels to [0, 1]
let tmp = img.as_rgb8().unwrap().to_vec().iter().map(|&e| e as f32 / 255.0).collect::<Vec<f32>>();
// Reduce dimensions
let mut mat = Matrix::new(n, 3, tmp);
// Change the array values by using some other method
mat = my_processing_method(mat);
// Image buffer for the new image
let mut img_buf = image::ImageBuffer::new(width, height);
for i in 0..mat.rows() {
// Move back to the [0, 255] range
let color0 = (mat[[i, 0]] * 255.0) as u8;
let color1 = (mat[[i, 1]] * 255.0) as u8;
let color2 = (mat[[i, 2]] * 255.0) as u8;
let x = i as u32 % width;
let y = i as u32 / width;
let pixel = img_buf.get_pixel_mut(x, y);
*pixel = image::Rgb([color0, color1, color2]);
}
// Save the updated image
img_buf.save("path/to/new/image.jpg").unwrap();
使用ndarray
和ndarray_image
:为简便起见,您还可以使用
ndarray-image
(0.3.0
版本)以打开图像并将其保存在ndarray::Array3
(3d数组)中。如果要使用ndarray
而不是rulinalg
crate ,则可以执行以下操作:use ndarray::{Array2, Dim};
use ndarray_image::{open_image, save_image, Colors};
let img = open_image("path/to/img.jpg", Colors::Rgb).expect("unable to open input image");
// A vector of 3 spots for each dimension
let sh = img.shape();
let (height, width, colors) = (sh[0] as u32, sh[1] as u32, sh[2]);
let n = (width * height) as usize;
// The dimension of the new 2d array
let new_dim = Dim([n, 3]);
// Normalize to [0, 1] and convert to 1d vector
let img_vec = img.map(|&e| e as f32 / 255_f32).into_raw_vec();
// Convert the 1d vector to the 2d ndarray::Array2
let img_arr = Array2::from_shape_vec(new_dim, img_vec).ok().unwrap();
let mut img_buf = image::ImageBuffer::new(width, height);
for i in 0..img_arr.nrows() {
let color0 = (img_arr[[i, 0]] * 255.0) as u8;
let color1 = (img_arr[[i, 1]] * 255.0) as u8;
let color2 = (img_arr[[i, 2]] * 255.0) as u8;
let x = i as u32 % width;
let y = i as u32 / width;
let pixel = img_buf.get_pixel_mut(x, y);
*pixel = image::Rgb([color0, color1, color2]);
}
img_buf.save("path/to/new/image.jpg").unwrap();
希望这会有所帮助!
关于arrays - 在Rust中将图像转换为Rulinalg矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66125636/