IC Imaging Control .NET

Performing Image Processing

Performing Image Processing

This chapter shows you how to create your own image processing functions.

The source code for the VB.NET and C# versions of this sample program can be found in the directories samples\VB *\Image Processing and samples\C# *\Image Processing in your My Documents/IC Imaging Control 3.4 directory.

Setting up the Project

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 Visual Studio .NET 2003 - 2008 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:

[VB.NET]
Private Sub cmdStartLive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 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 ' VideoHasStopped is set to False, because the live ' video has just been started. VideoHasStopped = False End Sub
[C#]
private void cmdStartLive_Click_1(object sender, EventArgs e) { 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; // VideoHasStopped is set to False, because the live // video has just been started. VideoHasStopped = false; }
[VB.NET]
Private Sub cmdStopLive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStopLive.Click IcImagingControl1.LiveStop() If VideoHasStarted = True Then VideoHasStopped = True End If End Sub
[C#]
private void cmdStopLive_Click(object sender, EventArgs e) { icImagingControl1.LiveStop(); if (VideoHasStarted == true) { VideoHasStopped = true; } }
[VB.NET]
Private Sub cmdProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdProcess.Click Dim ImgBuffer As TIS.Imaging.ImageBuffer Dim x As Integer, y As Integer Dim BytesPerLine As Integer Cursor = Cursors.WaitCursor Try ' Check whether the video stream has been started once. ' If it has been started once, there are two possible situations: ' 1) The live video is running. In this case we stop the live video ' in order to get the last frame and display the processed image. ' 2) The live video has been stopped. In this case the last frame of the ' live video was grabbed automatically and can be processed and ' displayed. If VideoHasStarted Then If VideoHasStopped = False Then IcImagingControl1.LiveStop() End If ImgBuffer = IcImagingControl1.ImageActiveBuffer ' Calculate the count of bytes ber line using the color format and the ' pixels per line of the image buffer. BytesPerLine = ImgBuffer.BitsPerPixel / 8 * ImgBuffer.PixelPerLine - 1 For y = 0 To ImgBuffer.Lines - 1 For x = 0 To BytesPerLine ImgBuffer(x, y) = 255 - ImgBuffer(x, y) Next x Next y IcImagingControl1.Display() Else MessageBox.Show("Please click the Start Live button first!", _ "Image Processing", _ MessageBoxButtons.OK, _ MessageBoxIcon.None) End If Catch ex As System.Exception MessageBox.Show(ex.Message) End Try Cursor = Cursors.Default End Sub
[C#]
private void cmdProcess_Click(object sender, EventArgs e) { TIS.Imaging.ImageBuffer ImgBuffer; int x, y; int BytesPerLine; Cursor = Cursors.WaitCursor; try { // Check whether the video stream has been started once. // If it has been started once, there are two possible situations: // 1) The live video is running. In this case we stop the live video // in order to get the last frame and display the processed image. // 2) The live video has been stopped. In this case the last frame of the // live video was grabbed automatically and can be processed and // displayed. if (VideoHasStarted) { if (VideoHasStopped == false) { icImagingControl1.LiveStop(); } ImgBuffer = icImagingControl1.ImageActiveBuffer; // Calculate the count of bytes per line. BytesPerLine = ImgBuffer.BitsPerPixel / 8 * ImgBuffer.PixelPerLine; unsafe { byte* pDatabyte = ImgBuffer.GetImageData(); for (y = 0; y < ImgBuffer.Lines; y++) { for (x = 0; x < BytesPerLine; x++) { *pDatabyte = (byte)(255 - *pDatabyte); pDatabyte++; } } } icImagingControl1.DisplayImageBuffer(ImgBuffer); } else { MessageBox.Show("Please click the Start Live button first!", "Image Processing", MessageBoxButtons.OK, MessageBoxIcon.None); } } catch (Exception ex) { MessageBox.Show(ex.Message); } Cursor = Cursors.Default; }

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.

Then the active buffer is retrieved. This is the buffer that contains the data of the previously snapped image. The image data can be accessed with the Item property. 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.

The sample code above requires 8-bit or 24-bit image data, so we need to select an appropriate value for the MemoryCurrentGrabberColorformat Property:

image

image

Using Color Formats

If a video format is selected, which has more than 8 bits per pixel, it must be considered that the image buffer still contains the same number of pixel per line, but that each pixel contains more than 1 byte now, meaning the size of the buffer has increased. 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:

image

image

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.

<< Programmer's Guide

Get in touch with us


About The Imaging Source

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.

Automated Imaging Association ISO 9001:2015 certified

Contact us