OpenCV之学习对象跟踪—跟踪特定颜色的对象(六)

通过使用色彩空间信息来构建一个好的视觉跟踪器,颜色信息对光照条件敏感,实际应用程序中必须进行预处理解决光照问题。假设该问题已解决,获取的是干净的彩色图像。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;
}

跟踪器根据颜色特征识别视频中特定对象,使用这个跟踪器需要知道目标对象颜色分布。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值