OpenCV之用于跟踪的好特征(九)

在Harris角点检测器提出之后Shi和Tomasi提出更好的办法,称为”Good Features to Track”(用于跟踪的好特征)。原始论文http://www.ai.mit.edu/courses/6.891/handouts/shi94good.pdf。其使用不同的打分函数来提高质量。
采用这种方法可以找到给定图像中N个最强的角点。

int main(int argc, char* argv[])
{
	//Read the input value for the number of corners
	int numCorners;
	istringstream iss(argv[1]);
	iss >> numCorners;
	//Check if 'numCorners' is positive
	if (numCorners < 1)
		numCorners = 1;

	RNG rng(12345);
	string windowName = "Feature points";
	//Current frame
	Mat frame, frameGray;
	char ch;
	
	//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;
	}
	//Scaling factor to resize the input frames from the webcam
	float scalingFactor = 0.75;

	//Iterate until the user presses the ESC key
	while (true) {
		//Capture the current frame
		cap >> frame;
		//Resize the frame
		resize(frame, frame, Size(), scalingFactor, scalingFactor, INTER_AREA);
		//Convert to grayscale
		cvtColor(frame, frameGray, COLOR_BGR2GRAY);

		//Initialize the parameters for Shi-Tomasi algorithm
		vector<Point2f> corners;
		double qualityThreshold = 0.02;
		double minDist = 15;
		int blockSize = 5;
		bool useHarrisDetector = false;
		double k = 0.07;

		//Clone the input frame
		Mat frameCopy;
		frameCopy = frame.clone();
		//Apply corner detection
		goodFeaturesToTrack(frameGray, corners, numCorners, qualityThreshold, minDist, Mat(), blockSize, useHarrisDetector, k);
		//Parameters for the circles to display the corners
		int radius = 8;			//radius of the circles
		int thickness = 2;		//thickness of the circles
		int lineType = 8;

		//Draw the detected corners using circles
		for (size_t i = 0; i < corners.size(); i++) {
			Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
			circle(frameCopy, corners[i], radius, color, thickness, lineType, 0);
		}
		imshow(windowName, frameCopy);
		ch = waitKey(30);
		if (ch == 27)
			break;
	}
	cap.release();
	destroyAllWindows();
	return 0;
}

numCorners表示要跟踪的最大角点数量,可以100开始尝试。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值