Color tracking using EMGUcv

烂漫一生 提交于 2019-12-06 02:54:50

Typically, the colored blob detection part of such an application works along the lines of:

  1. Convert the image to HSV (hue, saturation, value) color space.
  2. Filter the hue channel for all pixels with a hue value near the target value. Thresholding will typically give you all pixels with a value above or below the threshold. You are interested in the pixels near some target value.
  3. Filter the obtained mask some more, possibly using the saturation/value channels or by removing small blobs. Ideally only the target blob remains.

Some sample code that aims to find a green object (hue ~50) such as the green ball shown in the video:

// 1. Convert the image to HSV
using (Image<Hsv, byte> hsv = original.Convert<Hsv, byte>())
{
    // 2. Obtain the 3 channels (hue, saturation and value) that compose the HSV image
    Image<Gray, byte>[] channels = hsv.Split(); 

    try
    {
        // 3. Remove all pixels from the hue channel that are not in the range [40, 60]
        CvInvoke.cvInRangeS(channels[0], new Gray(40).MCvScalar, new Gray(60).MCvScalar, channels[0]);

        // 4. Display the result
        imageBox1.Image = channels[0];
    }
    finally
    {
        channels[1].Dispose();
        channels[2].Dispose();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!