BitmapSource→Matへの変換
パッケージのインストール
dotnet add package OpenCvSharp4
dotnet add package OpenCvSharp4.runtime.win
dotnet add package OpenCvSharp4.WpfExtensions
サンプルコード
プロジェクトファイル
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0-Windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="OpenCvSharp4" Version="4.11.0.20250507" />
<PackageReference Include="OpenCvSharp4.runtime.win" Version="4.11.0.20250507" />
<PackageReference Include="OpenCvSharp4.WpfExtensions" Version="4.11.0.20250507" />
</ItemGroup>
</Project>
ファイル名:Program.cs
using OpenCvSharp;
using OpenCvSharp.WpfExtensions;
using System.Windows.Media.Imaging;
// BitmapSource → Mat
BitmapSource bitmapSource = new BitmapImage(new Uri("sample.png", UriKind.RelativeOrAbsolute));
Mat mat = bitmapSource.ToMat();
// Mat → BitmapSource
BitmapSource backToBitmap = mat.ToBitmapSource();


コメント