コード例 #1
0
ファイル: main.cpp プロジェクト: iamlixiao/archives-12s2
int main(){
    GpuMat img1, img2(imread("/home/lixiao/pgm/cv/130526cv/2.png",IMREAD_GRAYSCALE));
    Mat rst=imread("/home/lixiao/pgm/cv/130526cv/1.png",IMREAD_GRAYSCALE);
    img1.upload(rst);

    SURF_GPU surf;
    GpuMat keypoints1GPU, keypoints2GPU;
    GpuMat descriptors1GPU, descriptors2GPU;
    surf(img1, GpuMat(), keypoints1GPU, descriptors1GPU);

    timeval start,finish;
    gettimeofday(&start,NULL);
    surf(img2, GpuMat(), keypoints2GPU, descriptors2GPU);
    gettimeofday(&finish,NULL);
    std::cout<<(1000000*(finish.tv_sec - start.tv_sec) + finish.tv_usec - start.tv_usec)<<std::endl;

    BFMatcher_GPU matcher(NORM_L2);
    GpuMat trainIdx, distance;
    gettimeofday(&start,NULL);
    matcher.matchSingle(descriptors1GPU, descriptors2GPU, trainIdx, distance);
    gettimeofday(&finish,NULL);
    std::cout<<(1000000*(finish.tv_sec - start.tv_sec) + finish.tv_usec - start.tv_usec)<<std::endl;

    vector<KeyPoint> keypoints1, keypoints2;
    vector<float> descriptors1, descriptors2;
    vector<DMatch> matches;
    gettimeofday(&start,NULL);
    surf.downloadKeypoints(keypoints1GPU, keypoints1);
    surf.downloadKeypoints(keypoints2GPU, keypoints2);
    surf.downloadDescriptors(descriptors1GPU, descriptors1);
    surf.downloadDescriptors(descriptors2GPU, descriptors2);
    BFMatcher_GPU::matchDownload(trainIdx, distance, matches);
    gettimeofday(&finish,NULL);
    std::cout<<(1000000*(finish.tv_sec - start.tv_sec) + finish.tv_usec - start.tv_usec)<<std::endl;

    // drawing the results
    Mat img_matches;
    drawMatches(Mat(img1), keypoints1, Mat(img2), keypoints2, matches, img_matches);

    imshow("hh",img_matches);
    waitKey(0);
}
コード例 #2
0
ファイル: im_utility.cpp プロジェクト: jslhs/imtools
	matches im_utility::diff(const std::string &im_file1, const std::string &im_file2, const parameters &params, key_points *im1_kps, key_points *im2_kps)
	{
		using namespace cv;
		matches mt;
		std::vector<KeyPoint> im1_key_points, im2_key_points;
		std::vector<DMatch> matches;
		std::vector<DMatch> good_matches;

		double min_hessian = 400.0;
		if (params.find(key_hessian_threshold) != params.end()) min_hessian = params.at(key_hessian_threshold);
		double sigma = 2.0;
		if (params.find(key_match_threshold) != params.end()) sigma = params.at(key_match_threshold);
		int speedup = speedup_default;
		if (params.find(key_speedup) != params.end())
			speedup = params.at(key_speedup);

		//auto info = getBuildInformation();
		
		if (speedup == speedup_use_cuda)
		{
			using namespace gpu;
			DeviceInfo dev;
			auto name = dev.name();
			GpuMat img1, img2;

			// upload data
			img1.upload(imread(im_file1, CV_LOAD_IMAGE_GRAYSCALE));
			img2.upload(imread(im_file2, CV_LOAD_IMAGE_GRAYSCALE));

			// detect keypoints & computing descriptors
			SURF_GPU surf;
			GpuMat kps_gpu1, kps_gpu2;
			GpuMat desc_gpu1, desc_gpu2;

			surf(img1, GpuMat(), kps_gpu1, desc_gpu1);
			surf(img2, GpuMat(), kps_gpu2, desc_gpu2);

			// matching descriptors
			BFMatcher_GPU matcher_gpu(NORM_L2);
			GpuMat trainIdx, distance;
			matcher_gpu.matchSingle(desc_gpu1, desc_gpu2, trainIdx, distance);

			// download results
			std::vector<float> desc1, desc2;
			surf.downloadKeypoints(kps_gpu1, im1_key_points);
			surf.downloadKeypoints(kps_gpu2, im2_key_points);
			surf.downloadDescriptors(desc_gpu1, desc1);
			surf.downloadDescriptors(desc_gpu2, desc2);
			BFMatcher_GPU::matchDownload(trainIdx, distance, matches);

			good_matches = matches;
		}
		else if (speedup == speedup_use_ocl)
		{
			using namespace ocl;
			DevicesInfo devs;
			getOpenCLDevices(devs, CVCL_DEVICE_TYPE_GPU);

			int dev = 0;
			if (params.find(key_ocl_dev) != params.end())
				dev = params.at(key_ocl_dev);

 			if (dev < 0 || dev >= devs.size()) dev = 0;
			setDevice(devs[dev]);

			SURF_OCL surf(800.0);
			BFMatcher_OCL matcher(NORM_L2);
			oclMat desc1, desc2;
			oclMat im1, im2;
			im1 = imread(im_file1, CV_LOAD_IMAGE_GRAYSCALE);
			im2 = imread(im_file2, CV_LOAD_IMAGE_GRAYSCALE);
			surf(im1, oclMat(), im1_key_points, desc1);
			surf(im2, oclMat(), im2_key_points, desc2);
			matcher.match(desc1, desc2, matches);

			double max_dist = 0;
			double min_dist = 100;
			for (int i = 0; i < desc1.rows; i++)
			{
				double dist = matches[i].distance;
				if (dist < min_dist) min_dist = dist;
				if (dist > max_dist) max_dist = dist;
			}

			for (int i = 0; i < desc1.rows; i++)
			{
				if (matches[i].distance <= max(sigma * min_dist, 0.02))
				{
					good_matches.push_back(matches[i]);
				}
			}
		}
		else
		{
			auto get_file_desc = [](const std::string &filename, double min_hessian, std::vector<cv::KeyPoint> *out_key_points)
			{
				using namespace cv;
				Mat desc;
				Mat img = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
				if (!img.data) return desc;

				SurfFeatureDetector detecter(min_hessian);

				std::vector<KeyPoint> key_points;
				detecter.detect(img, key_points);

				if (out_key_points) *out_key_points = key_points;

				if (key_points.empty())
				{
					std::cout << "no feature detected: " << filename << std::endl;
					return desc;
				}

				SurfDescriptorExtractor extractor;
				extractor.compute(img, key_points, desc);

				return desc;
			};

			auto im1_desc = get_file_desc(im_file1, min_hessian, &im1_key_points);
			auto im2_desc = get_file_desc(im_file2, min_hessian, &im2_key_points);
			
			FlannBasedMatcher matcher;

			matcher.match(im1_desc, im2_desc, matches);

			double max_dist = 0;
			double min_dist = 100;
			for (int i = 0; i < im1_desc.rows; i++)
			{
				double dist = matches[i].distance;
				if (dist < min_dist) min_dist = dist;
				if (dist > max_dist) max_dist = dist;
			}

			for (int i = 0; i < im1_desc.rows; i++)
			{
				if (matches[i].distance <= max(sigma * min_dist, 0.02))
				{
					good_matches.push_back(matches[i]);
				}
			}
		}

		auto conv_keypoint = [](const std::vector<KeyPoint> &kps)
		{
			key_points out;
			std::transform(kps.begin(), kps.end(), std::back_inserter(out), [](const KeyPoint &kp){
				//return key_point{ kp.pt.x, kp.pt.y, kp.size, kp.angle };
				key_point p;
				p.x = kp.pt.x;
				p.y = kp.pt.y;
				p.size = kp.size;
				p.angle = kp.angle;
				return p;
			});
			return out;
		};

		auto kps1 = conv_keypoint(im1_key_points);
		auto kps2 = conv_keypoint(im2_key_points);

		if (im1_kps)
			*im1_kps = kps1;
		if (im2_kps)
			*im2_kps = kps2;

		std::transform(good_matches.begin(), good_matches.end(), std::back_inserter(mt), [&](const DMatch &m)
		{
			//return match{ kps1[m.queryIdx] , kps2[m.trainIdx] , m.distance };
			match ma;
			ma.pt1 = kps1[m.queryIdx];
			ma.pt2 = kps2[m.trainIdx];
			ma.distance = m.distance;
			return ma;
		});

		return mt;
	}
