plcsharp/PLCSharpWpf/VVMs/Vision/VisionFlowHandler/Processing/SplitChannelHandler.cs

45 lines
1.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using OpenCvSharp;
namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Processing
{
/// <summary>
/// 拆分通道Handler
/// </summary>
public class SplitChannelHandler : IVisionFlowHandler
{
/// <summary>
/// 类型
/// </summary>
public VisionFlowType Type => VisionFlowType.取通道;
/// <summary>
/// 执行
/// </summary>
/// <param name="func">视觉功能</param>
/// <param name="item">变量项</param>
/// <returns>返回布尔值</returns>
public bool Execute(VisionFunction func, VisionFlow item)
{
if (func.Src.Channels() == 3)
{
if (item.IntParams.TryGetValue("ChannelIndex", out int _channelIndex))
{
Mat[] channels = Cv2.Split(func.Src);
try
{
// Clone 独立拷贝交给 func.Src 管理,channels 数组整体释放,避免未用通道泄漏
func.Src = channels[_channelIndex].Clone();
}
finally
{
foreach (var ch in channels)
{
ch.Dispose();
}
}
}
}
return true;
}
}
}