Various thresholds

﹥>﹥吖頭↗ 提交于 2019-12-11 08:38:02

问题


I am trying to track multiple objects with different color at the same time via a single web cam. Now I can do that for single color with single threshold:

IplImage* GetThresholdedImage(IplImage* imgHSV)
{       
    IplImage* imgThresh=cvCreateImage(cvGetSize(imgHSV),IPL_DEPTH_8U, 1);
    cvInRangeS(imgHSV, cvScalar(170,160,60), cvScalar(180,2556,256), imgThresh); 
    return imgThresh;
}

I'm looking for some hints to do various threshold. Also if its possible, how many windows does it require? Do i need to assign different windows for different colors?


回答1:


The easiest way to do this is create a thresholded image for each color you wish to track. Instead of hard-coding your threshold ranges, you could modify your function to take them as parameters. This lets you re-use the function for different objects.

The modified function might look like this:

IplImage* GetThresholdedImage(IplImage* imgHSV, CvScalar lower, CvScalar upper)
{       
    IplImage* imgThresh=cvCreateImage(cvGetSize(imgHSV),IPL_DEPTH_8U, 1);
    cvInRangeS(imgHSV, lower, upper, imgThresh); 
    return imgThresh;
}

And then call it using different thresholds for different objects:

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);


来源:https://stackoverflow.com/questions/17477974/various-thresholds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!