docs(core): 为 ddddocr-core 全量补充并精简 Rustdoc 文档
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
//! 图像处理算法:OpenCV 风格的常用函数封装。
|
||||
|
||||
use image::{imageops::FilterType, DynamicImage, GrayImage, ImageBuffer, Luma};
|
||||
|
||||
use ndarray::{azip, Array2, Array3, ArrayView2, ArrayView3};
|
||||
use std::cmp::{max, min};
|
||||
|
||||
// 模拟openCV
|
||||
/// 1. 计算两个数组的绝对差值 (对应 cv2.absdiff)
|
||||
/// 计算两个 HWC 数组的绝对差值(对应 cv2.absdiff)。
|
||||
pub fn abs_diff(a: &ArrayView3<u8>, b: &ArrayView3<u8>) -> Array3<u8> {
|
||||
// 利用 ndarray 的 map_collect,生成差值的绝对值数组
|
||||
// 或者直接使用 zip_mut_with 处理以减少内存分配
|
||||
@@ -27,7 +29,7 @@ pub fn rgb_to_gray(rgb: ArrayView3<u8>) -> Array2<u8> {
|
||||
})
|
||||
}
|
||||
|
||||
/// 寻找匹配结果图中的最大值及其坐标 (模拟 cv2.minMaxLoc 的一部分)
|
||||
/// 查找匹配结果图中的最大值及其坐标(对应 cv2.minMaxLoc)。
|
||||
pub fn min_max_loc(result_map: &ImageBuffer<Luma<f32>, Vec<f32>>) -> (f32, (u32, u32)) {
|
||||
// 4. 找到最佳匹配位置 (对齐 cv2.minMaxLoc)
|
||||
let mut max_val: f32 = -1.0;
|
||||
@@ -48,8 +50,7 @@ pub fn min_max_loc(result_map: &ImageBuffer<Luma<f32>, Vec<f32>>) -> (f32, (u32,
|
||||
(max_val, max_loc)
|
||||
}
|
||||
|
||||
/// 1. 模拟 findContours 并获取最大面积区域的 Label
|
||||
/// 返回 Option<u32>,如果找不到任何区域则返回 None
|
||||
/// 模拟 findContours:返回面积最大的连通域标签,找不到时返回 `None`。
|
||||
pub fn find_contours_and_max(labelled: &ImageBuffer<Luma<u32>, Vec<u32>>) -> Option<u32> {
|
||||
// 统计每个标签出现的频率(即面积)
|
||||
let mut max_label = 0;
|
||||
@@ -74,9 +75,7 @@ pub fn find_contours_and_max(labelled: &ImageBuffer<Luma<u32>, Vec<u32>>) -> Opt
|
||||
Some(max_label)
|
||||
}
|
||||
}
|
||||
/// 根据目标连通域标签,计算其在图像中的外接矩形边界框(对应 `cv2.boundingRect`)
|
||||
///
|
||||
/// 返回格式: `(min_x, min_y, width, height)`
|
||||
/// 计算指定连通域标签的外接矩形(对应 cv2.boundingRect),返回 `(min_x, min_y, width, height)`。
|
||||
pub fn bounding_rect(
|
||||
labelled: &ImageBuffer<Luma<u32>, Vec<u32>>,
|
||||
max_label: u32,
|
||||
@@ -109,9 +108,7 @@ pub fn calculate_center(top_left: (u32, u32), width: usize, height: usize) -> (i
|
||||
(center_x, center_y)
|
||||
}
|
||||
|
||||
/// 高性能转换:将 `ndarray` 2D 灰度视图规整为 `image::ImageBuffer` 格式
|
||||
///
|
||||
/// 放弃低效的逐像素显式嵌套循环,采用原生内存池直接构造,减少寻址开销
|
||||
/// 将 2D 灰度 ndarray 视图转换为灰度 ImageBuffer。
|
||||
pub fn ndarray_to_luma8(array: ArrayView2<u8>) -> ImageBuffer<Luma<u8>, Vec<u8>> {
|
||||
let (height, width) = array.dim();
|
||||
// 技巧:直接将已有的规整连续内存打平转换,或用 from_raw 包装
|
||||
@@ -128,6 +125,7 @@ pub fn ndarray_to_luma8(array: ArrayView2<u8>) -> ImageBuffer<Luma<u8>, Vec<u8>>
|
||||
// 5. 核心高性能图像转换算法 (纯 Rust 编写)
|
||||
// =====================================================================
|
||||
|
||||
/// RGB 像素转换为 OpenCV 风格的 HSV 值。
|
||||
#[inline(always)]
|
||||
pub fn rgb_to_opencv_hsv(r: u8, g: u8, b: u8) -> (u8, u8, u8) {
|
||||
// 1. 规避高昂的除法,直接转为 f32 进行比对
|
||||
@@ -174,15 +172,13 @@ pub fn rgb_to_opencv_hsv(r: u8, g: u8, b: u8) -> (u8, u8, u8) {
|
||||
(h_opencv, s_opencv, v_opencv)
|
||||
}
|
||||
|
||||
/// 对应 Python 的 convert_to_grayscale
|
||||
/// 将图像转换为灰度图 (L模式)
|
||||
/// 将图像转换为灰度图(L 模式)。
|
||||
pub fn convert_to_grayscale(image: &DynamicImage) -> GrayImage {
|
||||
// Rust utils 库的 to_luma8 会根据标准的亮度公式进行转换
|
||||
image.to_luma8()
|
||||
}
|
||||
|
||||
/// 对应 Python 的 resize_image
|
||||
/// 调整图像尺寸。当前版本仅实现 keep_aspect_ratio=false
|
||||
/// 按指定宽高调整图像尺寸。
|
||||
pub fn resize_image(
|
||||
image: &DynamicImage,
|
||||
target_width: u32,
|
||||
|
||||
Reference in New Issue
Block a user