示例#1
0
/*****************************************************************************
    *  @brief    : surfFeatureDetect
    *  @author   : Zhangle
    *  @date     : 2014/9/8 11:18
    *  @version  : ver 1.0
    *  @inparam  :
    *  @outparam :
*****************************************************************************/
void FeatureDetect::surfFeatureDetect(string inputImageName, string outputImageName,string outputTxtName) {
    Mat image = imread(inputImageName);
    Mat descriptors;
    vector<KeyPoint> keypoints;
    SURF surf;
    time_t beginTime = time(NULL);
    surf.detect(image,keypoints);
    time_t endTime = time(NULL);
    time_t runTime = endTime - beginTime;
    drawKeypoints(image,keypoints,image,Scalar(255,0,0));
    imwrite(outputImageName,image);
    ofstream outTxt(outputTxtName);
    outTxt << "SURF" << endl;
    outTxt << "影像尺寸:" << image.cols<<" * "<<image.rows<<endl;
    outTxt << "特征点数目:" << keypoints.size() <<"个"<< endl;
    outTxt << "提取特征点耗费时间:" << runTime << "s"<< endl;
    outTxt << "默认参数设置" << endl;
    outTxt.close();
}
int main(int argc, char* argv[])
{
	//video input
	string videoName("A_kind_of_a_Show.avi");
	VideoCapture capture(videoName);
	if (!capture.isOpened())
	{
		cout << "!capture.isOpened()";
		return -1;
	}

	//path list
	vector<vector<Point2f>> pathList;
	vector<int> kpIdx2pathListIdx;
	
	vector<KeyPoint> kpTrackedPrev;
	Mat desTrackedPrev;
	vector<KeyPoint> kpEdgePrev;
	Mat desEdgePrev;

	//firstFrame init
	Mat firstFrame;
	Mat frame, framePrev;
	capture.read(firstFrame);
	keypointDetectorAnddescriptor.detect(firstFrame, kpTrackedPrev);
	keypointDetectorAnddescriptor.compute(firstFrame, kpTrackedPrev, desTrackedPrev);
	getEdgeKeypoint(firstFrame.cols, firstFrame.rows, 0.25,
		kpTrackedPrev, desTrackedPrev,
		kpEdgePrev, desEdgePrev);
	for (int i = 0; i < kpTrackedPrev.size(); ++i)
	{
		pathList.push_back(vector<Point2f>());
		pathList[i].push_back(kpTrackedPrev[i].pt);
		kpIdx2pathListIdx.push_back(i);
	}
	firstFrame.copyTo(framePrev);

	//video writer
	VideoWriter vw("result.avi", CV_FOURCC('M', 'J', 'P', 'G'), 12, Size(firstFrame.cols, firstFrame.rows));
	if (!vw.isOpened())
		return -1;

	//frame
	vector<KeyPoint> kpCur;
	Mat desCur;
	int frameIdx = 0;

	//processing
	while (capture.read(frame))
	{
		++frameIdx;

		keypointDetectorAnddescriptor.detect(frame, kpCur);
		keypointDetectorAnddescriptor.compute(frame, kpCur, desCur);

		//edge keypoint matching for homography
		vector<Point2f> ptEdgeCurMatched;
		vector<Point2f> ptEdgePrevMatched;
		vector<vector<DMatch>> vvmatchs;
		matcher.knnMatch(desEdgePrev, desCur, vvmatchs, 2);
		for (int i = 0; i < vvmatchs.size(); ++i)
		{
			if (vvmatchs[i][0].distance < vvmatchs[i][1].distance * 0.8)
			{
				ptEdgeCurMatched.push_back(kpCur[vvmatchs[i][0].trainIdx].pt);
				ptEdgePrevMatched.push_back(kpEdgePrev[vvmatchs[i][0].queryIdx].pt);
			}
		}

		//findHomography
		Mat h = findHomography(ptEdgePrevMatched,ptEdgeCurMatched, RANSAC);
		cout << h << endl;
		
		// camera movement compensation
		for (vector<Point2f>& path : pathList){
			perspectiveTransform(path, path, h);
		}

		Mat warpedframe;
		warpPerspective(framePrev, warpedframe, h, frame.size());
		imshow("frame", frame);
		imshow("prev", framePrev);
		imshow("warpedframe", warpedframe);

		getEdgeKeypoint(frame.cols, frame.rows, 0.25,
			kpCur, desCur,
			kpEdgePrev, desEdgePrev);
		frame.copyTo(framePrev);

		//keypoint tracking for pathlist
		vector<int> kpIdx2pathListIdxTemp;
		vector<KeyPoint> kpTrackedCur;
		Mat desTrackedCur;
		set<int> curMatchedKpIdxSet;
		matcher.knnMatch(desTrackedPrev, desCur, vvmatchs, 2);
		for (int i = 0; i < vvmatchs.size(); ++i)
		{
			if (vvmatchs[i][0].distance < vvmatchs[i][1].distance * 0.6)
			{
				pathList[kpIdx2pathListIdx[i]].push_back(kpCur[vvmatchs[i][0].trainIdx].pt);
				kpTrackedCur.push_back(kpCur[vvmatchs[i][0].trainIdx]);
				desTrackedCur.push_back(desCur.row(vvmatchs[i][0].trainIdx));
				kpIdx2pathListIdxTemp.push_back(kpIdx2pathListIdx[i]);
				curMatchedKpIdxSet.insert(vvmatchs[i][0].trainIdx);
			}
		}
		if (frameIdx%5==0)
		{
			//add new keypoint
			for (int i = 0; i < kpCur.size(); ++i)
			{
				if (curMatchedKpIdxSet.find(i) == curMatchedKpIdxSet.end()){
					kpTrackedCur.push_back(kpCur[i]);
					desTrackedCur.push_back(desCur.row(i));
					pathList.push_back(vector<Point2f>());
					pathList.rbegin()->push_back(kpCur[i].pt);
					kpIdx2pathListIdxTemp.push_back(pathList.size() - 1);
				}
			}
		}

		kpIdx2pathListIdx = kpIdx2pathListIdxTemp;
		kpTrackedPrev = kpTrackedCur;
		desTrackedCur.copyTo(desTrackedPrev);
		Mat show;
		drawPathList(frame, show, pathList);
		imshow("pathlist", show);
		waitKey(1);

		vw << show;
	}
	vw.release();
	//uniform
	for (vector<Point2f>& path : pathList)
	{
		for (Point2f& pt : path)
		{
			pt.x /= firstFrame.cols;
			pt.y /= firstFrame.rows;
		}
	}

	vector<double> motionHist;
	calMotionHist(pathList, motionHist);
	cout << "h : " << endl;
	for (double h : motionHist)
		cout << h << " ";
	cout << endl;
	return 1;
}