通过使用色彩空间信息来构建一个好的视觉跟踪器,颜色信息对光照条件敏感,实际应用程序中必须进行预处理解决光照问题。假设该问题已解决,获取的是干净的彩色图像。RGB是计算机屏幕上的原生表示形式,色调饱和度值(HSV)则信息量丰富与颜色感知相关,色调指色谱,饱和度指特定颜色强度,值为该像素亮度。
int main(int argc, char* argv[])
{
//Create the capture object
//0->input arg that specifies it should take the input from the webcam
VideoCapture cap(0);
if (!cap.isOpened()) {
cerr << "Unable to open the webcam. Exiting!" << endl;
return -1;
}
Mat frame, hsvImage, mask, outputImage;
char ch;
//Image size scaling factor for the input frames from the webcam
float scalingFactor = 0.75;
//Variable declarations and initializations
//Iterate until the user presses the Esc key
while(true) {
//Initialize the output image before each iteration
outputImage = Scalar(0, 0, 0);
//Capture the current frame
cap >> frame;
//Check if 'frame' is empty
if (frame.empty())
break;
//Resize the frame
resize(frame, frame, Size(), scalingFactor, scalingFactor, INTER_AREA);
//Convert to HSV colorspace
cvtColor(frame, hsvImage, COLOR_BGR2HSV);
//Define the range of "blue" color in HSV colorspace
Scalar lowerLimit = Scalar(60, 100, 100);
Scalar upperLimit = Scalar(180, 255, 255);
//Threshold the HSV image to get only blue color
inRange(hsvImage, lowerLimit, upperLimit, mask);
//Compute bitwise-ADD of input image and mask
bitwise_and(frame, frame, outputImage, mask = mask);
//Run median filter on the output to smoothen it
medianBlur(outputImage, outputImage, 5);
//Display the input and output image
imshow("Input", frame);
imshow("Output", outputImage);
ch = waitKey(30);
if (ch == 27)
break;
}
cap.release();
destroyAllWindows();
return 0;
}
跟踪器根据颜色特征识别视频中特定对象,使用这个跟踪器需要知道目标对象颜色分布。