用脚本可以实现
Imports System
Imports System.Threading
Imports System.Windows.Forms
Imports Cognex.VisionPro
Imports Cognex.VisionPro.QuickBuild
Imports Cognex.VisionPro.ImageProcessing
Public Class UserScript
Inherits CogJobBaseScript
' A CogCopyRegionTool is used to stitch images and hold the intermediate result.
Dim imageStitcher As CogCopyRegionTool
' A counter of the number of sub-images acquired. This is used to control the
' image stitching and determine when the final image is finished.
Dim counter As Integer
' Helper function which resets our internal variables so that the next acquired
' image will restart the stitching process.
Private Sub ResetImage
imageStitcher = Nothing
counter = 0
End Sub
' Called immediately after an acquisition has completed.
' Return true if the image should be inspected.
' Return false to skip the inspection and acquire another image.
Overrides Function PostAcquisitionRef(ByRef image As Cognex.VisionPro.ICogImage) As Boolean
counter = counter + 1
' First sub-image has been acquired
If Counter = 1
' Create a new tool
imageStitcher = new CogCopyRegionTool()
' Create a destination image and assign it to the tool
Dim stitchedImage As CogImage8Grey = new CogImage8Grey()
stitchedImage.Allocate(image.Width*2, image.Height*2)
imageStitcher.DestinationImage = stitchedImage
imageStitcher.Region = Nothing
imageStitcher.RunParams.ImageAlignmentEnabled = true
' First sub-image goes into the upper left corner
imageStitcher.RunParams.DestinationImageAlignmentX = 0
imageStitcher.RunParams.DestinationImageAlignmentY = 0
Else If Counter = 2
' Second sub-image goes into the upper right corner
imageStitcher.RunParams.DestinationImageAlignmentX = image.Width
imageStitcher.RunParams.DestinationImageAlignmentY = 0
Else If Counter = 3
' Third sub-image goes into the lower left corner
imageStitcher.RunParams.DestinationImageAlignmentX = 0
imageStitcher.RunParams.DestinationImageAlignmentY = image.Height
Else
' Final sub-image goes into the lower right corner
imageStitcher.RunParams.DestinationImageAlignmentX = image.Width
imageStitcher.RunParams.DestinationImageAlignmentY = image.Height
End If
' Run the tool to add the just-acquired sub-image
imageStitcher.InputImage = CType(image, CogImage8Grey)
imageStitcher.Run()
' If this was the last portion of the image
If Counter = 4
' Set the acquired image to the final stitched image
image = imageStitcher.OutputImage
' Reset to begin a new stitched image next time
ResetImage()
' Return true to inspect the stitched image
Return True
Else
' Return false to skip inspection and acquire the next sub-image
Return False
End If
End Function
#Region "When the Script is Initialized"
'Perform any initialization required by your script here
Overrides Sub Initialize(ByVal jobParam As CogJob)
'DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
MyBase.Initialize(jobParam)
ResetImage()
'Display an informational message box explaining the sample. Note that this
'is done from a separate thread so that you can still interact with QuickBuild
'while viewing the message box.
Dim msgThread As Thread
msgThread = new System.Threading.Thread(new ThreadStart(addressof showMsg))
msgThread.Start()
End Sub
'A helper subroutine to display the informational message box. This subroutine
'is run in a separate thread.
<MTAThread()> Sub showMsg()
Dim msgText as String
msgText = "This sample uses script to combine 4 acquired images into a single large" + Environment.NewLine
msgText += "image for inspection."
msgText += Environment.NewLine + Environment.NewLine
msgText += "To view the script:" + Environment.NewLine
msgText += " 1) Select Configure, Job Properties... from the Job Editor menu" + Environment.NewLine
msgText += " 2) Click the Edit Job Script button" + Environment.NewLine
MessageBox.Show(msgText, "Image Stitching Script Sample")
End Sub
#End Region
End Class
自带的例程里有 |