天天看點

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

跟蹤器根據顔色特征識别視訊中特定對象,使用這個跟蹤器需要知道目标對象顔色分布。

繼續閱讀