将四张图拼接在一起,新建作业,在配置中新建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 }
最终拼接效果如图所示: