两图的差
This commit is contained in:
parent
50f15ad271
commit
2d9639bfae
@ -44,6 +44,7 @@ public partial class VisionConfigViewModel
|
||||
{
|
||||
case VisionFlowType.从全局图像获取图片:
|
||||
case VisionFlowType.存到全局图像:
|
||||
case VisionFlowType.图像相减:
|
||||
ContentRegion = new ImagesPool();
|
||||
break;
|
||||
case VisionFlowType.从局部图像获取图片:
|
||||
@ -410,6 +411,8 @@ public partial class VisionConfigViewModel
|
||||
newFlow.DoubleParams["ClipLimit"] = 2.0;
|
||||
newFlow.IntParams["TileSize"] = 8;
|
||||
break;
|
||||
case VisionFlowType.图像相减:
|
||||
break;
|
||||
case VisionFlowType.ROI解码:
|
||||
newFlow.IntParams["DecodeType"] = 0;
|
||||
newFlow.BoolParams["EnableMirror"] = false;
|
||||
|
||||
@ -0,0 +1,52 @@
|
||||
using OpenCvSharp;
|
||||
|
||||
namespace PLCSharp.VVMs.Vision.VisionFlowHandler.Processing
|
||||
{
|
||||
/// <summary>
|
||||
/// 图像相减 — func.Src 与全局图像做绝对差值。
|
||||
/// 常用于缺陷检测、变化检测。
|
||||
/// </summary>
|
||||
public class ImageSubtractHandler : IVisionFlowHandler
|
||||
{
|
||||
public VisionFlowType Type => VisionFlowType.图像相减;
|
||||
|
||||
public bool Execute(VisionFunction func, VisionFlow item)
|
||||
{
|
||||
var src = func.Src;
|
||||
if (src == null || src.Empty()) { item.Flow.ErrorState = "请先获取图片!"; return false; }
|
||||
|
||||
if (!item.StringParams.TryGetValue("Image", out var imageName) || string.IsNullOrEmpty(imageName))
|
||||
{ item.Flow.ErrorState = "请选择要相减的全局图像!"; return false; }
|
||||
|
||||
var imageData = func.ImageDatas.FirstOrDefault(i => i.Name == imageName);
|
||||
if (imageData?.Mat == null || imageData.Mat.Empty())
|
||||
{ item.Flow.ErrorState = $"全局图像池中未找到图片:{imageName}"; return false; }
|
||||
|
||||
using Mat other = imageData.Mat;
|
||||
|
||||
// 尺寸不一致时,取交集区域
|
||||
int w = Math.Min(src.Width, other.Width);
|
||||
int h = Math.Min(src.Height, other.Height);
|
||||
using Mat srcRoi = src[new OpenCvSharp.Rect(0, 0, w, h)];
|
||||
using Mat otherRoi = other[new OpenCvSharp.Rect(0, 0, w, h)];
|
||||
|
||||
// 统一为灰度做差值
|
||||
using Mat graySrc = new();
|
||||
using Mat grayOther = new();
|
||||
Cv2.CvtColor(srcRoi, graySrc, ColorConversionCodes.BGR2GRAY);
|
||||
Cv2.CvtColor(otherRoi, grayOther, ColorConversionCodes.BGR2GRAY);
|
||||
|
||||
// 绝对差值
|
||||
Cv2.Absdiff(graySrc, grayOther, graySrc);
|
||||
|
||||
// 写回 func.Src(BGR 格式)
|
||||
var oldSrc = func.Src;
|
||||
func.Src = new Mat();
|
||||
Cv2.CvtColor(graySrc, func.Src, ColorConversionCodes.GRAY2BGR);
|
||||
oldSrc.Dispose();
|
||||
|
||||
item.Flow.Done = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -383,6 +383,7 @@ namespace PLCSharp.VVMs.Vision
|
||||
[VisionFlowType.图像旋转] = new ImageRotateHandler(),
|
||||
[VisionFlowType.ROI剪切] = new ROICropHandler(),
|
||||
[VisionFlowType.直方图均衡化] = new HistogramEqualizeHandler(),
|
||||
[VisionFlowType.图像相减] = new ImageSubtractHandler(),
|
||||
|
||||
};
|
||||
[NotMapped]
|
||||
|
||||
@ -56,6 +56,9 @@
|
||||
[FlowCategory("图像处理")]
|
||||
直方图均衡化 = 22,
|
||||
|
||||
[FlowCategory("图像处理")]
|
||||
图像相减 = 23,
|
||||
|
||||
[FlowCategory("图像处理")]
|
||||
坐标转换 = 18,
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user