Example #1
0
int main(int argc, char** argv)
{
    if(argc != 3)
    {
        help();
        return -1;
    }

    Mat img1 = imread(argv[1], IMREAD_GRAYSCALE);
    Mat img2 = imread(argv[2], IMREAD_GRAYSCALE);
    if(img1.empty() || img2.empty())
    {
        printf("Can't read one of the images\n");
        return -1;
    }

    // detecting keypoints
    BRISK detector(400);
    std::vector<KeyPoint> keypoints1, keypoints2;
    detector.detect(img1, keypoints1);
    detector.detect(img2, keypoints2);

    // computing descriptors
    BRISK extractor;
    Mat descriptors1, descriptors2;
    extractor.compute(img1, keypoints1, descriptors1);
    extractor.compute(img2, keypoints2, descriptors2);

    // matching descriptors
    BFMatcher matcher(NORM_L2);
    std::vector<DMatch> matches;
    matcher.match(descriptors1, descriptors2, matches);

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

    return 0;
}
Example #2
0
//------------------------------------------------------------------------------
String PAN::authenticate(String CWD,String fileoutput){
	Point matchLoc;
	float percentage, threshold;
	float average = 0;
	int count = 0;
	Mat big_image;
	big_image = panimage.img->clone();//big image
	resize(big_image, big_image, Size(2000, 1500));
	if (!big_image.data)
	{
		std::cout << "Error reading images " << std::endl; return"";
	}
	Mat temp, temp1[3];
	if (big_image.channels() >= 2){
		cvtColor(big_image, temp, COLOR_BGR2GRAY);
	}
	//split(temp, temp1);
	big_image = temp.clone();
	/*img_1 = temp2.clone();
	resize(img_2, img_2, Size(600, 400));
	*///-- Step 1: Detect the keypoints using SURF Detector
	vector<KeyPoint> keypoints_big, keypoints_small;
	int minHessian = 200;
	//FeatureDetector * detector = new SURF();
	FastFeatureDetector detector;
	detector.detect(big_image, keypoints_big);
	cout << "big sift done\n\n";

	//-- Step 2: Calculate descriptors (feature vectors)
	int Threshl = 10;
	int Octaves = 3;
	//(pyramid layer) from which the keypoint has been extracted
	float PatternScales = 1.0f;
	//declare a variable BRISKD of the type cv::BRISK
	Mat descriptors_2, descriptors_small;
	BRISK BRISKD;

	//BRISKD.detect(img_1, keypoints_1);
	//BRISKD.detect(img_2, keypoints_2);
	BRISKD.compute(big_image, keypoints_big, descriptors_2);

	cout << "big brisk done\n\n";



	int i = 0;
	for ( i = 0; i < 7; i++){
		String path(CWD);
		// setting up input standard containers used for matching to
		String temp = "win1";
		temp = temp + char(i + 48) + ".jpg";
		path = path + temp;
		Mat find = imread(path, CV_LOAD_IMAGE_UNCHANGED);
		//cout << path << "\n\n";
		if (find.data == NULL){ break; }
		//templateMatch(*panimage.img, find, matchLoc, threshold, percentage);
		//-------------------------------------------------------------------------------------
		if (!find.data)
		{
			std::cout << "Error reading images " << std::endl; return "";
		}

		if (find.channels() >= 2){
			cvtColor(find,find, COLOR_BGR2GRAY);
		}


		//img_1 = temp2.clone();
		resize(find ,find, Size(1200, 600));
		//-- Step 1: Detect the keypoints using SURF Detector
		vector<KeyPoint>  keypoints_small;
		int minHessian = 200;
		detector.detect(find, keypoints_small);
		cout << "small sift done\n\n";

		//-- Step 2: Calculate descriptors (feature vectors)
		int Threshl = 10;
		int Octaves = 3;
		//(pyramid layer) from which the keypoint has been extracted
		float PatternScales = 1.0f;
		//declare a variable BRISKD of the type cv::BRISK
		Mat descriptors_small;
		//BRISKD.detect(img_1, keypoints_1);
		//BRISKD.detect(img_2, keypoints_2);
		BRISKD.compute(find, keypoints_small, descriptors_small);
		cout << "brisk done\n\n";

		//-------------------------------------------------------------------------------------
		

		//-- Step 3: Matching descriptor vectors using FLANN matcher
		//FlannBasedMatcher matcher;

		BFMatcher matcher;
		std::vector< DMatch > matches;
		matcher.match(descriptors_small, descriptors_2, matches);
		cv::Mat all_matches;
		drawMatches(find, keypoints_small, big_image, keypoints_big, matches, all_matches, cv::Scalar::all(-1), cv::Scalar::all(-1), vector<char>(), cv::DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
		
		namedWindow("BRISK", CV_WINDOW_NORMAL);
			imshow("BRISK", all_matches);
		cv::waitKey(0);
		double max_dist = 0; double min_dist = 800;

		//-- Quick calculation of max and min distances between keypoints
		for (int i = 0; i < descriptors_small.rows; i++)
		{
			double dist = matches[i].distance;
			if (dist < min_dist) min_dist = dist;
			if (dist > max_dist) max_dist = dist;
		}

		//-- Draw only "good" matches (i.e. whose distance is less than 2*min_dist,
		//-- or a small arbitary value ( 0.02 ) in the event that min_dist is very
		//-- small)
		//-- PS.- radiusMatch can also be used here.
		std::vector< DMatch > good_matches;

		for (int i = 0; i < descriptors_small.rows; i++)
		{
			if (matches[i].distance <= 1.2 * min_dist)		{
				good_matches.push_back(matches[i]);
			}
		}

		//-- Draw only "good" matches
		Mat img_matches;
		drawMatches(find, keypoints_small, big_image, keypoints_big,good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);

		////-- Show detected matches
		namedWindow("Good Matches", CV_WINDOW_NORMAL);
		imshow("Good Matches", img_matches);
		waitKey();
		for (int i = 0; i < (int)good_matches.size(); i++)
		{
			//printf("-- Good Match [%d] Keypoint 1: %d  -- Keypoint 2: %d  \n", i, good_matches[i].queryIdx, good_matches[i].trainIdx);
		}
		percentage = (float)((float)good_matches.size() / (float)matches.size()) * 100;

		
		int width, height;
		width = find.size().width; height = find.size().height;
		//cout << i + 1 << "--LOC x=" << matchLoc.x << "  y=" << matchLoc.y << "  % match= " << percentage << "\n";
		if (percentage > 50){
			average += percentage;
			count++;
		}
		fileoutput = fileoutput + to_string(percentage) + "$";
		//cout << percentage << "$";
		//rectangle(find,Rect(matchLoc.x, matchLoc.y, width, height),0,1,4,0); //removed to prevent alteration of the find image
		find.release();
		}
	if (count != 0){
		average /= count;
	}
	else { average = 0; }
	
	//cout << "Authenticity is " << average <<"\n";
	authenticity = average;
	fileoutput = fileoutput + to_string(average) + "$";
	//cout << to_string(average) << "$";
	cout << fileoutput;
	if (i == 0){ cout << "database not loaded"; }
	return fileoutput;
}