コード例 #3
0
int main(int argc, char* argv[])
{
    if (argc != 5)
    {
        help();
        return -1;
    }

    GpuMat img1, img2;
    for (int i = 1; i < argc; ++i)
    {
        if (string(argv[i]) == "--left")
        {
            img1.upload(imread(argv[++i], CV_LOAD_IMAGE_GRAYSCALE));
            CV_Assert(!img1.empty());
        }
        else if (string(argv[i]) == "--right")
        {
            img2.upload(imread(argv[++i], CV_LOAD_IMAGE_GRAYSCALE));
            CV_Assert(!img2.empty());
        }
        else if (string(argv[i]) == "--help")
        {
            help();
            return -1;
        }
    }

    cv::gpu::printShortCudaDeviceInfo(cv::gpu::getDevice());

    SURF_GPU surf;

    // detecting keypoints & computing descriptors
    GpuMat keypoints1GPU, keypoints2GPU;
    GpuMat descriptors1GPU, descriptors2GPU;
    surf(img1, GpuMat(), keypoints1GPU, descriptors1GPU);
    surf(img2, GpuMat(), keypoints2GPU, descriptors2GPU);
    
    cout << "FOUND " << keypoints1GPU.cols << " keypoints on first image" << endl;
    cout << "FOUND " << keypoints2GPU.cols << " keypoints on second image" << endl;

    // matching descriptors
    BruteForceMatcher_GPU< L2<float> > matcher;
    GpuMat trainIdx, distance;
    matcher.matchSingle(descriptors1GPU, descriptors2GPU, trainIdx, distance);
    
    // downloading results
    vector<KeyPoint> keypoints1, keypoints2;
    vector<float> descriptors1, descriptors2;
    vector<DMatch> matches;
    surf.downloadKeypoints(keypoints1GPU, keypoints1);
    surf.downloadKeypoints(keypoints2GPU, keypoints2);
    surf.downloadDescriptors(descriptors1GPU, descriptors1);
    surf.downloadDescriptors(descriptors2GPU, descriptors2);
    BruteForceMatcher_GPU< L2<float> >::matchDownload(trainIdx, distance, matches);

    // drawing the results
    Mat img_matches;
    drawMatches(Mat(img1), keypoints1, Mat(img2), keypoints2, matches, img_matches);
    
    namedWindow("matches", 0);
    imshow("matches", img_matches);
    waitKey(0);

    return 0;
}