
| 语言: | .NET C#/Visual Basic |
| 版本: | 3.0.3 |
| 发布于: | 2007年7月27日 |
| 作者: | IC Imaging Control 技术支持部 |
| 系统要求: | IC Imaging Control >3.0.3, Visual Studio™ 2005 由WDM数据流类驱动程序驱动的相机、视频转换器或图像采集卡 |
| |
简介
本示例程序将演示如何找到视频图像里的一个激光点,并在这个点上标记出一个十字准心。
程序窗口如图所示:

程序开始后首先调用用于选择视频设备的内置对话框 (.ShowDeviceSettingsDialog)。 而后,依次执行以下几个步骤:
[C#] private TIS.Imaging.OverlayBitmap overlayBitmap;
[C#] private void Form1_Load(object sender, EventArgs e) { icImagingControl1.ShowDeviceSettingsDialog(); if (!icImagingControl1.DeviceValid) { cmdStartLive.Enabled = false; cmdSettings.Enabled = false; } // The color format of the image buffers is set to ICRGB24. icImagingControl1.MemoryCurrentGrabberColorformat = TIS.Imaging.ICImagingControlColorformats.ICRGB24; // The overlaybitmap position is set in the display path. icImagingControl1.OverlayBitmapPosition = TIS.Imaging.PathPositions.Display; overlayBitmap = icImagingControl1.OverlayBitmapAtPath[TIS.Imaging.PathPositions.Display]; // Enable the overlay. overlayBitmap.Enable = true; // Save current frame to the ImageActiveBuffer. icImagingControl1.LiveCaptureContinuous = true; cmdFindLaser.Enabled = false; cmdStoplive.Enabled = false; }
用户点击 find! 按钮后,程序将调用 findLaserPoint。 如果找到了激光点,则这个方法返回 True 值,通过变量 xLaser 及 yLaser 获取这个点的坐标值。 而后在这个点的位置(xLaser 和 yLaser)上标记出十字准心。
[C#] private void cmdFindLaser_Click(object sender, EventArgs e) { int xLaser, yLaser; // Clear the overlay. overlayBitmap.Fill(overlayBitmap.DropOutColor); if (findLaserPoint(out xLaser, out yLaser)) { DrawCrosshairs(xLaser, yLaser); } else { MessageBox.Show("No laser point found!"); } }
在cmdFindLaser_Click中调用的方法 findLaserPoint执行搜寻图像中激光点的具体操作。
通过调用ImageBuffers.Activebuffer,它将图像缓存中的最近一帧图像赋值给 imagebuffer。
激光点的搜寻工作只针对每个像素的红色分量值。
图象缓存中像素的引用方式是:行 x 与列 y 组成的坐标。
为了获取每个像素的红色分量值,每个 x 的值都首先需要乘以3,因为 RGB24 色彩格式为每个像素提供了3bytes, 而后,因为色彩值分量的存储顺序为“蓝、绿、红”,所以需要再加2。
纵向坐标 y 也需要重新计算一遍,因为每幅图像都是上下颠倒地被存储在图象缓存中的。
这些计算结果都被存储在变量 indexXred 与 indexY中,imagebuffer 将使用它们作为访问每个像素点的参数。
激光点在图像中都不只一个像素那么大。 为了计算出激光点的中心位置,程序使用 redPixel 用以统计所有激光点像素。 此外,坐标值x 与 y也通过变量 xLaser 和 yLaser 进行累积。 在整幅图像处理完之后,经过累积的x和y的值-xLaser 和 yLaser 将被所找到的象素点的数目 redPixel 除。 这样就获得了激光点的中心位置。
具体的处理过程需要在两个 for 循环内完成, 它们分别被用于遍历一幅图像的每一行和每一列。 如果当前像素点的红色分量值大于事先设定的阈值 (230),该像素就被视为一个激光点像素。 这个方法最后计算所有激光点像素区域的中心, 并返回其坐标值。
[C#] private bool findLaserPoint(out int xLaser, out int yLaser) { TIS.Imaging.ImageBuffer imagebuffer = icImagingControl1.ImageActiveBuffer; int redPixel = 0; xLaser = 0; yLaser = 0; // Check each pixel in the image if the red value is greater than the desired threshold (230). for (int y = 0; y < imagebuffer.Lines; y++) { for (int x = 0; x < imagebuffer.PixelPerLine; x++) { // x and y index for the imagebuffer. int indexXred = x * 3 + 2; int indexY = imagebuffer.Lines - y - 1; if (imagebuffer[indexXred, indexY] > 230) { // Count red pixels. redPixel++; xLaser = xLaser + x; yLaser = yLaser + y; } } } // Calculate the center of all red pixels. if (redPixel > 0) { xLaser = xLaser / redPixel; yLaser = yLaser / redPixel; return true; } else { return false; } }
十字准心由两条红线交叉构成,同时附有白色文字用以显示其位置坐标。具体做法是两次调用 overlayBitmap.DrawLine 用于划线,一次调用 overlayBitmap.DrawText 用以生成坐标文字。
[C#] private void DrawCrosshairs(int x, int y) { overlayBitmap.DrawLine(Color.Red, x, y - 10, x, y + 10); overlayBitmap.DrawLine(Color.Red, x - 10, y, x + 10, y); overlayBitmap.DrawText(Color.White, x + 3, y + 2, x.ToString() + "," + y.ToString()); }
责任声明
IC Imaging Control 源代码库中的所有代码均只用于教学目的,The Imaging Source Europe GmbH 作为IC Imaging Control的开发制造商,不对任何由于使用本文或其中源代码所产生的后果承担责任。