Leaf.CvLibraryExtensions 1.0.7

dotnet add package Leaf.CvLibraryExtensions --version 1.0.7
                    
NuGet\Install-Package Leaf.CvLibraryExtensions -Version 1.0.7
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Leaf.CvLibraryExtensions" Version="1.0.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Leaf.CvLibraryExtensions" Version="1.0.7" />
                    
Directory.Packages.props
<PackageReference Include="Leaf.CvLibraryExtensions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Leaf.CvLibraryExtensions --version 1.0.7
                    
#r "nuget: Leaf.CvLibraryExtensions, 1.0.7"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Leaf.CvLibraryExtensions@1.0.7
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Leaf.CvLibraryExtensions&version=1.0.7
                    
Install as a Cake Addin
#tool nuget:?package=Leaf.CvLibraryExtensions&version=1.0.7
                    
Install as a Cake Tool

Leaf.CvLibraryExtensions

NuGet License: MIT .NET

基于 Leaf.CvLibrary 的 WPF 扩展功能库,提供 OpenCV Mat 与 WPF 图像格式的高效转换和绘制扩展方法。

📦 安装

通过 NuGet 包管理器安装

dotnet add package Leaf.CvLibraryExtensions

或在 Visual Studio 中使用包管理器控制台:

Install-Package Leaf.CvLibraryExtensions

手动安装依赖

dotnet add package Leaf.CvLibrary

🎯 功能特性

Mat 转换扩展

  • BitmapFrame 转换 - 高效将 Mat 转换为 BitmapFrame
  • WriteableBitmap 转换 - 最高性能的 WPF 图像转换
  • 异步转换支持 - 提供异步转换方法避免 UI 阻塞
  • 线程安全 - 自动处理 Dispatcher 调度

绘制扩展

  • 结果 ROI 绘制 - 快速绘制带状态的感兴趣区域(成功/失败)

🚀 快速开始

示例 1:Mat 转换为 BitmapFrame

using CvLibraryExtensions.OpenCV;
using OpenCvSharp;

// 加载图像
var mat = Cv2.ImRead("image.jpg");

// 转换为 BitmapFrame(同步)
var bitmapFrame = mat.ToBitmapFrame();
if (bitmapFrame != null)
{
    // 绑定到 WPF Image 控件
    myImage.Source = bitmapFrame;
}

示例 2:异步转换为 WriteableBitmap

using CvLibraryExtensions.OpenCV;
using OpenCvSharp;

// 异步加载并转换
var mat = await Task.Run(() => Cv2.ImRead("image.jpg"));
var writeableBitmap = await mat.ToWriteableBitmapAsync();

if (writeableBitmap != null)
{
    // 绑定到 WPF Image 控件
    myImage.Source = writeableBitmap;
}

示例 3:绘制检测结果 ROI

using CvLibraryExtensions.OpenCV;
using CvCommon;
using OpenCvSharp;

var mat = Cv2.ImRead("image.jpg");
var roi = new CvRect(100, 100, 200, 150);
bool detectionSuccess = true;

// 绘制结果 ROI(成功为绿色,失败为红色)
mat.DrawResultROI(roi, detectionSuccess, thickness: 3);

// 显示结果
var bitmapFrame = mat.ToBitmapFrame();
myImage.Source = bitmapFrame;

🏗️ API 文档

MatConverterExtensions 类

方法 描述
ToBitmapFrame(Mat) 同步转换 Mat 为 BitmapFrame
ToBitmapFrameAsync(Mat) 异步转换 Mat 为 BitmapFrame
ToWriteableBitmap(Mat) 同步转换 Mat 为 WriteableBitmap(最高性能)
ToWriteableBitmapAsync(Mat) 异步转换 Mat 为 WriteableBitmap

特点:

  • 避免使用内存流,直接复制像素数据,性能更高
  • 自动处理 WPF Dispatcher 线程调度
  • 支持多种像素格式(Bgr24, Bgra32, Gray8 等)
  • 返回线程安全的冻结位图

DrawROIExtensions 类

方法 描述
DrawResultROI(Mat, CvRect, bool, int) 绘制带状态的感兴趣区域

参数:

  • mat - 目标图像
  • roi - 感兴趣区域
  • isOK - 状态(true=成功/绿色,false=失败/红色)
  • thickness - 线条粗细(默认 2)

📋 系统要求

  • .NET 8.0 for Windows 或更高版本
  • WPF 支持 - 仅支持 Windows 平台
  • 支持平台
    • Windows 10/11 (x64, AnyCPU)

🔧 依赖项

  • Leaf.CvLibrary (>= 1.0.5) - 核心图像处理库
  • OpenCvSharp4.Windows - 通过 CvLibrary 间接依赖

💡 最佳实践

1. 异步转换避免 UI 阻塞

// 推荐:在后台线程处理图像,异步转换到 UI
var processedMat = await Task.Run(() => ProcessImage(sourceMat));
var bitmap = await processedMat.ToWriteableBitmapAsync();
myImage.Source = bitmap;

2. 选择合适的转换方法

  • BitmapFrame - 通用选择,适合大多数场景
  • WriteableBitmap - 需要频繁更新图像时使用,性能最高

3. 线程安全

扩展方法会自动处理 Dispatcher 调度,无需手动调用 Dispatcher.Invoke

🤝 贡献

欢迎提交 Issue 和 Pull Request!

  1. Fork 本仓库
  2. 创建您的特性分支 (git checkout -b feature/AmazingFeature)
  3. 提交您的更改 (git commit -m 'Add some AmazingFeature')
  4. 推送到分支 (git push origin feature/AmazingFeature)
  5. 打开一个 Pull Request

📝 版本历史

v1.0.2 (当前版本)

  • 当前稳定版本

v1.0.1

  • 改进性能和稳定性

v1.0.0

  • 初始发布版本

👤 作者

Mitsuha9527

📄 许可证

本项目采用 MIT 许可证 - 查看 LICENSE 文件了解详情。

本项目使用了以下第三方库:

🔗 相关链接

⭐ 支持

如果这个项目对您有帮助,请给它一个星标 ⭐!


Made with ❤️ by Mitsuha9527

Product Compatible and additional computed target framework versions.
.NET net8.0-windows7.0 is compatible.  net9.0-windows was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.7 94 4/30/2026
1.0.6 110 3/31/2026
1.0.5 113 2/9/2026
1.0.4 118 1/30/2026
1.0.3 117 1/30/2026