最简单的方法是为您希望跟踪的每种颜色创建一个阈值图像。您可以修改函数以将它们作为参数,而不是对阈值范围进行硬编码。这使您可以将函数重新用于不同的对象。
修改后的函数可能如下所示:
IplImage* GetThresholdedImage(IplImage* imgHSV, CvScalar lower, CvScalar upper)
{
IplImage* imgThresh=cvCreateImage(cvGetSize(imgHSV),IPL_DEPTH_8U, 1);
cvInRangeS(imgHSV, lower, upper, imgThresh);
return imgThresh;
}
然后对不同的对象使用不同的阈值来调用它:
IplImage* hsv; /* Already initialized*/
/* Set thresholds for blue and green objects as an example. */
/*Obviously, set these to be whatever is necessary. */
CvScalar blue_lower = cvScalar(110,60,10);
CvScalar blue_upper = cvScalar(120,256,256);
CvScalar green_lower = cvScalar(40,60,10);
CvScalar green_upper = cvScalar(71,256,256);
/* Get the images thresholded for blue and green. */
IplImage* blue_mask = GetThresholdedImage(hsv, blue_lower, blue_upper);
IplImage* green_mask = GetThresholdedImage(hsv, green_lower, green_upper);