This chapter shows you how to create your own image processing functions.
The source code to this sample application can be found in the samples\VB6\Image Processing directory.
Create a new project and add IC Imaging Control to the form. Before you run the program, select the video device, input and video format as shown in the First Steps in Visual Basic 6 chapter. Alternatively, run the program without selecting a device. In this case, the program shows the device selection dialog provided by IC Imaging Control. If you close this dialog without having made a selection, the program will display an error message and terminate.
Add 3 buttons to the form and label them Start Live, Stop Live and Process. Name them cmdStartLive, cmdStopLive and cmdProcess respectively. Now add the following code to the Click events of the buttons:
Private Sub cmdStartLive_Click() ICImagingControl1.LiveStart ' Store in VideoHasStarted whether a video stream ' has been started once. If VideoHasStarted is True, ' you can be sure that there is an image that can be processed. VideoHasStarted = True End Sub
Private Sub cmdStopLive_Click() ICImagingControl1.LiveStop End Sub
Dim x As Integer, y As Integer Dim ImageData As Variant ImageData = ICImagingControl1.MemoryGetImageData Dim MaxX, MaxY As Integer MaxY = ICImagingControl1.ImageHeight - 1 MaxX = ICImagingControl1.ImageWidth - 1 If ICImagingControl1.MemoryCurrentGrabberColorformat = ICY8 Then For y = 0 To MaxY For x = 0 To MaxX ImageData(x, y) = 255 - ImageData(x, y) Next x Next y ElseIf ICImagingControl1.MemoryCurrentGrabberColorformat = ICRGB24 Then For y = 0 To MaxY For x = 0 To MaxX 'blue ImageData(x * 3 + 0, y) = 255 - ImageData(x * 3 + 0, y) 'green ImageData(x * 3 + 1, y) = 255 - ImageData(x * 3 + 1, y) 'red ImageData(x * 3 + 2, y) = 255 - ImageData(x * 3 + 2, y) Next x Next y Else MsgBox "Please select a different 'MemoryCurrentGrabberColorformat'" Exit Sub End If ICImagingControl1.MemoryReleaseImageData ImageData ICImagingControl1.Display
First, an image is captured automatically when the LiveStop method is called. This can either be done directly by clicking the Stop Live button, or implicitly by clicking the Process button. The variable VideoHasStarted is used to determine whether live video has been started at least once, in which case image processing can be performed.
The image data returned by MemoryGetImageData can be accessed as a 2-dimensional array of bytes. The two nested for..next loops are typical for image processing algorithms, in that they access individual pixels of the image by specifying their coordinates. The algorithm shown here simply inverts the image. Finally, a call to MemoryReleaseImageData is required to release the image buffer.
The sample code above requires 8-bit or 24-bit image data, so we need to select an appropriate value for the MemoryCurrentGrabberColorformat Property:
If a image buffer format is selected, which has more than 8 bits per pixel, MemoryGetImageData will still return an array of bytes, but with a width that is a multiple of the image width. For instance, if the video format is RGB24(640x480), 640 by 480 pixels with 3 bytes per pixel, then the image buffer will be 1920 by 480 bytes in size.
For RGB24, the pixel data has to be interpreted as follows: The first byte of a row is the blue value of the first pixel, the second byte is the green value of the first pixel and the third byte is the red value of the first pixel. The fourth byte is the blue value of pixel 2, and so on.
And this is what the result will look like:
While possible and required in some cases, you should avoid using different formats for VideoFormat and MemoryCurrentGrabberColorformat, because the image data then needs to be converted, resulting in poor performance.
Established in 1990, The Imaging Source is one of the leading manufacturers of industrial cameras, frame grabbers and video converters for production automation, quality assurance, logistics, medicine, science and security.
Our comprehensive range of cameras with USB 3.1, USB 3.0, USB 2.0, GigE interfaces and other innovative machine vision products are renowned for their high quality and ability to meet the performance requirements of demanding applications.