日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

rust(50)-图像(3)

發布時間:2025/3/12 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 rust(50)-图像(3) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

DynamicImage

DynamicImage是所有受支持的ImageBuffer

類型的枚舉。它的確切圖像類型是在運行時確定的。它是打開圖像時返回的類型。為了方便,動態圖像重新實現了所有的圖像處理功能。
DynamicImage實現RGBA像素的一般圖像特征。

SubImage
以矩形坐標為界的另一幅圖像的視圖。它用于在圖像的子區域上執行圖像處理功能。

extern crate image;use image::{GenericImageView, ImageBuffer, RgbImage, imageops};let mut img: RgbImage = ImageBuffer::new(512, 512); let subimg = imageops::crop(&mut img, 0, 0, 100, 100);assert!(subimg.dimensions() == (100, 100));

這些是在imageops模塊中定義的函數。所有函數都對實現GenericImage trait.的類型進行操作。
blur: 模糊:對提供的圖像執行高斯模糊。
brighten: 變亮:變亮提供的圖像
huerotate:色調將提供的圖像按程度旋轉
contrast 對比度:調整提供的圖像的對比度
crop:裁剪:將一個可變的視圖返回到一個圖像中
filter3x3:對提供的圖像執行一個3x3框式過濾器。
flip_horizontal: 水平翻轉:水平翻轉圖像
flip_vertical:將圖像垂直翻轉
grayscale: 灰度:將提供的圖像轉換為灰度
invert: 反轉:反轉提供的圖像中的每個像素。
resize:調整大小:將提供的圖像調整到指定的尺寸
rotate180:順時針旋轉圖像180度。
rotate旋轉270:順時針旋轉圖像270度。
rotate90:順時針旋轉圖像90度。
unsharpen: 未銳化:對提供的映像執行未銳化掩碼

打開和保存圖像
image提供用于從路徑打開圖像的open函數。圖像格式由路徑的文件擴展名決定。io模塊提供了一個提供更多控制的閱讀器。

extern crate image;use image::GenericImageView;fn main() {// Use the open function to load an image from a Path.// `open` returns a `DynamicImage` on success.let img = image::open("tests/images/jpg/progressive/cat.jpg").unwrap();// The dimensions method returns the images width and height.println!("dimensions {:?}", img.dimensions());// The color method returns the image's `ColorType`.println!("{:?}", img.color());// Write the contents of this image to the Writer in PNG format.img.save("test.png").unwrap(); }

生成的分形

//! An example of generating julia fractals. extern crate image; extern crate num_complex;fn main() {let imgx = 800;let imgy = 800;let scalex = 3.0 / imgx as f32;let scaley = 3.0 / imgy as f32;// Create a new ImgBuf with width: imgx and height: imgylet mut imgbuf = image::ImageBuffer::new(imgx, imgy);// Iterate over the coordinates and pixels of the imagefor (x, y, pixel) in imgbuf.enumerate_pixels_mut() {let r = (0.3 * x as f32) as u8;let b = (0.3 * y as f32) as u8;*pixel = image::Rgb([r, 0, b]);}// A redundant loop to demonstrate reading image datafor x in 0..imgx {for y in 0..imgy {let cx = y as f32 * scalex - 1.5;let cy = x as f32 * scaley - 1.5;let c = num_complex::Complex::new(-0.4, 0.6);let mut z = num_complex::Complex::new(cx, cy);let mut i = 0;while i < 255 && z.norm() <= 2.0 {z = z * z + c;i += 1;}let pixel = imgbuf.get_pixel_mut(x, y);let image::Rgb(data) = *pixel;*pixel = image::Rgb([data[0], i as u8, data[2]]);}}// Save the image as “fractal.png”, the format is deduced from the pathimgbuf.save("fractal.png").unwrap(); }

寫原始緩沖區
如果由于圖像是通過其他方式獲得的,因此不需要高級接口,則image提供save_buffer函數來將緩沖區保存到文件中。

extern crate image;fn main() {let buffer: &[u8] = unimplemented!(); // Generate the image data// Save the buffer as "image.png"image::save_buffer("image.png", buffer, 800, 600, image::ColorType::Rgb8).unwrap() }

總結

以上是生活随笔為你收集整理的rust(50)-图像(3)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。