Color tracking with OpenCV in C# can be accomplished by using the Emgu CV library, which is a .NET wrapper for the OpenCV library. This allows you to leverage OpenCV’s powerful computer vision functionality in C#. Below is an example of how you could implement basic color tracking in C# using Emgu CV.
Installation
- Install Emgu CV: You can install Emgu CV via NuGet Package Manager in Visual Studio. Search for
Emgu.CV and install via NuGet.
- Add References: Ensure you have added references to the necessary Emgu CV libraries in your project.
Sample Code
The following code demonstrates basic color tracking for a specific color range using OpenCV through Emgu CV. It tracks a colored object and draws a contour around it.
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using System.Drawing;
class ColorTracking
{
static void Main(string[] args)
{
// Initialize camera capture
var capture = new VideoCapture(0); // Use camera index 0
var frame = new Mat();
// Define color range (e.g., tracking a blue object)
var lowerBound = new ScalarArray(new MCvScalar(100, 150, 0)); // Lower HSV bounds
var upperBound = new ScalarArray(new MCvScalar(140, 255, 255)); // Upper HSV bounds
while (true)
{
capture.Read(frame);
if (frame.IsEmpty)
break;
// Convert to HSV color space for easier color filtering
Mat hsv = new Mat();
CvInvoke.CvtColor(frame, hsv, ColorConversion.Bgr2Hsv);
// Create mask based on color range
Mat mask = new Mat();
CvInvoke.InRange(hsv, lowerBound, upperBound, mask);
// Find contours from the mask
var contours = new Emgu.CV.Util.VectorOfVectorOfPoint();
CvInvoke.FindContours(mask, contours, null, RetrType.External, ChainApproxMethod.ChainApproxSimple);
// Draw the contours on the original frame
for (int i = 0; i < contours.Size; i++)
{
var contour = contours[i];
CvInvoke.DrawContours(frame, contours, i, new MCvScalar(0, 255, 0), 2);
// You can add additional logic to find the centroid or bounding box here
}
// Display the result
CvInvoke.Imshow("Color Tracking", frame);
// Exit if 'ESC' is pressed
if (CvInvoke.WaitKey(30) == 27)
break;
}
// Release resources
capture.Release();
CvInvoke.DestroyAllWindows();
}
}
Explanation
- VideoCapture: Initializes the webcam capture. You may need to adjust the camera index depending on your setup.
- HSV Conversion: The BGR image is converted to HSV to make color filtering easier.
- InRange: Applies the specified color range (lower and upper bounds) to create a binary mask.
- Contours: Finds the contours in the binary mask which represent the tracked objects.
- DrawContours: Draws the detected contours back on the original frame.
Notes
- Adjust the
lowerBound and upperBound for the specific color you want to track by manipulating the HSV values.
- Ensure that your machine has the necessary hardware drivers installed for webcam access.
- The code runs an infinite loop displaying the frame with tracked color contours until you press the 'ESC' key.
This code provides a simple introduction to color tracking using Emgu CV in C#. For more sophisticated tracking, such as handling multiple colors or different lighting conditions, further refinements would be necessary.