一杯清酒邀明月
天下本无事,庸人扰之而烦耳。
posts - 3121,comments - 209,views - 578万

将四张图拼接在一起,新建作业,在配置中新建C#脚本,添加代码如下

复制代码
  1 using System;
  2 using System.Threading;
  3 using System.Windows.Forms;
  4 using Cognex.VisionPro;
  5 using Cognex.VisionPro.QuickBuild;
  6 using Cognex.VisionPro.ImageProcessing;
  7  
  8 public class UserScript : CogJobBaseScript
  9 {
 10   private CogCopyRegionTool  imageStitcher;
 11   private int                Counter;
 12   
 13  
 14 #region "When an Acq Fifo Has Been Constructed and Assigned To The Job"
 15   // This function is called when a new fifo is assigned to the job.  This usually
 16   // occurs when the "Initialize Acquisition" button is pressed on the image source
 17   // control.  This function is where you would perform custom setup associated
 18   // with the fifo.
 19   public override void AcqFifoConstruction(Cognex.VisionPro.ICogAcqFifo fifo)
 20   {
 21   }
 22 #endregion
 23  
 24 #region "When an Acquisition is About To Be Started"
 25   // Called before an acquisition is started for manual and semi-automatic trigger
 26   // models.  If "Number of Software Acquisitions Pre-queued" is set to 1 in the
 27   // job configuration, then no acquisitions should be in progress when this
 28   // function is called.
 29   public override void PreAcquisition()
 30   {
 31     // To let the execution stop in this script when a debugger is attached, uncomment the following lines.
 32     // #if DEBUG
 33     // if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();
 34     // #endif
 35  
 36   }
 37 #endregion
 38  
 39 #region "When an Acquisition Has Just Completed"
 40   // Called immediately after an acquisition has completed.
 41   // Return true if the image should be inspected.
 42   // Return false to skip the inspection and acquire another image.
 43   public override bool PostAcquisitionRefInfo(ref Cognex.VisionPro.ICogImage image,
 44                                                   Cognex.VisionPro.ICogAcqInfo info)
 45   {
 46     // To let the execution stop in this script when a debugger is attached, uncomment the following lines.
 47     // #if DEBUG
 48     // if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();
 49     // #endif
 50     
 51     Counter = Counter + 1;
 52     
 53     if(Counter == 1)
 54     {
 55       //Create a new tool
 56       imageStitcher = new CogCopyRegionTool();
 57       
 58       //Create a destination image and assign it to the tool
 59       CogImage8Grey stitchedImage = new CogImage8Grey();
 60       stitchedImage.Allocate(image.Width * 2, image.Height * 2);
 61       imageStitcher.DestinationImage = stitchedImage;
 62       
 63       imageStitcher.Region = null;
 64       imageStitcher.RunParams.ImageAlignmentEnabled = true;
 65       
 66       //First sub-image goes into the upper left corner
 67       imageStitcher.RunParams.DestinationImageAlignmentX = 0;
 68       imageStitcher.RunParams.DestinationImageAlignmentY = 0;
 69       
 70     }
 71     
 72     else if(Counter == 2)
 73     {
 74       //Second sub-image goes into the upper right cornet
 75       imageStitcher.RunParams.DestinationImageAlignmentX = image.Width;
 76       imageStitcher.RunParams.DestinationImageAlignmentY = 0;
 77     }
 78     
 79     else if(Counter == 3)
 80     {
 81       //Third sub-image goes into the lower left corner
 82       imageStitcher.RunParams.DestinationImageAlignmentX = 0;
 83       imageStitcher.RunParams.DestinationImageAlignmentY = image.Height;
 84     }
 85     
 86     else
 87     {
 88       //Final sub-image goes into the lower right corner
 89       imageStitcher.RunParams.DestinationImageAlignmentX = image.Width;
 90       imageStitcher.RunParams.DestinationImageAlignmentY = image.Height;
 91     }
 92     
 93     //Run the tool to add the just-acquired sub-image
 94     imageStitcher.InputImage = CogImageConvert.GetIntensityImage(image, 0, 0, image.Width, image.Height);
 95     imageStitcher.Run();
 96     
 97     if(Counter == 4)
 98     {
 99       //Set the acquired image to the final stitched image
100       image = imageStitcher.OutputImage;
101       
102       //Reset to begin a new stitched image next time
103       imageStitcher = null;
104       Counter = 0;
105       
106       //Return true to inspect the stitched image
107       return true;
108     }
109  
110     else
111     {
112       //Return false to skip inspectiona nd acquire the next sub-image
113       return false;
114     }
115   }
116 #endregion
117  
118 #region "When the Script is Initialized"
119   //Perform any initialization required by your script here.
120   public override void Initialize(CogJob jobParam)
121   {
122     //DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
123     base.Initialize(jobParam);
124     
125     imageStitcher = null;
126     Counter = 0;
127   }
128 #endregion
129  
130 }
复制代码

最终拼接效果如图所示:

posted on   一杯清酒邀明月  阅读(1954)  评论(1编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示