无法匹配相同图像尺寸但斑点图案偏移的图像

时间:2019-07-19 10:48:16

标签: c# opencv emgucv aforge accord.net

我正在尝试将图像与模板主图像进行匹配。所有图像的大小相同,但是斑点图案在图像内移动。

我在aforge中尝试了thresholdfilter,当缺陷表中的斑点与模板图像完美对齐时,它可以很好地发现差异。如果差异很小,则所有斑点都被检测为缺陷(不同)。

var img1 = AForge.Imaging.Image.FromFile(@"compare2.jpg");
var img2 = AForge.Imaging.Image.FromFile(@"compare1.jpg");

// (* calculate absolute difference *)
var difference = new AForge.Imaging.Filters.ThresholdedDifference(15)
    {OverlayImage = img1}
    .Apply(img2);

// (* create and initialize the blob counter *)
var bc = new AForge.Imaging.BlobCounter();
bc.FilterBlobs = true;
bc.MinWidth = 5;
bc.MinHeight = 5;

 // (* find blobs *)
 bc.ProcessImage(difference);

 // (* draw result *)
 BitmapData data = img2.LockBits(
   new Rectangle(0, 0, img2.Width, img2.Height),
      ImageLockMode.ReadWrite, img2.PixelFormat);

 Pen redPen = new Pen(Color.Red, 2); 

 foreach (var rc in bc.GetObjectsRectangles())
    AForge.Imaging.Drawing.Rectangle(data, rc, Color.Red);

  img2.UnlockBits(data);
   imageBox1.Image = img2;

主图像: master template

斑点转移” Shifted Blobs

如何对齐两张图像,以使图案始终完美对齐?

1 个答案:

答案 0 :(得分:0)

如果您知道两个图像上的第一个斑点的坐标you can use CanvasMove进行过滤,以使其完全对齐:

// create filter
CanvasMove filter = new CanvasMove(new IntPoint(
    YOUR_X_DIFF, YOUR_Y_DIFF
), Color.Green );
// apply the filter
filter.ApplyInPlace( image );

您还可以查看MoveTowards过滤器,该过滤器试图自动对齐图像(如果我理解正确的话),描述听起来像是有用的:

  

此过滤器的结果是一张图像,该图像基于源图像,   但进行了更新,以减少重叠图像的差异-   源图像移向重叠图像