说明
1、在HSV颜色空间下进行颜色追踪,RGB颜色空间每个通道分量受亮度影响大,HSV颜色空间受亮度影响较小;
2、EmguCV与OpenCV的HSV取值: H:0-180 ; S: 0-255; V: 0-255(注意取值范围);

3、常用的HSV参考值:

4、使用Inrange()函数在HSV空间中寻找HSV在某一范围内的值,输出掩膜,作为寻找结果。
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Emgu.CV;
using Emgu.CV.Util;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
using Emgu.Util;
using System.Drawing;
namespace lesson28_console
{
class Program
{
static void Main(string[] args)
{
///图片颜色识别
//Mat srcImg = CvInvoke.Imread(@"C:\Users\hello\Desktop\EmguCVDemo\lesson28\lesson28_console\bin\Debug\opencv-logo-white.png");
//CvInvoke.Imshow("input", srcImg);
//Mat hsvImg = new Mat();
//Mat mask = new Mat();
//double h_min = 0, s_min = 43, v_min = 46;
//double h_max = 10, s_max = 255, v_max = 255;
//ScalarArray hsv_min = new ScalarArray(new MCvScalar(h_min, s_min, v_min));
//ScalarArray hsv_max = new ScalarArray(new MCvScalar(h_max, s_max, v_max));
//CvInvoke.CvtColor(srcImg, hsvImg, ColorConversion.Bgr2Hsv);
//CvInvoke.InRange(hsvImg, hsv_min, hsv_max, mask); //输出为符合要求的图像掩膜
//CvInvoke.MedianBlur(mask, mask, 5);
//CvInvoke.Imshow("mask", mask);
//VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
//VectorOfRect hierarchy = new VectorOfRect();
////发现轮廓
//CvInvoke.FindContours(mask, contours,hierarchy, RetrType.External, ChainApproxMethod.ChainApproxNone);
//VectorOfVectorOfPoint contours_approx = new VectorOfVectorOfPoint(contours.Size);
//Rectangle rect = new Rectangle();
//for (int i =0; i < contours.Size;i++)
//{
// CvInvoke.ApproxPolyDP(contours[i], contours_approx[i], 3, true);
// CvInvoke.DrawContours(srcImg, contours_approx, i, new MCvScalar(255, 0, 0), 1, LineType.EightConnected, hierarchy, 0);
// CvInvoke.Imshow("red", srcImg);
// rect = CvInvoke.BoundingRectangle(contours_approx[i]);
// CvInvoke.Rectangle(srcImg, rect, new MCvScalar(0, 0, 255), 1);
// CvInvoke.PutText(srcImg, "red", new Point(rect.X, rect.Y), FontFace.HersheyComplexSmall, 1.2, new MCvScalar(0, 255, 0));
//}
//CvInvoke.WaitKey(0);
///视频绿色物体追踪
//VideoCapture cap = new VideoCapture("1.mp4");
//if(!cap.IsOpened) //打开文件失败
//{
// Con

本文详细介绍了如何在HSV颜色空间中进行颜色追踪,包括不同颜色的HSV参考值、使用EmguCV与OpenCV进行颜色识别的方法,以及在图像和视频中追踪特定颜色物体的实例。
最低0.47元/天 解锁文章
2618

被折叠的 条评论
为什么被折叠?



