Galosys.Foundation.OpenCvSharp 26.7.13.1

dotnet add package Galosys.Foundation.OpenCvSharp --version 26.7.13.1
                    
NuGet\Install-Package Galosys.Foundation.OpenCvSharp -Version 26.7.13.1
                    
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="Galosys.Foundation.OpenCvSharp" Version="26.7.13.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Galosys.Foundation.OpenCvSharp" Version="26.7.13.1" />
                    
Directory.Packages.props
<PackageReference Include="Galosys.Foundation.OpenCvSharp" />
                    
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 Galosys.Foundation.OpenCvSharp --version 26.7.13.1
                    
#r "nuget: Galosys.Foundation.OpenCvSharp, 26.7.13.1"
                    
#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 Galosys.Foundation.OpenCvSharp@26.7.13.1
                    
#: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=Galosys.Foundation.OpenCvSharp&version=26.7.13.1
                    
Install as a Cake Addin
#tool nuget:?package=Galosys.Foundation.OpenCvSharp&version=26.7.13.1
                    
Install as a Cake Tool

Galosys.Foundation.OpenCvSharp

成熟度: 🟡 可用 — 功能完整,测试或文档可能不完善

基于 OpenCvSharp 的计算机视觉扩展库,提供人脸检测、图像处理、视频分析等视觉相关的常用功能。

功能概览

1. 人脸检测

using OpenCvSharp;
using OpenCvSharp.Extensions;
using System.Windows.Media.Imaging;

// 加载级联分类器
using var classifier = new CascadeClassifier("haarcascade_frontalface_default.xml");

// 检测人脸
using var gray = new Mat();
Cv2.CvtColor(image, gray, ColorConversionCodes.BGR2GRAY);
var faces = classifier.DetectMultiScale(gray);

foreach (var face in faces)
{
    Cv2.Rectangle(image, face, Scalar.Red, 2);
}

2. 图像处理

using OpenCvSharp;

// 灰度转换
using var gray = new Mat();
Cv2.CvtColor(image, gray, ColorConversionCodes.BGR2GRAY);

// 高斯模糊
using var blurred = new Mat();
Cv2.GaussianBlur(image, blurred, new Size(5, 5), 0);

// 边缘检测
using var edges = new Mat();
Cv2.Canny(gray, edges, 50, 150);

// 二值化
using var binary = new Mat();
Cv2.Threshold(gray, binary, 127, 255, ThresholdTypes.Binary);

3. 形态学操作

using OpenCvSharp;

// 膨胀
using var dilated = new Mat();
var kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
Cv2.Dilate(image, dilated, kernel);

// 腐蚀
using var eroded = new Mat();
Cv2.Erode(image, eroded, kernel);

// 开运算(去噪)
using var opened = new Mat();
Cv2.MorphologyEx(image, opened, MorphTypes.Open, kernel);

// 闭运算(填充)
using var closed = new Mat();
Cv2.MorphologyEx(image, closed, MorphTypes.Close, kernel);

4. 轮廓检测

using OpenCvSharp;

// 查找轮廓
var contours = new Point[][] { };
var hierarchy = new HierarchyIndex[] { };
Cv2.FindContours(binary, out contours, out hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxSimple);

// 绘制轮廓
foreach (var contour in contours)
{
    Cv2.DrawContours(image, new[] { contour }, 0, Scalar.Blue, 2);
}

5. 模板匹配

using OpenCvSharp;

// 模板匹配
using var template = Cv2.ImRead("template.png");
var result = new Mat();
Cv2.MatchTemplate(image, template, result, TemplateMatchModes.CCoeffNormed);

// 找最佳匹配位置
Cv2.MinMaxLoc(result, out _, out _, out var minLoc, out _);
var matchRect = new Rect(minLoc.X, minLoc.Y, template.Width, template.Height);
Cv2.Rectangle(image, matchRect, Scalar.Green, 2);

6. 颜色空间转换

using OpenCvSharp;

// BGR 转 HSV
using var hsv = new Mat();
Cv2.CvtColor(image, hsv, ColorConversionCodes.BGR2HSV);

// 颜色分离(通道提取)
var channels = new Mat[] { };
Cv2.Split(image, out channels);
using var blue = channels[0];
using var green = channels[1];
using var red = channels[2];

7. 图像直方图

using OpenCvSharp;

// 计算直方图
var hist = new Mat();
var channels = new[] { 0 };
var histSize = new[] { 256 };
var ranges = new[] { new RangeF(0, 256) };
Cv2.CalcHist(new[] { gray }, channels, null, hist, 1, histSize, ranges);

// 直方图均衡化
using var equalized = new Mat();
Cv2.EqualizeHist(gray, equalized);

8. 视频流处理

using OpenCvSharp;

// 打开摄像头
using var capture = new VideoCapture(0);
if (!capture.IsOpened()) return;

while (true)
{
    using var frame = new Mat();
    capture.Read(frame);
    if (frame.Empty()) break;
    
    // 处理帧
    Cv2.CvtColor(frame, frame, ColorConversionCodes.BGR2GRAY);
    Cv2.GaussianBlur(frame, frame, new Size(3, 3), 0);
    
    Cv2.imshow("Video", frame);
    if (Cv2.WaitKey(1) == 'q') break;
}

安装

<PackageReference Include="Galosys.Foundation.OpenCvSharp" Version="x.x.x" />

目录结构

Galosys.Foundation.OpenCvSharp/
└── OpenCvSharp\
    └── CascadeClassifierExtensions.cs

依赖

  • OpenCvSharp
  • OpenCvSharp.Extensions
  • OpenCvSharp.WpfExtensions
  • Galosys.Foundation.Core
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos 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
26.7.13.1 26 7/13/2026
26.7.12.1 43 7/12/2026
26.7.11.1 43 7/11/2026
26.7.10.2 39 7/10/2026
26.7.10.1 44 7/10/2026
26.7.7.2 47 7/7/2026
26.7.7.1 42 7/7/2026
26.7.2.1 96 7/2/2026
26.7.1.2 88 7/1/2026
26.4.12.8-rc1 125 4/12/2026
26.4.12.7-rc1 111 4/12/2026
26.4.12.5-rc1 109 4/12/2026
26.1.30.1-rc1 122 1/30/2026
26.1.29.1 143 1/29/2026
26.1.28.5 124 1/28/2026
26.1.28.4 122 1/28/2026
26.1.28.2 128 1/28/2026
26.1.23.6 127 1/23/2026
26.1.21.1 125 1/21/2026
26.1.2.1 145 1/2/2026
Loading failed